rcs.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import re
  2. from .common import InfoExtractor
  3. from ..networking import HEADRequest
  4. from ..utils import (
  5. ExtractorError,
  6. base_url,
  7. clean_html,
  8. extract_attributes,
  9. get_element_html_by_class,
  10. get_element_html_by_id,
  11. int_or_none,
  12. js_to_json,
  13. mimetype2ext,
  14. sanitize_url,
  15. traverse_obj,
  16. try_call,
  17. url_basename,
  18. urljoin,
  19. )
  20. class RCSBaseIE(InfoExtractor):
  21. # based on VideoPlayerLoader.prototype.getVideoSrc
  22. # and VideoPlayerLoader.prototype.transformSrc from
  23. # https://js2.corriereobjects.it/includes2013/LIBS/js/corriere_video.sjs
  24. _UUID_RE = r'[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}'
  25. _RCS_ID_RE = r'[\w-]+-\d{10}'
  26. _MIGRATION_MAP = {
  27. 'videoamica-vh.akamaihd': 'amica',
  28. 'media2-amica-it.akamaized': 'amica',
  29. 'corrierevam-vh.akamaihd': 'corriere',
  30. 'media2vam-corriere-it.akamaized': 'corriere',
  31. 'cormezzogiorno-vh.akamaihd': 'corrieredelmezzogiorno',
  32. 'media2vam-mezzogiorno-corriere-it.akamaized': 'corrieredelmezzogiorno',
  33. 'corveneto-vh.akamaihd': 'corrieredelveneto',
  34. 'media2vam-veneto-corriere-it.akamaized': 'corrieredelveneto',
  35. 'corbologna-vh.akamaihd': 'corrieredibologna',
  36. 'media2vam-bologna-corriere-it.akamaized': 'corrieredibologna',
  37. 'corfiorentino-vh.akamaihd': 'corrierefiorentino',
  38. 'media2vam-fiorentino-corriere-it.akamaized': 'corrierefiorentino',
  39. 'corinnovazione-vh.akamaihd': 'corriereinnovazione',
  40. 'media2-gazzanet-gazzetta-it.akamaized': 'gazzanet',
  41. 'videogazzanet-vh.akamaihd': 'gazzanet',
  42. 'videogazzaworld-vh.akamaihd': 'gazzaworld',
  43. 'gazzettavam-vh.akamaihd': 'gazzetta',
  44. 'media2vam-gazzetta-it.akamaized': 'gazzetta',
  45. 'videoiodonna-vh.akamaihd': 'iodonna',
  46. 'media2-leitv-it.akamaized': 'leitv',
  47. 'videoleitv-vh.akamaihd': 'leitv',
  48. 'videoliving-vh.akamaihd': 'living',
  49. 'media2-living-corriere-it.akamaized': 'living',
  50. 'media2-oggi-it.akamaized': 'oggi',
  51. 'videooggi-vh.akamaihd': 'oggi',
  52. 'media2-quimamme-it.akamaized': 'quimamme',
  53. 'quimamme-vh.akamaihd': 'quimamme',
  54. 'videorunning-vh.akamaihd': 'running',
  55. 'media2-style-corriere-it.akamaized': 'style',
  56. 'style-vh.akamaihd': 'style',
  57. 'videostyle-vh.akamaihd': 'style',
  58. 'media2-stylepiccoli-it.akamaized': 'stylepiccoli',
  59. 'stylepiccoli-vh.akamaihd': 'stylepiccoli',
  60. 'doveviaggi-vh.akamaihd': 'viaggi',
  61. 'media2-doveviaggi-it.akamaized': 'viaggi',
  62. 'media2-vivimilano-corriere-it.akamaized': 'vivimilano',
  63. 'vivimilano-vh.akamaihd': 'vivimilano',
  64. 'media2-youreporter-it.akamaized': 'youreporter',
  65. }
  66. def _get_video_src(self, video):
  67. for source in traverse_obj(video, (
  68. 'mediaProfile', 'mediaFile', lambda _, v: v.get('mimeType'))):
  69. url = source['value']
  70. for s, r in (
  71. ('media2vam.corriere.it.edgesuite.net', 'media2vam-corriere-it.akamaized.net'),
  72. ('media.youreporter.it.edgesuite.net', 'media-youreporter-it.akamaized.net'),
  73. ('corrierepmd.corriere.it.edgesuite.net', 'corrierepmd-corriere-it.akamaized.net'),
  74. ('media2vam-corriere-it.akamaized.net/fcs.quotidiani/vr/videos/', 'video.corriere.it/vr360/videos/'),
  75. ('http://', 'https://'),
  76. ):
  77. url = url.replace(s, r)
  78. type_ = mimetype2ext(source['mimeType'])
  79. if type_ == 'm3u8' and '-vh.akamaihd' in url:
  80. # still needed for some old content: see _TESTS #3
  81. matches = re.search(r'(?:https?:)?//(?P<host>[\w\.\-]+)\.net/i(?P<path>.+)$', url)
  82. if matches:
  83. url = f'https://vod.rcsobjects.it/hls/{self._MIGRATION_MAP[matches.group("host")]}{matches.group("path")}'
  84. if traverse_obj(video, ('mediaProfile', 'geoblocking')) or (
  85. type_ == 'm3u8' and 'fcs.quotidiani_!' in url):
  86. url = url.replace('vod.rcsobjects', 'vod-it.rcsobjects')
  87. if type_ == 'm3u8' and 'vod' in url:
  88. url = url.replace('.csmil', '.urlset')
  89. if type_ == 'mp3':
  90. url = url.replace('media2vam-corriere-it.akamaized.net', 'vod.rcsobjects.it/corriere')
  91. yield {
  92. 'type': type_,
  93. 'url': url,
  94. 'bitrate': source.get('bitrate'),
  95. }
  96. def _create_http_formats(self, m3u8_formats, video_id):
  97. for f in m3u8_formats:
  98. if f['vcodec'] == 'none':
  99. continue
  100. http_url = re.sub(r'(https?://[^/]+)/hls/([^?#]+?\.mp4).+', r'\g<1>/\g<2>', f['url'])
  101. if http_url == f['url']:
  102. continue
  103. http_f = f.copy()
  104. del http_f['manifest_url']
  105. format_id = try_call(lambda: http_f['format_id'].replace('hls-', 'https-'))
  106. urlh = self._request_webpage(HEADRequest(http_url), video_id, fatal=False,
  107. note=f'Check filesize for {format_id}')
  108. if not urlh:
  109. continue
  110. http_f.update({
  111. 'format_id': format_id,
  112. 'url': http_url,
  113. 'protocol': 'https',
  114. 'filesize_approx': int_or_none(urlh.headers.get('Content-Length', None)),
  115. })
  116. yield http_f
  117. def _create_formats(self, sources, video_id):
  118. for source in sources:
  119. if source['type'] == 'm3u8':
  120. m3u8_formats = self._extract_m3u8_formats(
  121. source['url'], video_id, 'mp4', m3u8_id='hls', fatal=False)
  122. yield from m3u8_formats
  123. yield from self._create_http_formats(m3u8_formats, video_id)
  124. elif source['type'] == 'mp3':
  125. yield {
  126. 'format_id': 'https-mp3',
  127. 'ext': 'mp3',
  128. 'acodec': 'mp3',
  129. 'vcodec': 'none',
  130. 'abr': source.get('bitrate'),
  131. 'url': source['url'],
  132. }
  133. def _real_extract(self, url):
  134. cdn, video_id = self._match_valid_url(url).group('cdn', 'id')
  135. display_id, video_data = None, None
  136. if re.match(self._UUID_RE, video_id) or re.match(self._RCS_ID_RE, video_id):
  137. url = f'https://video.{cdn}/video-json/{video_id}'
  138. else:
  139. webpage = self._download_webpage(url, video_id)
  140. data_config = get_element_html_by_id('divVideoPlayer', webpage) or get_element_html_by_class('divVideoPlayer', webpage)
  141. if data_config:
  142. data_config = self._parse_json(
  143. extract_attributes(data_config).get('data-config'),
  144. video_id, fatal=False) or {}
  145. if data_config.get('newspaper'):
  146. cdn = f'{data_config["newspaper"]}.it'
  147. display_id, video_id = video_id, data_config.get('uuid') or video_id
  148. url = f'https://video.{cdn}/video-json/{video_id}'
  149. else:
  150. json_url = self._search_regex(
  151. r'''(?x)url\s*=\s*(["'])
  152. (?P<url>
  153. (?:https?:)?//video\.rcs\.it
  154. /fragment-includes/video-includes/[^"']+?\.json
  155. )\1;''',
  156. webpage, video_id, group='url', default=None)
  157. if json_url:
  158. video_data = self._download_json(sanitize_url(json_url, scheme='https'), video_id)
  159. display_id, video_id = video_id, video_data.get('id') or video_id
  160. if not video_data:
  161. webpage = self._download_webpage(url, video_id)
  162. video_data = self._search_json(
  163. '##start-video##', webpage, 'video data', video_id, default=None,
  164. end_pattern='##end-video##', transform_source=js_to_json)
  165. if not video_data:
  166. # try search for iframes
  167. emb = RCSEmbedsIE._extract_url(webpage)
  168. if emb:
  169. return {
  170. '_type': 'url_transparent',
  171. 'url': emb,
  172. 'ie_key': RCSEmbedsIE.ie_key(),
  173. }
  174. if not video_data:
  175. raise ExtractorError('Video data not found in the page')
  176. return {
  177. 'id': video_id,
  178. 'display_id': display_id,
  179. 'title': video_data.get('title'),
  180. 'description': (clean_html(video_data.get('description'))
  181. or clean_html(video_data.get('htmlDescription'))
  182. or self._html_search_meta('description', webpage)),
  183. 'uploader': video_data.get('provider') or cdn,
  184. 'formats': list(self._create_formats(self._get_video_src(video_data), video_id)),
  185. }
  186. class RCSEmbedsIE(RCSBaseIE):
  187. _VALID_URL = r'''(?x)
  188. https?://(?P<vid>video)\.
  189. (?P<cdn>
  190. (?:
  191. rcs|
  192. (?:corriere\w+\.)?corriere|
  193. (?:gazzanet\.)?gazzetta
  194. )\.it)
  195. /video-embed/(?P<id>[^/=&\?]+?)(?:$|\?)'''
  196. _EMBED_REGEX = [r'''(?x)
  197. (?:
  198. data-frame-src=|
  199. <iframe[^\n]+src=
  200. )
  201. (["'])
  202. (?P<url>(?:https?:)?//video\.
  203. (?:
  204. rcs|
  205. (?:corriere\w+\.)?corriere|
  206. (?:gazzanet\.)?gazzetta
  207. )
  208. \.it/video-embed/.+?)
  209. \1''']
  210. _TESTS = [{
  211. 'url': 'https://video.rcs.it/video-embed/iodonna-0001585037',
  212. 'md5': '0faca97df525032bb9847f690bc3720c',
  213. 'info_dict': {
  214. 'id': 'iodonna-0001585037',
  215. 'ext': 'mp4',
  216. 'title': 'Sky Arte racconta Madonna nella serie "Artist to icon"',
  217. 'description': 'md5:65b09633df9ffee57f48b39e34c9e067',
  218. 'uploader': 'rcs.it',
  219. },
  220. }, {
  221. 'url': 'https://video.gazzanet.gazzetta.it/video-embed/gazzanet-mo05-0000260789',
  222. 'only_matching': True,
  223. }, {
  224. 'url': 'https://video.gazzetta.it/video-embed/49612410-00ca-11eb-bcd8-30d4253e0140',
  225. 'only_matching': True,
  226. }]
  227. _WEBPAGE_TESTS = [{
  228. 'url': 'https://www.iodonna.it/video-iodonna/personaggi-video/monica-bellucci-piu-del-lavoro-oggi-per-me-sono-importanti-lamicizia-e-la-famiglia/',
  229. 'info_dict': {
  230. 'id': 'iodonna-0002033648',
  231. 'ext': 'mp4',
  232. 'title': 'Monica Bellucci: «Più del lavoro, oggi per me sono importanti l\'amicizia e la famiglia»',
  233. 'description': 'md5:daea6d9837351e56b1ab615c06bebac1',
  234. 'uploader': 'rcs.it',
  235. },
  236. }]
  237. @staticmethod
  238. def _sanitize_url(url):
  239. url = sanitize_url(url, scheme='https')
  240. return urljoin(base_url(url), url_basename(url))
  241. @classmethod
  242. def _extract_embed_urls(cls, url, webpage):
  243. return map(cls._sanitize_url, super()._extract_embed_urls(url, webpage))
  244. class RCSIE(RCSBaseIE):
  245. _VALID_URL = r'''(?x)https?://(?P<vid>video|viaggi)\.
  246. (?P<cdn>
  247. (?:
  248. corrieredelmezzogiorno\.
  249. |corrieredelveneto\.
  250. |corrieredibologna\.
  251. |corrierefiorentino\.
  252. )?corriere\.it
  253. |(?:gazzanet\.)?gazzetta\.it)
  254. /(?!video-embed/)[^?#]+?/(?P<id>[^/\?]+)(?=\?|/$|$)'''
  255. _TESTS = [{
  256. # json iframe directly from id
  257. 'url': 'https://video.corriere.it/sport/formula-1/vettel-guida-ferrari-sf90-mugello-suo-fianco-c-elecrerc-bendato-video-esilarante/b727632a-f9d0-11ea-91b0-38d50a849abb',
  258. 'md5': '14946840dec46ecfddf66ba4eea7d2b2',
  259. 'info_dict': {
  260. 'id': 'b727632a-f9d0-11ea-91b0-38d50a849abb',
  261. 'ext': 'mp4',
  262. 'title': 'Vettel guida la Ferrari SF90 al Mugello e al suo fianco c\'è Leclerc (bendato): il video è esilarante',
  263. 'description': 'md5:3915ce5ebb3d2571deb69a5eb85ac9b5',
  264. 'uploader': 'Corriere Tv',
  265. },
  266. }, {
  267. # search for video id inside the page
  268. 'url': 'https://viaggi.corriere.it/video/norvegia-il-nuovo-ponte-spettacolare-sopra-la-cascata-di-voringsfossen/',
  269. 'md5': 'f22a92d9e666e80f2fffbf2825359c81',
  270. 'info_dict': {
  271. 'id': '5b7cd134-e2c1-11ea-89b3-b56dd0df2aa2',
  272. 'display_id': 'norvegia-il-nuovo-ponte-spettacolare-sopra-la-cascata-di-voringsfossen',
  273. 'ext': 'mp4',
  274. 'title': 'La nuova spettacolare attrazione in Norvegia: il ponte sopra Vøringsfossen',
  275. 'description': 'md5:18b35a291f6746c0c8dacd16e5f5f4f8',
  276. 'uploader': 'DOVE Viaggi',
  277. },
  278. }, {
  279. # only audio format https://github.com/yt-dlp/yt-dlp/issues/5683
  280. 'url': 'https://video.corriere.it/cronaca/audio-telefonata-il-papa-becciu-santita-lettera-che-mi-ha-inviato-condanna/b94c0d20-70c2-11ed-9572-e4b947a0ebd2',
  281. 'md5': 'aaffb08d02f2ce4292a4654694c78150',
  282. 'info_dict': {
  283. 'id': 'b94c0d20-70c2-11ed-9572-e4b947a0ebd2',
  284. 'ext': 'mp3',
  285. 'title': 'L\'audio della telefonata tra il Papa e Becciu: «Santità, la lettera che mi ha inviato è una condanna»',
  286. 'description': 'md5:c0ddb61bd94a8d4e0d4bb9cda50a689b',
  287. 'uploader': 'Corriere Tv',
  288. 'formats': [{'format_id': 'https-mp3', 'ext': 'mp3'}],
  289. },
  290. }, {
  291. # old content still needs cdn migration
  292. 'url': 'https://viaggi.corriere.it/video/milano-varallo-sesia-sul-treno-a-vapore/',
  293. 'md5': '2dfdce7af249654ad27eeba03fe1e08d',
  294. 'info_dict': {
  295. 'id': 'd8f6c8d0-f7d7-11e8-bfca-f74cf4634191',
  296. 'display_id': 'milano-varallo-sesia-sul-treno-a-vapore',
  297. 'ext': 'mp4',
  298. 'title': 'Milano-Varallo Sesia sul treno a vapore',
  299. 'description': 'md5:6348f47aac230397fe341a74f7678d53',
  300. 'uploader': 'DOVE Viaggi',
  301. },
  302. }, {
  303. 'url': 'https://video.corriere.it/video-360/metro-copenaghen-tutta-italiana/a248a7f0-e2db-11e9-9830-af2de6b1f945',
  304. 'only_matching': True,
  305. }]
  306. class RCSVariousIE(RCSBaseIE):
  307. _VALID_URL = r'''(?x)https?://www\.
  308. (?P<cdn>
  309. leitv\.it|
  310. youreporter\.it|
  311. amica\.it
  312. )/(?:[^/]+/)?(?P<id>[^/]+?)(?:$|\?|/)'''
  313. _TESTS = [{
  314. 'url': 'https://www.leitv.it/benessere/mal-di-testa/',
  315. 'md5': '3b7a683d105a7313ec7513b014443631',
  316. 'info_dict': {
  317. 'id': 'leitv-0000125151',
  318. 'display_id': 'mal-di-testa',
  319. 'ext': 'mp4',
  320. 'title': 'Cervicalgia e mal di testa, il video con i suggerimenti dell\'esperto',
  321. 'description': 'md5:ae21418f34cee0b8d02a487f55bcabb5',
  322. 'uploader': 'leitv.it',
  323. },
  324. }, {
  325. 'url': 'https://www.youreporter.it/fiume-sesia-3-ottobre-2020/',
  326. 'md5': '3989b6d603482611a2abd2f32b79f739',
  327. 'info_dict': {
  328. 'id': 'youreporter-0000332574',
  329. 'display_id': 'fiume-sesia-3-ottobre-2020',
  330. 'ext': 'mp4',
  331. 'title': 'Fiume Sesia 3 ottobre 2020',
  332. 'description': 'md5:0070eef1cc884d13c970a4125063de55',
  333. 'uploader': 'youreporter.it',
  334. },
  335. }, {
  336. 'url': 'https://www.amica.it/video-post/saint-omer-al-cinema-il-film-leone-dargento-che-ribalta-gli-stereotipi/',
  337. 'md5': '187cce524dfd0343c95646c047375fc4',
  338. 'info_dict': {
  339. 'id': 'amica-0001225365',
  340. 'display_id': 'saint-omer-al-cinema-il-film-leone-dargento-che-ribalta-gli-stereotipi',
  341. 'ext': 'mp4',
  342. 'title': '"Saint Omer": al cinema il film Leone d\'argento che ribalta gli stereotipi',
  343. 'description': 'md5:b1c8869c2dcfd6073a2a311ba0008aa8',
  344. 'uploader': 'rcs.it',
  345. },
  346. }]