tvplay.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_HTTPError,
  7. compat_urlparse,
  8. )
  9. from ..utils import (
  10. determine_ext,
  11. ExtractorError,
  12. int_or_none,
  13. parse_iso8601,
  14. qualities,
  15. try_get,
  16. update_url_query,
  17. url_or_none,
  18. )
  19. class TVPlayIE(InfoExtractor):
  20. IE_NAME = 'mtg'
  21. IE_DESC = 'MTG services'
  22. _VALID_URL = r'''(?x)
  23. (?:
  24. mtg:|
  25. https?://
  26. (?:www\.)?
  27. (?:
  28. tvplay(?:\.skaties)?\.lv(?:/parraides)?|
  29. (?:tv3play|play\.tv3)\.lt(?:/programos)?|
  30. tv3play(?:\.tv3)?\.ee/sisu|
  31. (?:tv(?:3|6|8|10)play|viafree)\.se/program|
  32. (?:(?:tv3play|viasat4play|tv6play|viafree)\.no|(?:tv3play|viafree)\.dk)/programmer|
  33. play\.nova(?:tv)?\.bg/programi
  34. )
  35. /(?:[^/]+/)+
  36. )
  37. (?P<id>\d+)
  38. '''
  39. _TESTS = [
  40. {
  41. 'url': 'http://www.tvplay.lv/parraides/vinas-melo-labak/418113?autostart=true',
  42. 'md5': 'a1612fe0849455423ad8718fe049be21',
  43. 'info_dict': {
  44. 'id': '418113',
  45. 'ext': 'mp4',
  46. 'title': 'Kādi ir īri? - Viņas melo labāk',
  47. 'description': 'Baiba apsmej īrus, kādi tie ir un ko viņi dara.',
  48. 'series': 'Viņas melo labāk',
  49. 'season': '2.sezona',
  50. 'season_number': 2,
  51. 'duration': 25,
  52. 'timestamp': 1406097056,
  53. 'upload_date': '20140723',
  54. },
  55. },
  56. {
  57. 'url': 'http://play.tv3.lt/programos/moterys-meluoja-geriau/409229?autostart=true',
  58. 'info_dict': {
  59. 'id': '409229',
  60. 'ext': 'flv',
  61. 'title': 'Moterys meluoja geriau',
  62. 'description': 'md5:9aec0fc68e2cbc992d2a140bd41fa89e',
  63. 'series': 'Moterys meluoja geriau',
  64. 'episode_number': 47,
  65. 'season': '1 sezonas',
  66. 'season_number': 1,
  67. 'duration': 1330,
  68. 'timestamp': 1403769181,
  69. 'upload_date': '20140626',
  70. },
  71. 'params': {
  72. # rtmp download
  73. 'skip_download': True,
  74. },
  75. },
  76. {
  77. 'url': 'http://www.tv3play.ee/sisu/kodu-keset-linna/238551?autostart=true',
  78. 'info_dict': {
  79. 'id': '238551',
  80. 'ext': 'flv',
  81. 'title': 'Kodu keset linna 398537',
  82. 'description': 'md5:7df175e3c94db9e47c0d81ffa5d68701',
  83. 'duration': 1257,
  84. 'timestamp': 1292449761,
  85. 'upload_date': '20101215',
  86. },
  87. 'params': {
  88. # rtmp download
  89. 'skip_download': True,
  90. },
  91. },
  92. {
  93. 'url': 'http://www.tv3play.se/program/husraddarna/395385?autostart=true',
  94. 'info_dict': {
  95. 'id': '395385',
  96. 'ext': 'mp4',
  97. 'title': 'Husräddarna S02E07',
  98. 'description': 'md5:f210c6c89f42d4fc39faa551be813777',
  99. 'duration': 2574,
  100. 'timestamp': 1400596321,
  101. 'upload_date': '20140520',
  102. },
  103. 'params': {
  104. 'skip_download': True,
  105. },
  106. },
  107. {
  108. 'url': 'http://www.tv6play.se/program/den-sista-dokusapan/266636?autostart=true',
  109. 'info_dict': {
  110. 'id': '266636',
  111. 'ext': 'mp4',
  112. 'title': 'Den sista dokusåpan S01E08',
  113. 'description': 'md5:295be39c872520221b933830f660b110',
  114. 'duration': 1492,
  115. 'timestamp': 1330522854,
  116. 'upload_date': '20120229',
  117. 'age_limit': 18,
  118. },
  119. 'params': {
  120. 'skip_download': True,
  121. },
  122. },
  123. {
  124. 'url': 'http://www.tv8play.se/program/antikjakten/282756?autostart=true',
  125. 'info_dict': {
  126. 'id': '282756',
  127. 'ext': 'mp4',
  128. 'title': 'Antikjakten S01E10',
  129. 'description': 'md5:1b201169beabd97e20c5ad0ad67b13b8',
  130. 'duration': 2646,
  131. 'timestamp': 1348575868,
  132. 'upload_date': '20120925',
  133. },
  134. 'params': {
  135. 'skip_download': True,
  136. },
  137. },
  138. {
  139. 'url': 'http://www.tv3play.no/programmer/anna-anka-soker-assistent/230898?autostart=true',
  140. 'info_dict': {
  141. 'id': '230898',
  142. 'ext': 'mp4',
  143. 'title': 'Anna Anka søker assistent - Ep. 8',
  144. 'description': 'md5:f80916bf5bbe1c5f760d127f8dd71474',
  145. 'duration': 2656,
  146. 'timestamp': 1277720005,
  147. 'upload_date': '20100628',
  148. },
  149. 'params': {
  150. 'skip_download': True,
  151. },
  152. },
  153. {
  154. 'url': 'http://www.viasat4play.no/programmer/budbringerne/21873?autostart=true',
  155. 'info_dict': {
  156. 'id': '21873',
  157. 'ext': 'mp4',
  158. 'title': 'Budbringerne program 10',
  159. 'description': 'md5:4db78dc4ec8a85bb04fd322a3ee5092d',
  160. 'duration': 1297,
  161. 'timestamp': 1254205102,
  162. 'upload_date': '20090929',
  163. },
  164. 'params': {
  165. 'skip_download': True,
  166. },
  167. },
  168. {
  169. 'url': 'http://www.tv6play.no/programmer/hotelinspektor-alex-polizzi/361883?autostart=true',
  170. 'info_dict': {
  171. 'id': '361883',
  172. 'ext': 'mp4',
  173. 'title': 'Hotelinspektør Alex Polizzi - Ep. 10',
  174. 'description': 'md5:3ecf808db9ec96c862c8ecb3a7fdaf81',
  175. 'duration': 2594,
  176. 'timestamp': 1393236292,
  177. 'upload_date': '20140224',
  178. },
  179. 'params': {
  180. 'skip_download': True,
  181. },
  182. },
  183. {
  184. 'url': 'http://play.novatv.bg/programi/zdravei-bulgariya/624952?autostart=true',
  185. 'info_dict': {
  186. 'id': '624952',
  187. 'ext': 'flv',
  188. 'title': 'Здравей, България (12.06.2015 г.) ',
  189. 'description': 'md5:99f3700451ac5bb71a260268b8daefd7',
  190. 'duration': 8838,
  191. 'timestamp': 1434100372,
  192. 'upload_date': '20150612',
  193. },
  194. 'params': {
  195. # rtmp download
  196. 'skip_download': True,
  197. },
  198. },
  199. {
  200. 'url': 'https://play.nova.bg/programi/zdravei-bulgariya/764300?autostart=true',
  201. 'only_matching': True,
  202. },
  203. {
  204. 'url': 'http://tvplay.skaties.lv/parraides/vinas-melo-labak/418113?autostart=true',
  205. 'only_matching': True,
  206. },
  207. {
  208. 'url': 'https://tvplay.skaties.lv/vinas-melo-labak/418113/?autostart=true',
  209. 'only_matching': True,
  210. },
  211. {
  212. # views is null
  213. 'url': 'http://tvplay.skaties.lv/parraides/tv3-zinas/760183',
  214. 'only_matching': True,
  215. },
  216. {
  217. 'url': 'http://tv3play.tv3.ee/sisu/kodu-keset-linna/238551?autostart=true',
  218. 'only_matching': True,
  219. },
  220. {
  221. 'url': 'http://www.viafree.se/program/underhallning/i-like-radio-live/sasong-1/676869',
  222. 'only_matching': True,
  223. },
  224. {
  225. 'url': 'mtg:418113',
  226. 'only_matching': True,
  227. }
  228. ]
  229. def _real_extract(self, url):
  230. video_id = self._match_id(url)
  231. geo_country = self._search_regex(
  232. r'https?://[^/]+\.([a-z]{2})', url,
  233. 'geo country', default=None)
  234. if geo_country:
  235. self._initialize_geo_bypass({'countries': [geo_country.upper()]})
  236. video = self._download_json(
  237. 'http://playapi.mtgx.tv/v3/videos/%s' % video_id, video_id, 'Downloading video JSON')
  238. title = video['title']
  239. try:
  240. streams = self._download_json(
  241. 'http://playapi.mtgx.tv/v3/videos/stream/%s' % video_id,
  242. video_id, 'Downloading streams JSON')
  243. except ExtractorError as e:
  244. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  245. msg = self._parse_json(e.cause.read().decode('utf-8'), video_id)
  246. raise ExtractorError(msg['msg'], expected=True)
  247. raise
  248. quality = qualities(['hls', 'medium', 'high'])
  249. formats = []
  250. for format_id, video_url in streams.get('streams', {}).items():
  251. video_url = url_or_none(video_url)
  252. if not video_url:
  253. continue
  254. ext = determine_ext(video_url)
  255. if ext == 'f4m':
  256. formats.extend(self._extract_f4m_formats(
  257. update_url_query(video_url, {
  258. 'hdcore': '3.5.0',
  259. 'plugin': 'aasp-3.5.0.151.81'
  260. }), video_id, f4m_id='hds', fatal=False))
  261. elif ext == 'm3u8':
  262. formats.extend(self._extract_m3u8_formats(
  263. video_url, video_id, 'mp4', 'm3u8_native',
  264. m3u8_id='hls', fatal=False))
  265. else:
  266. fmt = {
  267. 'format_id': format_id,
  268. 'quality': quality(format_id),
  269. 'ext': ext,
  270. }
  271. if video_url.startswith('rtmp'):
  272. m = re.search(
  273. r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
  274. if not m:
  275. continue
  276. fmt.update({
  277. 'ext': 'flv',
  278. 'url': m.group('url'),
  279. 'app': m.group('app'),
  280. 'play_path': m.group('playpath'),
  281. 'preference': -1,
  282. })
  283. else:
  284. fmt.update({
  285. 'url': video_url,
  286. })
  287. formats.append(fmt)
  288. if not formats and video.get('is_geo_blocked'):
  289. self.raise_geo_restricted(
  290. 'This content might not be available in your country due to copyright reasons')
  291. self._sort_formats(formats)
  292. # TODO: webvtt in m3u8
  293. subtitles = {}
  294. sami_path = video.get('sami_path')
  295. if sami_path:
  296. lang = self._search_regex(
  297. r'_([a-z]{2})\.xml', sami_path, 'lang',
  298. default=compat_urlparse.urlparse(url).netloc.rsplit('.', 1)[-1])
  299. subtitles[lang] = [{
  300. 'url': sami_path,
  301. }]
  302. series = video.get('format_title')
  303. episode_number = int_or_none(video.get('format_position', {}).get('episode'))
  304. season = video.get('_embedded', {}).get('season', {}).get('title')
  305. season_number = int_or_none(video.get('format_position', {}).get('season'))
  306. return {
  307. 'id': video_id,
  308. 'title': title,
  309. 'description': video.get('description'),
  310. 'series': series,
  311. 'episode_number': episode_number,
  312. 'season': season,
  313. 'season_number': season_number,
  314. 'duration': int_or_none(video.get('duration')),
  315. 'timestamp': parse_iso8601(video.get('created_at')),
  316. 'view_count': try_get(video, lambda x: x['views']['total'], int),
  317. 'age_limit': int_or_none(video.get('age_limit', 0)),
  318. 'formats': formats,
  319. 'subtitles': subtitles,
  320. }
  321. class ViafreeIE(InfoExtractor):
  322. _VALID_URL = r'''(?x)
  323. https?://
  324. (?:www\.)?
  325. viafree\.(?P<country>dk|no|se)
  326. /(?P<id>program(?:mer)?/(?:[^/]+/)+[^/?#&]+)
  327. '''
  328. _TESTS = [{
  329. 'url': 'http://www.viafree.no/programmer/underholdning/det-beste-vorspielet/sesong-2/episode-1',
  330. 'info_dict': {
  331. 'id': '757786',
  332. 'ext': 'mp4',
  333. 'title': 'Det beste vorspielet - Sesong 2 - Episode 1',
  334. 'description': 'md5:b632cb848331404ccacd8cd03e83b4c3',
  335. 'series': 'Det beste vorspielet',
  336. 'season_number': 2,
  337. 'duration': 1116,
  338. 'timestamp': 1471200600,
  339. 'upload_date': '20160814',
  340. },
  341. 'params': {
  342. 'skip_download': True,
  343. },
  344. }, {
  345. # with relatedClips
  346. 'url': 'http://www.viafree.se/program/reality/sommaren-med-youtube-stjarnorna/sasong-1/avsnitt-1',
  347. 'only_matching': True,
  348. }, {
  349. # Different og:image URL schema
  350. 'url': 'http://www.viafree.se/program/reality/sommaren-med-youtube-stjarnorna/sasong-1/avsnitt-2',
  351. 'only_matching': True,
  352. }, {
  353. 'url': 'http://www.viafree.se/program/livsstil/husraddarna/sasong-2/avsnitt-2',
  354. 'only_matching': True,
  355. }, {
  356. 'url': 'http://www.viafree.dk/programmer/reality/paradise-hotel/saeson-7/episode-5',
  357. 'only_matching': True,
  358. }]
  359. _GEO_BYPASS = False
  360. @classmethod
  361. def suitable(cls, url):
  362. return False if TVPlayIE.suitable(url) else super(ViafreeIE, cls).suitable(url)
  363. def _real_extract(self, url):
  364. country, path = re.match(self._VALID_URL, url).groups()
  365. content = self._download_json(
  366. 'https://viafree-content.mtg-api.com/viafree-content/v1/%s/path/%s' % (country, path), path)
  367. program = content['_embedded']['viafreeBlocks'][0]['_embedded']['program']
  368. guid = program['guid']
  369. meta = content['meta']
  370. title = meta['title']
  371. try:
  372. stream_href = self._download_json(
  373. program['_links']['streamLink']['href'], guid,
  374. headers=self.geo_verification_headers())['embedded']['prioritizedStreams'][0]['links']['stream']['href']
  375. except ExtractorError as e:
  376. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  377. self.raise_geo_restricted(countries=[country])
  378. raise
  379. formats = self._extract_m3u8_formats(stream_href, guid, 'mp4')
  380. self._sort_formats(formats)
  381. episode = program.get('episode') or {}
  382. return {
  383. 'id': guid,
  384. 'title': title,
  385. 'thumbnail': meta.get('image'),
  386. 'description': meta.get('description'),
  387. 'series': episode.get('seriesTitle'),
  388. 'episode_number': int_or_none(episode.get('episodeNumber')),
  389. 'season_number': int_or_none(episode.get('seasonNumber')),
  390. 'duration': int_or_none(try_get(program, lambda x: x['video']['duration']['milliseconds']), 1000),
  391. 'timestamp': parse_iso8601(try_get(program, lambda x: x['availability']['start'])),
  392. 'formats': formats,
  393. }
  394. class TVPlayHomeIE(InfoExtractor):
  395. _VALID_URL = r'https?://tvplay\.(?:tv3\.lt|skaties\.lv|tv3\.ee)/[^/]+/[^/?#&]+-(?P<id>\d+)'
  396. _TESTS = [{
  397. 'url': 'https://tvplay.tv3.lt/aferistai-n-7/aferistai-10047125/',
  398. 'info_dict': {
  399. 'id': '366367',
  400. 'ext': 'mp4',
  401. 'title': 'Aferistai',
  402. 'description': 'Aferistai. Kalėdinė pasaka.',
  403. 'series': 'Aferistai [N-7]',
  404. 'season': '1 sezonas',
  405. 'season_number': 1,
  406. 'duration': 464,
  407. 'timestamp': 1394209658,
  408. 'upload_date': '20140307',
  409. 'age_limit': 18,
  410. },
  411. 'params': {
  412. 'skip_download': True,
  413. },
  414. 'add_ie': [TVPlayIE.ie_key()],
  415. }, {
  416. 'url': 'https://tvplay.skaties.lv/vinas-melo-labak/vinas-melo-labak-10280317/',
  417. 'only_matching': True,
  418. }, {
  419. 'url': 'https://tvplay.tv3.ee/cool-d-ga-mehhikosse/cool-d-ga-mehhikosse-10044354/',
  420. 'only_matching': True,
  421. }]
  422. def _real_extract(self, url):
  423. video_id = self._match_id(url)
  424. webpage = self._download_webpage(url, video_id)
  425. video_id = self._search_regex(
  426. r'data-asset-id\s*=\s*["\'](\d{5,})\b', webpage, 'video id')
  427. if len(video_id) < 8:
  428. return self.url_result(
  429. 'mtg:%s' % video_id, ie=TVPlayIE.ie_key(), video_id=video_id)
  430. m3u8_url = self._search_regex(
  431. r'data-file\s*=\s*(["\'])(?P<url>(?:(?!\1).)+)\1', webpage,
  432. 'm3u8 url', group='url')
  433. formats = self._extract_m3u8_formats(
  434. m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
  435. m3u8_id='hls')
  436. self._sort_formats(formats)
  437. title = self._search_regex(
  438. r'data-title\s*=\s*(["\'])(?P<value>(?:(?!\1).)+)\1', webpage,
  439. 'title', default=None, group='value') or self._html_search_meta(
  440. 'title', webpage, default=None) or self._og_search_title(
  441. webpage)
  442. description = self._html_search_meta(
  443. 'description', webpage,
  444. default=None) or self._og_search_description(webpage)
  445. thumbnail = self._search_regex(
  446. r'data-image\s*=\s*(["\'])(?P<url>(?:(?!\1).)+)\1', webpage,
  447. 'thumbnail', default=None, group='url') or self._html_search_meta(
  448. 'thumbnail', webpage, default=None) or self._og_search_thumbnail(
  449. webpage)
  450. duration = int_or_none(self._search_regex(
  451. r'data-duration\s*=\s*["\'](\d+)', webpage, 'duration',
  452. fatal=False))
  453. season = self._search_regex(
  454. (r'data-series-title\s*=\s*(["\'])[^/]+/(?P<value>(?:(?!\1).)+)\1',
  455. r'\bseason\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1'), webpage,
  456. 'season', default=None, group='value')
  457. season_number = int_or_none(self._search_regex(
  458. r'(\d+)(?:[.\s]+sezona|\s+HOOAEG)', season or '', 'season number',
  459. default=None))
  460. episode = self._search_regex(
  461. (r'\bepisode\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1',
  462. r'data-subtitle\s*=\s*(["\'])(?P<value>(?:(?!\1).)+)\1'), webpage,
  463. 'episode', default=None, group='value')
  464. episode_number = int_or_none(self._search_regex(
  465. r'(?:S[eē]rija|Osa)\s+(\d+)', episode or '', 'episode number',
  466. default=None))
  467. return {
  468. 'id': video_id,
  469. 'title': title,
  470. 'description': description,
  471. 'thumbnail': thumbnail,
  472. 'duration': duration,
  473. 'season': season,
  474. 'season_number': season_number,
  475. 'episode': episode,
  476. 'episode_number': episode_number,
  477. 'formats': formats,
  478. }