logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

tvplay.py (18176B)


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