logo

youtube-dl

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

nova.py (11830B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. clean_html,
  7. determine_ext,
  8. int_or_none,
  9. js_to_json,
  10. qualities,
  11. unified_strdate,
  12. url_or_none,
  13. )
  14. class NovaEmbedIE(InfoExtractor):
  15. _VALID_URL = r'https?://media\.cms\.nova\.cz/embed/(?P<id>[^/?#&]+)'
  16. _TEST = {
  17. 'url': 'https://media.cms.nova.cz/embed/8o0n0r?autoplay=1',
  18. 'md5': 'ee009bafcc794541570edd44b71cbea3',
  19. 'info_dict': {
  20. 'id': '8o0n0r',
  21. 'ext': 'mp4',
  22. 'title': '2180. díl',
  23. 'thumbnail': r're:^https?://.*\.jpg',
  24. 'duration': 2578,
  25. },
  26. }
  27. def _real_extract(self, url):
  28. video_id = self._match_id(url)
  29. webpage = self._download_webpage(url, video_id)
  30. duration = None
  31. formats = []
  32. player = self._parse_json(
  33. self._search_regex(
  34. r'Player\.init\s*\([^,]+,\s*({.+?})\s*,\s*{.+?}\s*\)\s*;',
  35. webpage, 'player', default='{}'), video_id, fatal=False)
  36. if player:
  37. for format_id, format_list in player['tracks'].items():
  38. if not isinstance(format_list, list):
  39. format_list = [format_list]
  40. for format_dict in format_list:
  41. if not isinstance(format_dict, dict):
  42. continue
  43. format_url = url_or_none(format_dict.get('src'))
  44. format_type = format_dict.get('type')
  45. ext = determine_ext(format_url)
  46. if (format_type == 'application/x-mpegURL'
  47. or format_id == 'HLS' or ext == 'm3u8'):
  48. formats.extend(self._extract_m3u8_formats(
  49. format_url, video_id, 'mp4',
  50. entry_protocol='m3u8_native', m3u8_id='hls',
  51. fatal=False))
  52. elif (format_type == 'application/dash+xml'
  53. or format_id == 'DASH' or ext == 'mpd'):
  54. formats.extend(self._extract_mpd_formats(
  55. format_url, video_id, mpd_id='dash', fatal=False))
  56. else:
  57. formats.append({
  58. 'url': format_url,
  59. })
  60. duration = int_or_none(player.get('duration'))
  61. else:
  62. # Old path, not actual as of 08.04.2020
  63. bitrates = self._parse_json(
  64. self._search_regex(
  65. r'(?s)(?:src|bitrates)\s*=\s*({.+?})\s*;', webpage, 'formats'),
  66. video_id, transform_source=js_to_json)
  67. QUALITIES = ('lq', 'mq', 'hq', 'hd')
  68. quality_key = qualities(QUALITIES)
  69. for format_id, format_list in bitrates.items():
  70. if not isinstance(format_list, list):
  71. format_list = [format_list]
  72. for format_url in format_list:
  73. format_url = url_or_none(format_url)
  74. if not format_url:
  75. continue
  76. if format_id == 'hls':
  77. formats.extend(self._extract_m3u8_formats(
  78. format_url, video_id, ext='mp4',
  79. entry_protocol='m3u8_native', m3u8_id='hls',
  80. fatal=False))
  81. continue
  82. f = {
  83. 'url': format_url,
  84. }
  85. f_id = format_id
  86. for quality in QUALITIES:
  87. if '%s.mp4' % quality in format_url:
  88. f_id += '-%s' % quality
  89. f.update({
  90. 'quality': quality_key(quality),
  91. 'format_note': quality.upper(),
  92. })
  93. break
  94. f['format_id'] = f_id
  95. formats.append(f)
  96. self._sort_formats(formats)
  97. title = self._og_search_title(
  98. webpage, default=None) or self._search_regex(
  99. (r'<value>(?P<title>[^<]+)',
  100. r'videoTitle\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1'), webpage,
  101. 'title', group='value')
  102. thumbnail = self._og_search_thumbnail(
  103. webpage, default=None) or self._search_regex(
  104. r'poster\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1', webpage,
  105. 'thumbnail', fatal=False, group='value')
  106. duration = int_or_none(self._search_regex(
  107. r'videoDuration\s*:\s*(\d+)', webpage, 'duration',
  108. default=duration))
  109. return {
  110. 'id': video_id,
  111. 'title': title,
  112. 'thumbnail': thumbnail,
  113. 'duration': duration,
  114. 'formats': formats,
  115. }
  116. class NovaIE(InfoExtractor):
  117. IE_DESC = 'TN.cz, Prásk.tv, Nova.cz, Novaplus.cz, FANDA.tv, Krásná.cz and Doma.cz'
  118. _VALID_URL = r'https?://(?:[^.]+\.)?(?P<site>tv(?:noviny)?|tn|novaplus|vymena|fanda|krasna|doma|prask)\.nova\.cz/(?:[^/]+/)+(?P<id>[^/]+?)(?:\.html|/|$)'
  119. _TESTS = [{
  120. 'url': 'http://tn.nova.cz/clanek/tajemstvi-ukryte-v-podzemi-specialni-nemocnice-v-prazske-krci.html#player_13260',
  121. 'md5': '249baab7d0104e186e78b0899c7d5f28',
  122. 'info_dict': {
  123. 'id': '1757139',
  124. 'display_id': 'tajemstvi-ukryte-v-podzemi-specialni-nemocnice-v-prazske-krci',
  125. 'ext': 'mp4',
  126. 'title': 'Podzemní nemocnice v pražské Krči',
  127. 'description': 'md5:f0a42dd239c26f61c28f19e62d20ef53',
  128. 'thumbnail': r're:^https?://.*\.(?:jpg)',
  129. }
  130. }, {
  131. 'url': 'http://fanda.nova.cz/clanek/fun-and-games/krvavy-epos-zaklinac-3-divoky-hon-vychazi-vyhrajte-ho-pro-sebe.html',
  132. 'info_dict': {
  133. 'id': '1753621',
  134. 'ext': 'mp4',
  135. 'title': 'Zaklínač 3: Divoký hon',
  136. 'description': 're:.*Pokud se stejně jako my nemůžete.*',
  137. 'thumbnail': r're:https?://.*\.jpg(\?.*)?',
  138. 'upload_date': '20150521',
  139. },
  140. 'params': {
  141. # rtmp download
  142. 'skip_download': True,
  143. },
  144. 'skip': 'gone',
  145. }, {
  146. # media.cms.nova.cz embed
  147. 'url': 'https://novaplus.nova.cz/porad/ulice/epizoda/18760-2180-dil',
  148. 'info_dict': {
  149. 'id': '8o0n0r',
  150. 'ext': 'mp4',
  151. 'title': '2180. díl',
  152. 'thumbnail': r're:^https?://.*\.jpg',
  153. 'duration': 2578,
  154. },
  155. 'params': {
  156. 'skip_download': True,
  157. },
  158. 'add_ie': [NovaEmbedIE.ie_key()],
  159. 'skip': 'CHYBA 404: STRÁNKA NENALEZENA',
  160. }, {
  161. 'url': 'http://sport.tn.nova.cz/clanek/sport/hokej/nhl/zivot-jde-dal-hodnotil-po-vyrazeni-z-playoff-jiri-sekac.html',
  162. 'only_matching': True,
  163. }, {
  164. 'url': 'http://fanda.nova.cz/clanek/fun-and-games/krvavy-epos-zaklinac-3-divoky-hon-vychazi-vyhrajte-ho-pro-sebe.html',
  165. 'only_matching': True,
  166. }, {
  167. 'url': 'http://doma.nova.cz/clanek/zdravi/prijdte-se-zapsat-do-registru-kostni-drene-jiz-ve-stredu-3-cervna.html',
  168. 'only_matching': True,
  169. }, {
  170. 'url': 'http://prask.nova.cz/clanek/novinky/co-si-na-sobe-nase-hvezdy-nechaly-pojistit.html',
  171. 'only_matching': True,
  172. }, {
  173. 'url': 'http://tv.nova.cz/clanek/novinky/zivot-je-zivot-bondovsky-trailer.html',
  174. 'only_matching': True,
  175. }]
  176. def _real_extract(self, url):
  177. mobj = re.match(self._VALID_URL, url)
  178. display_id = mobj.group('id')
  179. site = mobj.group('site')
  180. webpage = self._download_webpage(url, display_id)
  181. description = clean_html(self._og_search_description(webpage, default=None))
  182. if site == 'novaplus':
  183. upload_date = unified_strdate(self._search_regex(
  184. r'(\d{1,2}-\d{1,2}-\d{4})$', display_id, 'upload date', default=None))
  185. elif site == 'fanda':
  186. upload_date = unified_strdate(self._search_regex(
  187. r'<span class="date_time">(\d{1,2}\.\d{1,2}\.\d{4})', webpage, 'upload date', default=None))
  188. else:
  189. upload_date = None
  190. # novaplus
  191. embed_id = self._search_regex(
  192. r'<iframe[^>]+\bsrc=["\'](?:https?:)?//media\.cms\.nova\.cz/embed/([^/?#&]+)',
  193. webpage, 'embed url', default=None)
  194. if embed_id:
  195. return {
  196. '_type': 'url_transparent',
  197. 'url': 'https://media.cms.nova.cz/embed/%s' % embed_id,
  198. 'ie_key': NovaEmbedIE.ie_key(),
  199. 'id': embed_id,
  200. 'description': description,
  201. 'upload_date': upload_date
  202. }
  203. video_id = self._search_regex(
  204. [r"(?:media|video_id)\s*:\s*'(\d+)'",
  205. r'media=(\d+)',
  206. r'id="article_video_(\d+)"',
  207. r'id="player_(\d+)"'],
  208. webpage, 'video id')
  209. config_url = self._search_regex(
  210. r'src="(https?://(?:tn|api)\.nova\.cz/bin/player/videojs/config\.php\?[^"]+)"',
  211. webpage, 'config url', default=None)
  212. config_params = {}
  213. if not config_url:
  214. player = self._parse_json(
  215. self._search_regex(
  216. r'(?s)Player\s*\(.+?\s*,\s*({.+?\bmedia\b["\']?\s*:\s*["\']?\d+.+?})\s*\)', webpage,
  217. 'player', default='{}'),
  218. video_id, transform_source=js_to_json, fatal=False)
  219. if player:
  220. config_url = url_or_none(player.get('configUrl'))
  221. params = player.get('configParams')
  222. if isinstance(params, dict):
  223. config_params = params
  224. if not config_url:
  225. DEFAULT_SITE_ID = '23000'
  226. SITES = {
  227. 'tvnoviny': DEFAULT_SITE_ID,
  228. 'novaplus': DEFAULT_SITE_ID,
  229. 'vymena': DEFAULT_SITE_ID,
  230. 'krasna': DEFAULT_SITE_ID,
  231. 'fanda': '30',
  232. 'tn': '30',
  233. 'doma': '30',
  234. }
  235. site_id = self._search_regex(
  236. r'site=(\d+)', webpage, 'site id', default=None) or SITES.get(
  237. site, DEFAULT_SITE_ID)
  238. config_url = 'https://api.nova.cz/bin/player/videojs/config.php'
  239. config_params = {
  240. 'site': site_id,
  241. 'media': video_id,
  242. 'quality': 3,
  243. 'version': 1,
  244. }
  245. config = self._download_json(
  246. config_url, display_id,
  247. 'Downloading config JSON', query=config_params,
  248. transform_source=lambda s: s[s.index('{'):s.rindex('}') + 1])
  249. mediafile = config['mediafile']
  250. video_url = mediafile['src']
  251. m = re.search(r'^(?P<url>rtmpe?://[^/]+/(?P<app>[^/]+?))/&*(?P<playpath>.+)$', video_url)
  252. if m:
  253. formats = [{
  254. 'url': m.group('url'),
  255. 'app': m.group('app'),
  256. 'play_path': m.group('playpath'),
  257. 'player_path': 'http://tvnoviny.nova.cz/static/shared/app/videojs/video-js.swf',
  258. 'ext': 'flv',
  259. }]
  260. else:
  261. formats = [{
  262. 'url': video_url,
  263. }]
  264. self._sort_formats(formats)
  265. title = mediafile.get('meta', {}).get('title') or self._og_search_title(webpage)
  266. thumbnail = config.get('poster')
  267. return {
  268. 'id': video_id,
  269. 'display_id': display_id,
  270. 'title': title,
  271. 'description': description,
  272. 'upload_date': upload_date,
  273. 'thumbnail': thumbnail,
  274. 'formats': formats,
  275. }