rcs.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. ExtractorError,
  5. base_url,
  6. clean_html,
  7. js_to_json,
  8. url_basename,
  9. urljoin,
  10. )
  11. class RCSBaseIE(InfoExtractor):
  12. # based on VideoPlayerLoader.prototype.getVideoSrc
  13. # and VideoPlayerLoader.prototype.transformSrc from
  14. # https://js2.corriereobjects.it/includes2013/LIBS/js/corriere_video.sjs
  15. _ALL_REPLACE = {
  16. 'media2vam.corriere.it.edgesuite.net':
  17. 'media2vam-corriere-it.akamaized.net',
  18. 'media.youreporter.it.edgesuite.net':
  19. 'media-youreporter-it.akamaized.net',
  20. 'corrierepmd.corriere.it.edgesuite.net':
  21. 'corrierepmd-corriere-it.akamaized.net',
  22. 'media2vam-corriere-it.akamaized.net/fcs.quotidiani/vr/videos/':
  23. 'video.corriere.it/vr360/videos/',
  24. '.net//': '.net/',
  25. }
  26. _MP4_REPLACE = {
  27. 'media2vam.corbologna.corriere.it.edgesuite.net':
  28. 'media2vam-bologna-corriere-it.akamaized.net',
  29. 'media2vam.corfiorentino.corriere.it.edgesuite.net':
  30. 'media2vam-fiorentino-corriere-it.akamaized.net',
  31. 'media2vam.cormezzogiorno.corriere.it.edgesuite.net':
  32. 'media2vam-mezzogiorno-corriere-it.akamaized.net',
  33. 'media2vam.corveneto.corriere.it.edgesuite.net':
  34. 'media2vam-veneto-corriere-it.akamaized.net',
  35. 'media2.oggi.it.edgesuite.net':
  36. 'media2-oggi-it.akamaized.net',
  37. 'media2.quimamme.it.edgesuite.net':
  38. 'media2-quimamme-it.akamaized.net',
  39. 'media2.amica.it.edgesuite.net':
  40. 'media2-amica-it.akamaized.net',
  41. 'media2.living.corriere.it.edgesuite.net':
  42. 'media2-living-corriere-it.akamaized.net',
  43. 'media2.style.corriere.it.edgesuite.net':
  44. 'media2-style-corriere-it.akamaized.net',
  45. 'media2.iodonna.it.edgesuite.net':
  46. 'media2-iodonna-it.akamaized.net',
  47. 'media2.leitv.it.edgesuite.net':
  48. 'media2-leitv-it.akamaized.net',
  49. }
  50. _MIGRATION_MAP = {
  51. 'videoamica-vh.akamaihd': 'amica',
  52. 'media2-amica-it.akamaized': 'amica',
  53. 'corrierevam-vh.akamaihd': 'corriere',
  54. 'media2vam-corriere-it.akamaized': 'corriere',
  55. 'cormezzogiorno-vh.akamaihd': 'corrieredelmezzogiorno',
  56. 'media2vam-mezzogiorno-corriere-it.akamaized': 'corrieredelmezzogiorno',
  57. 'corveneto-vh.akamaihd': 'corrieredelveneto',
  58. 'media2vam-veneto-corriere-it.akamaized': 'corrieredelveneto',
  59. 'corbologna-vh.akamaihd': 'corrieredibologna',
  60. 'media2vam-bologna-corriere-it.akamaized': 'corrieredibologna',
  61. 'corfiorentino-vh.akamaihd': 'corrierefiorentino',
  62. 'media2vam-fiorentino-corriere-it.akamaized': 'corrierefiorentino',
  63. 'corinnovazione-vh.akamaihd': 'corriereinnovazione',
  64. 'media2-gazzanet-gazzetta-it.akamaized': 'gazzanet',
  65. 'videogazzanet-vh.akamaihd': 'gazzanet',
  66. 'videogazzaworld-vh.akamaihd': 'gazzaworld',
  67. 'gazzettavam-vh.akamaihd': 'gazzetta',
  68. 'media2vam-gazzetta-it.akamaized': 'gazzetta',
  69. 'videoiodonna-vh.akamaihd': 'iodonna',
  70. 'media2-leitv-it.akamaized': 'leitv',
  71. 'videoleitv-vh.akamaihd': 'leitv',
  72. 'videoliving-vh.akamaihd': 'living',
  73. 'media2-living-corriere-it.akamaized': 'living',
  74. 'media2-oggi-it.akamaized': 'oggi',
  75. 'videooggi-vh.akamaihd': 'oggi',
  76. 'media2-quimamme-it.akamaized': 'quimamme',
  77. 'quimamme-vh.akamaihd': 'quimamme',
  78. 'videorunning-vh.akamaihd': 'running',
  79. 'media2-style-corriere-it.akamaized': 'style',
  80. 'style-vh.akamaihd': 'style',
  81. 'videostyle-vh.akamaihd': 'style',
  82. 'media2-stylepiccoli-it.akamaized': 'stylepiccoli',
  83. 'stylepiccoli-vh.akamaihd': 'stylepiccoli',
  84. 'doveviaggi-vh.akamaihd': 'viaggi',
  85. 'media2-doveviaggi-it.akamaized': 'viaggi',
  86. 'media2-vivimilano-corriere-it.akamaized': 'vivimilano',
  87. 'vivimilano-vh.akamaihd': 'vivimilano',
  88. 'media2-youreporter-it.akamaized': 'youreporter'
  89. }
  90. _MIGRATION_MEDIA = {
  91. 'advrcs-vh.akamaihd': '',
  92. 'corriere-f.akamaihd': '',
  93. 'corrierepmd-corriere-it.akamaized': '',
  94. 'corrprotetto-vh.akamaihd': '',
  95. 'gazzetta-f.akamaihd': '',
  96. 'gazzettapmd-gazzetta-it.akamaized': '',
  97. 'gazzprotetto-vh.akamaihd': '',
  98. 'periodici-f.akamaihd': '',
  99. 'periodicisecure-vh.akamaihd': '',
  100. 'videocoracademy-vh.akamaihd': ''
  101. }
  102. def _get_video_src(self, video):
  103. mediaFiles = video.get('mediaProfile').get('mediaFile')
  104. src = {}
  105. # audio
  106. if video.get('mediaType') == 'AUDIO':
  107. for aud in mediaFiles:
  108. # todo: check
  109. src['mp3'] = aud.get('value')
  110. # video
  111. else:
  112. for vid in mediaFiles:
  113. if vid.get('mimeType') == 'application/vnd.apple.mpegurl':
  114. src['m3u8'] = vid.get('value')
  115. if vid.get('mimeType') == 'video/mp4':
  116. src['mp4'] = vid.get('value')
  117. # replace host
  118. for t in src:
  119. for s, r in self._ALL_REPLACE.items():
  120. src[t] = src[t].replace(s, r)
  121. for s, r in self._MP4_REPLACE.items():
  122. src[t] = src[t].replace(s, r)
  123. # switch cdn
  124. if 'mp4' in src and 'm3u8' in src:
  125. if ('-lh.akamaihd' not in src.get('m3u8')
  126. and 'akamai' in src.get('mp4')):
  127. if 'm3u8' in src:
  128. matches = re.search(r'(?:https*:)?\/\/(?P<host>.*)\.net\/i(?P<path>.*)$', src.get('m3u8'))
  129. src['m3u8'] = 'https://vod.rcsobjects.it/hls/%s%s' % (
  130. self._MIGRATION_MAP[matches.group('host')],
  131. matches.group('path').replace(
  132. '///', '/').replace(
  133. '//', '/').replace(
  134. '.csmil', '.urlset'
  135. )
  136. )
  137. if 'mp4' in src:
  138. matches = re.search(r'(?:https*:)?\/\/(?P<host>.*)\.net\/i(?P<path>.*)$', src.get('mp4'))
  139. if matches:
  140. if matches.group('host') in self._MIGRATION_MEDIA:
  141. vh_stream = 'https://media2.corriereobjects.it'
  142. if src.get('mp4').find('fcs.quotidiani_!'):
  143. vh_stream = 'https://media2-it.corriereobjects.it'
  144. src['mp4'] = '%s%s' % (
  145. vh_stream,
  146. matches.group('path').replace(
  147. '///', '/').replace(
  148. '//', '/').replace(
  149. '/fcs.quotidiani/mediacenter', '').replace(
  150. '/fcs.quotidiani_!/mediacenter', '').replace(
  151. 'corriere/content/mediacenter/', '').replace(
  152. 'gazzetta/content/mediacenter/', '')
  153. )
  154. else:
  155. src['mp4'] = 'https://vod.rcsobjects.it/%s%s' % (
  156. self._MIGRATION_MAP[matches.group('host')],
  157. matches.group('path').replace('///', '/').replace('//', '/')
  158. )
  159. if 'mp3' in src:
  160. src['mp3'] = src.get('mp3').replace(
  161. 'media2vam-corriere-it.akamaized.net',
  162. 'vod.rcsobjects.it/corriere')
  163. if 'mp4' in src:
  164. if src.get('mp4').find('fcs.quotidiani_!'):
  165. src['mp4'] = src.get('mp4').replace('vod.rcsobjects', 'vod-it.rcsobjects')
  166. if 'm3u8' in src:
  167. if src.get('m3u8').find('fcs.quotidiani_!'):
  168. src['m3u8'] = src.get('m3u8').replace('vod.rcsobjects', 'vod-it.rcsobjects')
  169. if 'geoblocking' in video.get('mediaProfile'):
  170. if 'm3u8' in src:
  171. src['m3u8'] = src.get('m3u8').replace('vod.rcsobjects', 'vod-it.rcsobjects')
  172. if 'mp4' in src:
  173. src['mp4'] = src.get('mp4').replace('vod.rcsobjects', 'vod-it.rcsobjects')
  174. if 'm3u8' in src:
  175. if src.get('m3u8').find('csmil') and src.get('m3u8').find('vod'):
  176. src['m3u8'] = src.get('m3u8').replace('.csmil', '.urlset')
  177. return src
  178. def _create_formats(self, urls, video_id):
  179. formats = []
  180. formats = self._extract_m3u8_formats(
  181. urls.get('m3u8'), video_id, 'mp4', entry_protocol='m3u8_native',
  182. m3u8_id='hls', fatal=False)
  183. if urls.get('mp4'):
  184. formats.append({
  185. 'format_id': 'http-mp4',
  186. 'url': urls['mp4']
  187. })
  188. return formats
  189. def _real_extract(self, url):
  190. mobj = self._match_valid_url(url)
  191. video_id = mobj.group('id')
  192. if 'cdn' not in mobj.groupdict():
  193. raise ExtractorError('CDN not found in url: %s' % url)
  194. # for leitv/youreporter/viaggi don't use the embed page
  195. if ((mobj.group('cdn') not in ['leitv.it', 'youreporter.it'])
  196. and (mobj.group('vid') == 'video')):
  197. url = 'https://video.%s/video-embed/%s' % (mobj.group('cdn'), video_id)
  198. page = self._download_webpage(url, video_id)
  199. video_data = None
  200. # look for json video data url
  201. json = self._search_regex(
  202. r'''(?x)url\s*=\s*(["'])
  203. (?P<url>
  204. (?:https?:)?//video\.rcs\.it
  205. /fragment-includes/video-includes/.+?\.json
  206. )\1;''',
  207. page, video_id, group='url', default=None)
  208. if json:
  209. if json.startswith('//'):
  210. json = 'https:%s' % json
  211. video_data = self._download_json(json, video_id)
  212. # if json url not found, look for json video data directly in the page
  213. else:
  214. # RCS normal pages and most of the embeds
  215. json = self._search_regex(
  216. r'[\s;]video\s*=\s*({[\s\S]+?})(?:;|,playlist=)',
  217. page, video_id, default=None)
  218. if not json and 'video-embed' in url:
  219. page = self._download_webpage(url.replace('video-embed', 'video-json'), video_id)
  220. json = self._search_regex(
  221. r'##start-video##({[\s\S]+?})##end-video##',
  222. page, video_id, default=None)
  223. if not json:
  224. # if no video data found try search for iframes
  225. emb = RCSEmbedsIE._extract_url(page)
  226. if emb:
  227. return {
  228. '_type': 'url_transparent',
  229. 'url': emb,
  230. 'ie_key': RCSEmbedsIE.ie_key()
  231. }
  232. if json:
  233. video_data = self._parse_json(
  234. json, video_id, transform_source=js_to_json)
  235. if not video_data:
  236. raise ExtractorError('Video data not found in the page')
  237. formats = self._create_formats(
  238. self._get_video_src(video_data), video_id)
  239. description = (video_data.get('description')
  240. or clean_html(video_data.get('htmlDescription'))
  241. or self._html_search_meta('description', page))
  242. uploader = video_data.get('provider') or mobj.group('cdn')
  243. return {
  244. 'id': video_id,
  245. 'title': video_data.get('title'),
  246. 'description': description,
  247. 'uploader': uploader,
  248. 'formats': formats
  249. }
  250. class RCSEmbedsIE(RCSBaseIE):
  251. _VALID_URL = r'''(?x)
  252. https?://(?P<vid>video)\.
  253. (?P<cdn>
  254. (?:
  255. rcs|
  256. (?:corriere\w+\.)?corriere|
  257. (?:gazzanet\.)?gazzetta
  258. )\.it)
  259. /video-embed/(?P<id>[^/=&\?]+?)(?:$|\?)'''
  260. _EMBED_REGEX = [r'''(?x)
  261. (?:
  262. data-frame-src=|
  263. <iframe[^\n]+src=
  264. )
  265. (["'])
  266. (?P<url>(?:https?:)?//video\.
  267. (?:
  268. rcs|
  269. (?:corriere\w+\.)?corriere|
  270. (?:gazzanet\.)?gazzetta
  271. )
  272. \.it/video-embed/.+?)
  273. \1''']
  274. _TESTS = [{
  275. 'url': 'https://video.rcs.it/video-embed/iodonna-0001585037',
  276. 'md5': '623ecc8ffe7299b2d0c1046d8331a9df',
  277. 'info_dict': {
  278. 'id': 'iodonna-0001585037',
  279. 'ext': 'mp4',
  280. 'title': 'Sky Arte racconta Madonna nella serie "Artist to icon"',
  281. 'description': 'md5:65b09633df9ffee57f48b39e34c9e067',
  282. 'uploader': 'rcs.it',
  283. }
  284. }, {
  285. # redownload the page changing 'video-embed' in 'video-json'
  286. 'url': 'https://video.gazzanet.gazzetta.it/video-embed/gazzanet-mo05-0000260789',
  287. 'md5': 'a043e3fecbe4d9ed7fc5d888652a5440',
  288. 'info_dict': {
  289. 'id': 'gazzanet-mo05-0000260789',
  290. 'ext': 'mp4',
  291. 'title': 'Valentino Rossi e papà Graziano si divertono col drifting',
  292. 'description': 'md5:a8bf90d6adafd9815f70fc74c0fc370a',
  293. 'uploader': 'rcd',
  294. }
  295. }, {
  296. 'url': 'https://video.corriere.it/video-embed/b727632a-f9d0-11ea-91b0-38d50a849abb?player',
  297. 'match_only': True
  298. }, {
  299. 'url': 'https://video.gazzetta.it/video-embed/49612410-00ca-11eb-bcd8-30d4253e0140',
  300. 'match_only': True
  301. }]
  302. @staticmethod
  303. def _sanitize_urls(urls):
  304. # add protocol if missing
  305. for i, e in enumerate(urls):
  306. if e.startswith('//'):
  307. urls[i] = 'https:%s' % e
  308. # clean iframes urls
  309. for i, e in enumerate(urls):
  310. urls[i] = urljoin(base_url(e), url_basename(e))
  311. return urls
  312. @classmethod
  313. def _extract_embed_urls(cls, url, webpage):
  314. return cls._sanitize_urls(list(super()._extract_embed_urls(url, webpage)))
  315. class RCSIE(RCSBaseIE):
  316. _VALID_URL = r'''(?x)https?://(?P<vid>video|viaggi)\.
  317. (?P<cdn>
  318. (?:
  319. corrieredelmezzogiorno\.
  320. |corrieredelveneto\.
  321. |corrieredibologna\.
  322. |corrierefiorentino\.
  323. )?corriere\.it
  324. |(?:gazzanet\.)?gazzetta\.it)
  325. /(?!video-embed/).+?/(?P<id>[^/\?]+)(?=\?|/$|$)'''
  326. _TESTS = [{
  327. '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',
  328. 'md5': '0f4ededc202b0f00b6e509d831e2dcda',
  329. 'info_dict': {
  330. 'id': 'b727632a-f9d0-11ea-91b0-38d50a849abb',
  331. 'ext': 'mp4',
  332. 'title': 'Vettel guida la Ferrari SF90 al Mugello e al suo fianco c\'è Leclerc (bendato): il video è esilarante',
  333. 'description': 'md5:93b51c9161ac8a64fb2f997b054d0152',
  334. 'uploader': 'Corriere Tv',
  335. }
  336. }, {
  337. # video data inside iframe
  338. 'url': 'https://viaggi.corriere.it/video/norvegia-il-nuovo-ponte-spettacolare-sopra-la-cascata-di-voringsfossen/',
  339. 'md5': 'da378e4918d2afbf7d61c35abb948d4c',
  340. 'info_dict': {
  341. 'id': '5b7cd134-e2c1-11ea-89b3-b56dd0df2aa2',
  342. 'ext': 'mp4',
  343. 'title': 'La nuova spettacolare attrazione in Norvegia: il ponte sopra Vøringsfossen',
  344. 'description': 'md5:18b35a291f6746c0c8dacd16e5f5f4f8',
  345. 'uploader': 'DOVE Viaggi',
  346. }
  347. }, {
  348. 'url': 'https://video.gazzetta.it/video-motogp-catalogna-cadute-dovizioso-vale-rossi/49612410-00ca-11eb-bcd8-30d4253e0140?vclk=Videobar',
  349. 'md5': 'eedc1b5defd18e67383afef51ff7bdf9',
  350. 'info_dict': {
  351. 'id': '49612410-00ca-11eb-bcd8-30d4253e0140',
  352. 'ext': 'mp4',
  353. 'title': 'Dovizioso, il contatto con Zarco e la caduta. E anche Vale finisce a terra',
  354. 'description': 'md5:8c6e905dc3b9413218beca11ebd69778',
  355. 'uploader': 'AMorici',
  356. }
  357. }, {
  358. 'url': 'https://video.corriere.it/video-360/metro-copenaghen-tutta-italiana/a248a7f0-e2db-11e9-9830-af2de6b1f945',
  359. 'match_only': True
  360. }]
  361. class RCSVariousIE(RCSBaseIE):
  362. _VALID_URL = r'''(?x)https?://www\.
  363. (?P<cdn>
  364. leitv\.it|
  365. youreporter\.it
  366. )/(?:[^/]+/)?(?P<id>[^/]+?)(?:$|\?|/)'''
  367. _TESTS = [{
  368. 'url': 'https://www.leitv.it/benessere/mal-di-testa-come-combatterlo-ed-evitarne-la-comparsa/',
  369. 'md5': '92b4e63667b8f95acb0a04da25ae28a1',
  370. 'info_dict': {
  371. 'id': 'mal-di-testa-come-combatterlo-ed-evitarne-la-comparsa',
  372. 'ext': 'mp4',
  373. 'title': 'Cervicalgia e mal di testa, il video con i suggerimenti dell\'esperto',
  374. 'description': 'md5:ae21418f34cee0b8d02a487f55bcabb5',
  375. 'uploader': 'leitv.it',
  376. }
  377. }, {
  378. 'url': 'https://www.youreporter.it/fiume-sesia-3-ottobre-2020/',
  379. 'md5': '8dccd436b47a830bab5b4a88232f391a',
  380. 'info_dict': {
  381. 'id': 'fiume-sesia-3-ottobre-2020',
  382. 'ext': 'mp4',
  383. 'title': 'Fiume Sesia 3 ottobre 2020',
  384. 'description': 'md5:0070eef1cc884d13c970a4125063de55',
  385. 'uploader': 'youreporter.it',
  386. }
  387. }]