nova.py 12 KB

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