You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

502 lines
18 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_urlparse,
  6. compat_str,
  7. )
  8. from ..utils import (
  9. ExtractorError,
  10. determine_ext,
  11. find_xpath_attr,
  12. fix_xml_ampersands,
  13. GeoRestrictedError,
  14. int_or_none,
  15. parse_duration,
  16. strip_or_none,
  17. try_get,
  18. unescapeHTML,
  19. unified_strdate,
  20. unified_timestamp,
  21. update_url_query,
  22. urljoin,
  23. xpath_text,
  24. )
  25. class RaiBaseIE(InfoExtractor):
  26. _UUID_RE = r'[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}'
  27. _GEO_COUNTRIES = ['IT']
  28. _GEO_BYPASS = False
  29. def _extract_relinker_info(self, relinker_url, video_id):
  30. if not re.match(r'https?://', relinker_url):
  31. return {'formats': [{'url': relinker_url}]}
  32. formats = []
  33. geoprotection = None
  34. is_live = None
  35. duration = None
  36. for platform in ('mon', 'flash', 'native'):
  37. relinker = self._download_xml(
  38. relinker_url, video_id,
  39. note='Downloading XML metadata for platform %s' % platform,
  40. transform_source=fix_xml_ampersands,
  41. query={'output': 45, 'pl': platform},
  42. headers=self.geo_verification_headers())
  43. if not geoprotection:
  44. geoprotection = xpath_text(
  45. relinker, './geoprotection', default=None) == 'Y'
  46. if not is_live:
  47. is_live = xpath_text(
  48. relinker, './is_live', default=None) == 'Y'
  49. if not duration:
  50. duration = parse_duration(xpath_text(
  51. relinker, './duration', default=None))
  52. url_elem = find_xpath_attr(relinker, './url', 'type', 'content')
  53. if url_elem is None:
  54. continue
  55. media_url = url_elem.text
  56. # This does not imply geo restriction (e.g.
  57. # http://www.raisport.rai.it/dl/raiSport/media/rassegna-stampa-04a9f4bd-b563-40cf-82a6-aad3529cb4a9.html)
  58. if media_url == 'http://download.rai.it/video_no_available.mp4':
  59. continue
  60. ext = determine_ext(media_url)
  61. if (ext == 'm3u8' and platform != 'mon') or (ext == 'f4m' and platform != 'flash'):
  62. continue
  63. if ext == 'm3u8' or 'format=m3u8' in media_url or platform == 'mon':
  64. formats.extend(self._extract_m3u8_formats(
  65. media_url, video_id, 'mp4', 'm3u8_native',
  66. m3u8_id='hls', fatal=False))
  67. elif ext == 'f4m' or platform == 'flash':
  68. manifest_url = update_url_query(
  69. media_url.replace('manifest#live_hds.f4m', 'manifest.f4m'),
  70. {'hdcore': '3.7.0', 'plugin': 'aasp-3.7.0.39.44'})
  71. formats.extend(self._extract_f4m_formats(
  72. manifest_url, video_id, f4m_id='hds', fatal=False))
  73. else:
  74. bitrate = int_or_none(xpath_text(relinker, 'bitrate'))
  75. formats.append({
  76. 'url': media_url,
  77. 'tbr': bitrate if bitrate > 0 else None,
  78. 'format_id': 'http-%d' % bitrate if bitrate > 0 else 'http',
  79. })
  80. if not formats and geoprotection is True:
  81. self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
  82. return dict((k, v) for k, v in {
  83. 'is_live': is_live,
  84. 'duration': duration,
  85. 'formats': formats,
  86. }.items() if v is not None)
  87. @staticmethod
  88. def _extract_subtitles(url, subtitle_url):
  89. subtitles = {}
  90. if subtitle_url and isinstance(subtitle_url, compat_str):
  91. subtitle_url = urljoin(url, subtitle_url)
  92. STL_EXT = '.stl'
  93. SRT_EXT = '.srt'
  94. subtitles['it'] = [{
  95. 'ext': 'stl',
  96. 'url': subtitle_url,
  97. }]
  98. if subtitle_url.endswith(STL_EXT):
  99. srt_url = subtitle_url[:-len(STL_EXT)] + SRT_EXT
  100. subtitles['it'].append({
  101. 'ext': 'srt',
  102. 'url': srt_url,
  103. })
  104. return subtitles
  105. class RaiPlayIE(RaiBaseIE):
  106. _VALID_URL = r'(?P<url>https?://(?:www\.)?raiplay\.it/.+?-(?P<id>%s)\.html)' % RaiBaseIE._UUID_RE
  107. _TESTS = [{
  108. 'url': 'http://www.raiplay.it/video/2016/10/La-Casa-Bianca-e06118bb-59a9-4636-b914-498e4cfd2c66.html?source=twitter',
  109. 'md5': '340aa3b7afb54bfd14a8c11786450d76',
  110. 'info_dict': {
  111. 'id': 'e06118bb-59a9-4636-b914-498e4cfd2c66',
  112. 'ext': 'mp4',
  113. 'title': 'La Casa Bianca',
  114. 'alt_title': 'S2016 - Puntata del 23/10/2016',
  115. 'description': 'md5:a09d45890850458077d1f68bb036e0a5',
  116. 'thumbnail': r're:^https?://.*\.jpg$',
  117. 'uploader': 'Rai 3',
  118. 'creator': 'Rai 3',
  119. 'duration': 3278,
  120. 'timestamp': 1477764300,
  121. 'upload_date': '20161029',
  122. 'series': 'La Casa Bianca',
  123. 'season': '2016',
  124. },
  125. }, {
  126. 'url': 'http://www.raiplay.it/video/2014/04/Report-del-07042014-cb27157f-9dd0-4aee-b788-b1f67643a391.html',
  127. 'md5': '8970abf8caf8aef4696e7b1f2adfc696',
  128. 'info_dict': {
  129. 'id': 'cb27157f-9dd0-4aee-b788-b1f67643a391',
  130. 'ext': 'mp4',
  131. 'title': 'Report del 07/04/2014',
  132. 'alt_title': 'S2013/14 - Puntata del 07/04/2014',
  133. 'description': 'md5:f27c544694cacb46a078db84ec35d2d9',
  134. 'thumbnail': r're:^https?://.*\.jpg$',
  135. 'uploader': 'Rai 5',
  136. 'creator': 'Rai 5',
  137. 'duration': 6160,
  138. 'series': 'Report',
  139. 'season_number': 5,
  140. 'season': '2013/14',
  141. },
  142. 'params': {
  143. 'skip_download': True,
  144. },
  145. }, {
  146. 'url': 'http://www.raiplay.it/video/2016/11/gazebotraindesi-efebe701-969c-4593-92f3-285f0d1ce750.html?',
  147. 'only_matching': True,
  148. }]
  149. def _real_extract(self, url):
  150. mobj = re.match(self._VALID_URL, url)
  151. url, video_id = mobj.group('url', 'id')
  152. media = self._download_json(
  153. '%s?json' % url, video_id, 'Downloading video JSON')
  154. title = media['name']
  155. video = media['video']
  156. relinker_info = self._extract_relinker_info(video['contentUrl'], video_id)
  157. self._sort_formats(relinker_info['formats'])
  158. thumbnails = []
  159. if 'images' in media:
  160. for _, value in media.get('images').items():
  161. if value:
  162. thumbnails.append({
  163. 'url': value.replace('[RESOLUTION]', '600x400')
  164. })
  165. timestamp = unified_timestamp(try_get(
  166. media, lambda x: x['availabilities'][0]['start'], compat_str))
  167. subtitles = self._extract_subtitles(url, video.get('subtitles'))
  168. info = {
  169. 'id': video_id,
  170. 'title': self._live_title(title) if relinker_info.get(
  171. 'is_live') else title,
  172. 'alt_title': media.get('subtitle'),
  173. 'description': media.get('description'),
  174. 'uploader': strip_or_none(media.get('channel')),
  175. 'creator': strip_or_none(media.get('editor')),
  176. 'duration': parse_duration(video.get('duration')),
  177. 'timestamp': timestamp,
  178. 'thumbnails': thumbnails,
  179. 'series': try_get(
  180. media, lambda x: x['isPartOf']['name'], compat_str),
  181. 'season_number': int_or_none(try_get(
  182. media, lambda x: x['isPartOf']['numeroStagioni'])),
  183. 'season': media.get('stagione') or None,
  184. 'subtitles': subtitles,
  185. }
  186. info.update(relinker_info)
  187. return info
  188. class RaiPlayLiveIE(RaiBaseIE):
  189. _VALID_URL = r'https?://(?:www\.)?raiplay\.it/dirette/(?P<id>[^/?#&]+)'
  190. _TEST = {
  191. 'url': 'http://www.raiplay.it/dirette/rainews24',
  192. 'info_dict': {
  193. 'id': 'd784ad40-e0ae-4a69-aa76-37519d238a9c',
  194. 'display_id': 'rainews24',
  195. 'ext': 'mp4',
  196. 'title': 're:^Diretta di Rai News 24 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  197. 'description': 'md5:6eca31500550f9376819f174e5644754',
  198. 'uploader': 'Rai News 24',
  199. 'creator': 'Rai News 24',
  200. 'is_live': True,
  201. },
  202. 'params': {
  203. 'skip_download': True,
  204. },
  205. }
  206. def _real_extract(self, url):
  207. display_id = self._match_id(url)
  208. webpage = self._download_webpage(url, display_id)
  209. video_id = self._search_regex(
  210. r'data-uniquename=["\']ContentItem-(%s)' % RaiBaseIE._UUID_RE,
  211. webpage, 'content id')
  212. return {
  213. '_type': 'url_transparent',
  214. 'ie_key': RaiPlayIE.ie_key(),
  215. 'url': 'http://www.raiplay.it/dirette/ContentItem-%s.html' % video_id,
  216. 'id': video_id,
  217. 'display_id': display_id,
  218. }
  219. class RaiPlayPlaylistIE(InfoExtractor):
  220. _VALID_URL = r'https?://(?:www\.)?raiplay\.it/programmi/(?P<id>[^/?#&]+)'
  221. _TESTS = [{
  222. 'url': 'http://www.raiplay.it/programmi/nondirloalmiocapo/',
  223. 'info_dict': {
  224. 'id': 'nondirloalmiocapo',
  225. 'title': 'Non dirlo al mio capo',
  226. 'description': 'md5:9f3d603b2947c1c7abb098f3b14fac86',
  227. },
  228. 'playlist_mincount': 12,
  229. }]
  230. def _real_extract(self, url):
  231. playlist_id = self._match_id(url)
  232. webpage = self._download_webpage(url, playlist_id)
  233. title = self._html_search_meta(
  234. ('programma', 'nomeProgramma'), webpage, 'title')
  235. description = unescapeHTML(self._html_search_meta(
  236. ('description', 'og:description'), webpage, 'description'))
  237. entries = []
  238. for mobj in re.finditer(
  239. r'<a\b[^>]+\bhref=(["\'])(?P<path>/raiplay/video/.+?)\1',
  240. webpage):
  241. video_url = urljoin(url, mobj.group('path'))
  242. entries.append(self.url_result(
  243. video_url, ie=RaiPlayIE.ie_key(),
  244. video_id=RaiPlayIE._match_id(video_url)))
  245. return self.playlist_result(entries, playlist_id, title, description)
  246. class RaiIE(RaiBaseIE):
  247. _VALID_URL = r'https?://[^/]+\.(?:rai\.(?:it|tv)|rainews\.it)/.+?-(?P<id>%s)(?:-.+?)?\.html' % RaiBaseIE._UUID_RE
  248. _TESTS = [{
  249. # var uniquename = "ContentItem-..."
  250. # data-id="ContentItem-..."
  251. 'url': 'http://www.raisport.rai.it/dl/raiSport/media/rassegna-stampa-04a9f4bd-b563-40cf-82a6-aad3529cb4a9.html',
  252. 'info_dict': {
  253. 'id': '04a9f4bd-b563-40cf-82a6-aad3529cb4a9',
  254. 'ext': 'mp4',
  255. 'title': 'TG PRIMO TEMPO',
  256. 'thumbnail': r're:^https?://.*\.jpg$',
  257. 'duration': 1758,
  258. 'upload_date': '20140612',
  259. }
  260. }, {
  261. # with ContentItem in many metas
  262. 'url': 'http://www.rainews.it/dl/rainews/media/Weekend-al-cinema-da-Hollywood-arriva-il-thriller-di-Tate-Taylor-La-ragazza-del-treno-1632c009-c843-4836-bb65-80c33084a64b.html',
  263. 'info_dict': {
  264. 'id': '1632c009-c843-4836-bb65-80c33084a64b',
  265. 'ext': 'mp4',
  266. 'title': 'Weekend al cinema, da Hollywood arriva il thriller di Tate Taylor "La ragazza del treno"',
  267. 'description': 'I film in uscita questa settimana.',
  268. 'thumbnail': r're:^https?://.*\.png$',
  269. 'duration': 833,
  270. 'upload_date': '20161103',
  271. }
  272. }, {
  273. # with ContentItem in og:url
  274. 'url': 'http://www.rai.it/dl/RaiTV/programmi/media/ContentItem-efb17665-691c-45d5-a60c-5301333cbb0c.html',
  275. 'md5': '11959b4e44fa74de47011b5799490adf',
  276. 'info_dict': {
  277. 'id': 'efb17665-691c-45d5-a60c-5301333cbb0c',
  278. 'ext': 'mp4',
  279. 'title': 'TG1 ore 20:00 del 03/11/2016',
  280. 'description': 'TG1 edizione integrale ore 20:00 del giorno 03/11/2016',
  281. 'thumbnail': r're:^https?://.*\.jpg$',
  282. 'duration': 2214,
  283. 'upload_date': '20161103',
  284. }
  285. }, {
  286. # drawMediaRaiTV(...)
  287. 'url': 'http://www.report.rai.it/dl/Report/puntata/ContentItem-0c7a664b-d0f4-4b2c-8835-3f82e46f433e.html',
  288. 'md5': '2dd727e61114e1ee9c47f0da6914e178',
  289. 'info_dict': {
  290. 'id': '59d69d28-6bb6-409d-a4b5-ed44096560af',
  291. 'ext': 'mp4',
  292. 'title': 'Il pacco',
  293. 'description': 'md5:4b1afae1364115ce5d78ed83cd2e5b3a',
  294. 'thumbnail': r're:^https?://.*\.jpg$',
  295. 'upload_date': '20141221',
  296. },
  297. }, {
  298. # initEdizione('ContentItem-...'
  299. 'url': 'http://www.tg1.rai.it/dl/tg1/2010/edizioni/ContentSet-9b6e0cba-4bef-4aef-8cf0-9f7f665b7dfb-tg1.html?item=undefined',
  300. 'info_dict': {
  301. 'id': 'c2187016-8484-4e3a-8ac8-35e475b07303',
  302. 'ext': 'mp4',
  303. 'title': r're:TG1 ore \d{2}:\d{2} del \d{2}/\d{2}/\d{4}',
  304. 'duration': 2274,
  305. 'upload_date': '20170401',
  306. },
  307. 'skip': 'Changes daily',
  308. }, {
  309. # HDS live stream with only relinker URL
  310. 'url': 'http://www.rai.tv/dl/RaiTV/dirette/PublishingBlock-1912dbbf-3f96-44c3-b4cf-523681fbacbc.html?channel=EuroNews',
  311. 'info_dict': {
  312. 'id': '1912dbbf-3f96-44c3-b4cf-523681fbacbc',
  313. 'ext': 'flv',
  314. 'title': 'EuroNews',
  315. },
  316. 'params': {
  317. 'skip_download': True,
  318. },
  319. }, {
  320. # HLS live stream with ContentItem in og:url
  321. 'url': 'http://www.rainews.it/dl/rainews/live/ContentItem-3156f2f2-dc70-4953-8e2f-70d7489d4ce9.html',
  322. 'info_dict': {
  323. 'id': '3156f2f2-dc70-4953-8e2f-70d7489d4ce9',
  324. 'ext': 'mp4',
  325. 'title': 'La diretta di Rainews24',
  326. },
  327. 'params': {
  328. 'skip_download': True,
  329. },
  330. }, {
  331. # Direct MMS URL
  332. 'url': 'http://www.rai.it/dl/RaiTV/programmi/media/ContentItem-b63a4089-ac28-48cf-bca5-9f5b5bc46df5.html',
  333. 'only_matching': True,
  334. }, {
  335. 'url': 'https://www.rainews.it/tgr/marche/notiziari/video/2019/02/ContentItem-6ba945a2-889c-4a80-bdeb-8489c70a8db9.html',
  336. 'only_matching': True,
  337. }]
  338. def _extract_from_content_id(self, content_id, url):
  339. media = self._download_json(
  340. 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-%s.html?json' % content_id,
  341. content_id, 'Downloading video JSON')
  342. title = media['name'].strip()
  343. media_type = media['type']
  344. if 'Audio' in media_type:
  345. relinker_info = {
  346. 'formats': [{
  347. 'format_id': media.get('formatoAudio'),
  348. 'url': media['audioUrl'],
  349. 'ext': media.get('formatoAudio'),
  350. }]
  351. }
  352. elif 'Video' in media_type:
  353. relinker_info = self._extract_relinker_info(media['mediaUri'], content_id)
  354. else:
  355. raise ExtractorError('not a media file')
  356. self._sort_formats(relinker_info['formats'])
  357. thumbnails = []
  358. for image_type in ('image', 'image_medium', 'image_300'):
  359. thumbnail_url = media.get(image_type)
  360. if thumbnail_url:
  361. thumbnails.append({
  362. 'url': compat_urlparse.urljoin(url, thumbnail_url),
  363. })
  364. subtitles = self._extract_subtitles(url, media.get('subtitlesUrl'))
  365. info = {
  366. 'id': content_id,
  367. 'title': title,
  368. 'description': strip_or_none(media.get('desc')),
  369. 'thumbnails': thumbnails,
  370. 'uploader': media.get('author'),
  371. 'upload_date': unified_strdate(media.get('date')),
  372. 'duration': parse_duration(media.get('length')),
  373. 'subtitles': subtitles,
  374. }
  375. info.update(relinker_info)
  376. return info
  377. def _real_extract(self, url):
  378. video_id = self._match_id(url)
  379. webpage = self._download_webpage(url, video_id)
  380. content_item_id = None
  381. content_item_url = self._html_search_meta(
  382. ('og:url', 'og:video', 'og:video:secure_url', 'twitter:url',
  383. 'twitter:player', 'jsonlink'), webpage, default=None)
  384. if content_item_url:
  385. content_item_id = self._search_regex(
  386. r'ContentItem-(%s)' % self._UUID_RE, content_item_url,
  387. 'content item id', default=None)
  388. if not content_item_id:
  389. content_item_id = self._search_regex(
  390. r'''(?x)
  391. (?:
  392. (?:initEdizione|drawMediaRaiTV)\(|
  393. <(?:[^>]+\bdata-id|var\s+uniquename)=
  394. )
  395. (["\'])
  396. (?:(?!\1).)*\bContentItem-(?P<id>%s)
  397. ''' % self._UUID_RE,
  398. webpage, 'content item id', default=None, group='id')
  399. content_item_ids = set()
  400. if content_item_id:
  401. content_item_ids.add(content_item_id)
  402. if video_id not in content_item_ids:
  403. content_item_ids.add(video_id)
  404. for content_item_id in content_item_ids:
  405. try:
  406. return self._extract_from_content_id(content_item_id, url)
  407. except GeoRestrictedError:
  408. raise
  409. except ExtractorError:
  410. pass
  411. relinker_url = self._search_regex(
  412. r'''(?x)
  413. (?:
  414. var\s+videoURL|
  415. mediaInfo\.mediaUri
  416. )\s*=\s*
  417. ([\'"])
  418. (?P<url>
  419. (?:https?:)?
  420. //mediapolis(?:vod)?\.rai\.it/relinker/relinkerServlet\.htm\?
  421. (?:(?!\1).)*\bcont=(?:(?!\1).)+)\1
  422. ''',
  423. webpage, 'relinker URL', group='url')
  424. relinker_info = self._extract_relinker_info(
  425. urljoin(url, relinker_url), video_id)
  426. self._sort_formats(relinker_info['formats'])
  427. title = self._search_regex(
  428. r'var\s+videoTitolo\s*=\s*([\'"])(?P<title>[^\'"]+)\1',
  429. webpage, 'title', group='title',
  430. default=None) or self._og_search_title(webpage)
  431. info = {
  432. 'id': video_id,
  433. 'title': title,
  434. }
  435. info.update(relinker_info)
  436. return info