nova.py 12 KB

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