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.

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