You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

557 lines
20 KiB

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