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.

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