tvplay.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. import re
  2. from .common import InfoExtractor
  3. from ..compat import (
  4. compat_HTTPError,
  5. compat_urlparse,
  6. )
  7. from ..utils import (
  8. determine_ext,
  9. ExtractorError,
  10. int_or_none,
  11. parse_iso8601,
  12. qualities,
  13. traverse_obj,
  14. try_get,
  15. update_url_query,
  16. url_or_none,
  17. urljoin,
  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)\.se/program|
  32. (?:(?:tv3play|viasat4play|tv6play)\.no|(?:tv3play)\.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': 'mtg:418113',
  222. 'only_matching': True,
  223. }
  224. ]
  225. def _real_extract(self, url):
  226. video_id = self._match_id(url)
  227. geo_country = self._search_regex(
  228. r'https?://[^/]+\.([a-z]{2})', url,
  229. 'geo country', default=None)
  230. if geo_country:
  231. self._initialize_geo_bypass({'countries': [geo_country.upper()]})
  232. video = self._download_json(
  233. 'http://playapi.mtgx.tv/v3/videos/%s' % video_id, video_id, 'Downloading video JSON')
  234. title = video['title']
  235. try:
  236. streams = self._download_json(
  237. 'http://playapi.mtgx.tv/v3/videos/stream/%s' % video_id,
  238. video_id, 'Downloading streams JSON')
  239. except ExtractorError as e:
  240. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  241. msg = self._parse_json(e.cause.read().decode('utf-8'), video_id)
  242. raise ExtractorError(msg['msg'], expected=True)
  243. raise
  244. quality = qualities(['hls', 'medium', 'high'])
  245. formats = []
  246. for format_id, video_url in streams.get('streams', {}).items():
  247. video_url = url_or_none(video_url)
  248. if not video_url:
  249. continue
  250. ext = determine_ext(video_url)
  251. if ext == 'f4m':
  252. formats.extend(self._extract_f4m_formats(
  253. update_url_query(video_url, {
  254. 'hdcore': '3.5.0',
  255. 'plugin': 'aasp-3.5.0.151.81'
  256. }), video_id, f4m_id='hds', fatal=False))
  257. elif ext == 'm3u8':
  258. formats.extend(self._extract_m3u8_formats(
  259. video_url, video_id, 'mp4', 'm3u8_native',
  260. m3u8_id='hls', fatal=False))
  261. else:
  262. fmt = {
  263. 'format_id': format_id,
  264. 'quality': quality(format_id),
  265. 'ext': ext,
  266. }
  267. if video_url.startswith('rtmp'):
  268. m = re.search(
  269. r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
  270. if not m:
  271. continue
  272. fmt.update({
  273. 'ext': 'flv',
  274. 'url': m.group('url'),
  275. 'app': m.group('app'),
  276. 'play_path': m.group('playpath'),
  277. 'preference': -1,
  278. })
  279. else:
  280. fmt.update({
  281. 'url': video_url,
  282. })
  283. formats.append(fmt)
  284. if not formats and video.get('is_geo_blocked'):
  285. self.raise_geo_restricted(
  286. 'This content might not be available in your country due to copyright reasons',
  287. metadata_available=True)
  288. # TODO: webvtt in m3u8
  289. subtitles = {}
  290. sami_path = video.get('sami_path')
  291. if sami_path:
  292. lang = self._search_regex(
  293. r'_([a-z]{2})\.xml', sami_path, 'lang',
  294. default=compat_urlparse.urlparse(url).netloc.rsplit('.', 1)[-1])
  295. subtitles[lang] = [{
  296. 'url': sami_path,
  297. }]
  298. series = video.get('format_title')
  299. episode_number = int_or_none(video.get('format_position', {}).get('episode'))
  300. season = video.get('_embedded', {}).get('season', {}).get('title')
  301. season_number = int_or_none(video.get('format_position', {}).get('season'))
  302. return {
  303. 'id': video_id,
  304. 'title': title,
  305. 'description': video.get('description'),
  306. 'series': series,
  307. 'episode_number': episode_number,
  308. 'season': season,
  309. 'season_number': season_number,
  310. 'duration': int_or_none(video.get('duration')),
  311. 'timestamp': parse_iso8601(video.get('created_at')),
  312. 'view_count': try_get(video, lambda x: x['views']['total'], int),
  313. 'age_limit': int_or_none(video.get('age_limit', 0)),
  314. 'formats': formats,
  315. 'subtitles': subtitles,
  316. }
  317. class ViafreeIE(InfoExtractor):
  318. _VALID_URL = r'''(?x)
  319. https?://
  320. (?:www\.)?
  321. viafree\.(?P<country>dk|no|se|fi)
  322. /(?P<id>(?:program(?:mer)?|ohjelmat)?/(?:[^/]+/)+[^/?#&]+)
  323. '''
  324. _TESTS = [{
  325. 'url': 'http://www.viafree.no/programmer/underholdning/det-beste-vorspielet/sesong-2/episode-1',
  326. 'info_dict': {
  327. 'id': '757786',
  328. 'ext': 'mp4',
  329. 'title': 'Det beste vorspielet - Sesong 2 - Episode 1',
  330. 'description': 'md5:b632cb848331404ccacd8cd03e83b4c3',
  331. 'series': 'Det beste vorspielet',
  332. 'season_number': 2,
  333. 'duration': 1116,
  334. 'timestamp': 1471200600,
  335. 'upload_date': '20160814',
  336. },
  337. 'params': {
  338. 'skip_download': True,
  339. },
  340. }, {
  341. 'url': 'https://www.viafree.dk/programmer/humor/comedy-central-roast-of-charlie-sheen/film/1047660',
  342. 'info_dict': {
  343. 'id': '1047660',
  344. 'ext': 'mp4',
  345. 'title': 'Comedy Central Roast of Charlie Sheen - Comedy Central Roast of Charlie Sheen',
  346. 'description': 'md5:ec956d941ae9fd7c65a48fd64951dc6d',
  347. 'series': 'Comedy Central Roast of Charlie Sheen',
  348. 'season_number': 1,
  349. 'duration': 3747,
  350. 'timestamp': 1608246060,
  351. 'upload_date': '20201217'
  352. },
  353. 'params': {
  354. 'skip_download': True
  355. }
  356. }, {
  357. # with relatedClips
  358. 'url': 'http://www.viafree.se/program/reality/sommaren-med-youtube-stjarnorna/sasong-1/avsnitt-1',
  359. 'only_matching': True,
  360. }, {
  361. # Different og:image URL schema
  362. 'url': 'http://www.viafree.se/program/reality/sommaren-med-youtube-stjarnorna/sasong-1/avsnitt-2',
  363. 'only_matching': True,
  364. }, {
  365. 'url': 'http://www.viafree.se/program/livsstil/husraddarna/sasong-2/avsnitt-2',
  366. 'only_matching': True,
  367. }, {
  368. 'url': 'http://www.viafree.dk/programmer/reality/paradise-hotel/saeson-7/episode-5',
  369. 'only_matching': True,
  370. }, {
  371. 'url': 'http://www.viafree.se/program/underhallning/i-like-radio-live/sasong-1/676869',
  372. 'only_matching': True,
  373. }, {
  374. 'url': 'https://www.viafree.fi/ohjelmat/entertainment/amazing-makeovers/kausi-7/jakso-2',
  375. 'only_matching': True,
  376. }]
  377. _GEO_BYPASS = False
  378. def _real_extract(self, url):
  379. country, path = self._match_valid_url(url).groups()
  380. content = self._download_json(
  381. 'https://viafree-content.mtg-api.com/viafree-content/v1/%s/path/%s' % (country, path), path)
  382. program = content['_embedded']['viafreeBlocks'][0]['_embedded']['program']
  383. guid = program['guid']
  384. meta = content['meta']
  385. title = meta['title']
  386. try:
  387. stream_href = self._download_json(
  388. program['_links']['streamLink']['href'], guid,
  389. headers=self.geo_verification_headers())['embedded']['prioritizedStreams'][0]['links']['stream']['href']
  390. except ExtractorError as e:
  391. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  392. self.raise_geo_restricted(countries=[country])
  393. raise
  394. formats, subtitles = self._extract_m3u8_formats_and_subtitles(stream_href, guid, 'mp4')
  395. episode = program.get('episode') or {}
  396. return {
  397. 'id': guid,
  398. 'title': title,
  399. 'thumbnail': meta.get('image'),
  400. 'description': meta.get('description'),
  401. 'series': episode.get('seriesTitle'),
  402. 'subtitles': subtitles,
  403. 'episode_number': int_or_none(episode.get('episodeNumber')),
  404. 'season_number': int_or_none(episode.get('seasonNumber')),
  405. 'duration': int_or_none(try_get(program, lambda x: x['video']['duration']['milliseconds']), 1000),
  406. 'timestamp': parse_iso8601(try_get(program, lambda x: x['availability']['start'])),
  407. 'formats': formats,
  408. }
  409. class TVPlayHomeIE(InfoExtractor):
  410. _VALID_URL = r'''(?x)
  411. https?://
  412. (?:tv3?)?
  413. play\.(?:tv3|skaties)\.(?P<country>lv|lt|ee)/
  414. (?P<live>lives/)?
  415. [^?#&]+(?:episode|programme|clip)-(?P<id>\d+)
  416. '''
  417. _TESTS = [{
  418. 'url': 'https://play.tv3.lt/series/gauju-karai-karveliai,serial-2343791/serija-8,episode-2343828',
  419. 'info_dict': {
  420. 'id': '2343828',
  421. 'ext': 'mp4',
  422. 'title': 'Gaujų karai. Karveliai (2021) | S01E08: Serija 8',
  423. 'description': 'md5:f6fcfbb236429f05531131640dfa7c81',
  424. 'duration': 2710,
  425. 'season': 'Gaujų karai. Karveliai',
  426. 'season_number': 1,
  427. 'release_year': 2021,
  428. 'episode': 'Serija 8',
  429. 'episode_number': 8,
  430. },
  431. 'params': {
  432. 'skip_download': 'm3u8',
  433. },
  434. }, {
  435. 'url': 'https://play.tv3.lt/series/moterys-meluoja-geriau-n-7,serial-2574652/serija-25,episode-3284937',
  436. 'info_dict': {
  437. 'id': '3284937',
  438. 'ext': 'mp4',
  439. 'season': 'Moterys meluoja geriau [N-7]',
  440. 'season_number': 14,
  441. 'release_year': 2021,
  442. 'episode': 'Serija 25',
  443. 'episode_number': 25,
  444. 'title': 'Moterys meluoja geriau [N-7] (2021) | S14|E25: Serija 25',
  445. 'description': 'md5:c6926e9710f1a126f028fbe121eddb79',
  446. 'duration': 2440,
  447. },
  448. 'skip': '404'
  449. }, {
  450. 'url': 'https://play.tv3.lt/lives/tv6-lt,live-2838694/optibet-a-lygos-rungtynes-marijampoles-suduva--vilniaus-riteriai,programme-3422014',
  451. 'only_matching': True,
  452. }, {
  453. 'url': 'https://tv3play.skaties.lv/series/women-lie-better-lv,serial-1024464/women-lie-better-lv,episode-1038762',
  454. 'only_matching': True,
  455. }, {
  456. 'url': 'https://play.tv3.ee/series/_,serial-2654462/_,episode-2654474',
  457. 'only_matching': True,
  458. }, {
  459. 'url': 'https://tv3play.skaties.lv/clips/tv3-zinas-valsti-lidz-15novembrim-bus-majsede,clip-3464509',
  460. 'only_matching': True,
  461. }]
  462. def _real_extract(self, url):
  463. country, is_live, video_id = self._match_valid_url(url).groups()
  464. api_path = 'lives/programmes' if is_live else 'vods'
  465. data = self._download_json(
  466. urljoin(url, f'/api/products/{api_path}/{video_id}?platform=BROWSER&lang={country.upper()}'),
  467. video_id)
  468. video_type = 'CATCHUP' if is_live else 'MOVIE'
  469. stream_id = data['programRecordingId'] if is_live else video_id
  470. stream = self._download_json(
  471. urljoin(url, f'/api/products/{stream_id}/videos/playlist?videoType={video_type}&platform=BROWSER'), video_id)
  472. formats, subtitles = self._extract_m3u8_formats_and_subtitles(
  473. stream['sources']['HLS'][0]['src'], video_id, 'mp4', 'm3u8_native', m3u8_id='hls')
  474. thumbnails = set(traverse_obj(
  475. data, (('galary', 'images', 'artworks'), ..., ..., ('miniUrl', 'mainUrl')), expected_type=url_or_none))
  476. return {
  477. 'id': video_id,
  478. 'title': self._resolve_title(data),
  479. 'description': traverse_obj(data, 'description', 'lead'),
  480. 'duration': int_or_none(data.get('duration')),
  481. 'season': traverse_obj(data, ('season', 'serial', 'title')),
  482. 'season_number': int_or_none(traverse_obj(data, ('season', 'number'))),
  483. 'episode': data.get('title'),
  484. 'episode_number': int_or_none(data.get('episode')),
  485. 'release_year': int_or_none(traverse_obj(data, ('season', 'serial', 'year'))),
  486. 'thumbnails': [{'url': url, 'ext': 'jpg'} for url in thumbnails],
  487. 'formats': formats,
  488. 'subtitles': subtitles,
  489. }
  490. @staticmethod
  491. def _resolve_title(data):
  492. return try_get(data, lambda x: (
  493. f'{data["season"]["serial"]["title"]} ({data["season"]["serial"]["year"]}) | '
  494. f'S{data["season"]["number"]:02d}E{data["episode"]:02d}: {data["title"]}'
  495. )) or data.get('title')