rai.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..compat import compat_urlparse
  4. from ..utils import (
  5. ExtractorError,
  6. determine_ext,
  7. find_xpath_attr,
  8. fix_xml_ampersands,
  9. int_or_none,
  10. parse_duration,
  11. unified_strdate,
  12. update_url_query,
  13. xpath_text,
  14. )
  15. class RaiBaseIE(InfoExtractor):
  16. def _extract_relinker_formats(self, relinker_url, video_id):
  17. formats = []
  18. for platform in ('mon', 'flash', 'native'):
  19. relinker = self._download_xml(
  20. relinker_url, video_id,
  21. note='Downloading XML metadata for platform %s' % platform,
  22. transform_source=fix_xml_ampersands,
  23. query={'output': 45, 'pl': platform},
  24. headers=self.geo_verification_headers())
  25. media_url = find_xpath_attr(relinker, './url', 'type', 'content').text
  26. if media_url == 'http://download.rai.it/video_no_available.mp4':
  27. self.raise_geo_restricted()
  28. ext = determine_ext(media_url)
  29. if (ext == 'm3u8' and platform != 'mon') or (ext == 'f4m' and platform != 'flash'):
  30. continue
  31. if ext == 'm3u8':
  32. formats.extend(self._extract_m3u8_formats(
  33. media_url, video_id, 'mp4', 'm3u8_native',
  34. m3u8_id='hls', fatal=False))
  35. elif ext == 'f4m':
  36. manifest_url = update_url_query(
  37. media_url.replace('manifest#live_hds.f4m', 'manifest.f4m'),
  38. {'hdcore': '3.7.0', 'plugin': 'aasp-3.7.0.39.44'})
  39. formats.extend(self._extract_f4m_formats(
  40. manifest_url, video_id, f4m_id='hds', fatal=False))
  41. else:
  42. bitrate = int_or_none(xpath_text(relinker, 'bitrate'))
  43. formats.append({
  44. 'url': media_url,
  45. 'tbr': bitrate if bitrate > 0 else None,
  46. 'format_id': 'http-%d' % bitrate if bitrate > 0 else 'http',
  47. })
  48. return formats
  49. class RaiPlayIE(RaiBaseIE):
  50. _VALID_URL = r'https?://(?:www\.)?raiplay\.it/.+?-(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})\.html'
  51. _TESTS = [{
  52. 'url': 'http://www.raiplay.it/video/2016/10/La-Casa-Bianca-e06118bb-59a9-4636-b914-498e4cfd2c66.html?source=twitter',
  53. 'md5': '340aa3b7afb54bfd14a8c11786450d76',
  54. 'info_dict': {
  55. 'id': 'e06118bb-59a9-4636-b914-498e4cfd2c66',
  56. 'ext': 'mp4',
  57. 'title': 'La Casa Bianca',
  58. 'thumbnail': r're:^https?://.*\.jpg$',
  59. 'uploader': r're:^Rai.+',
  60. 'description': 're:^[A-Za-z]+'
  61. }
  62. }, {
  63. 'url': 'http://www.raiplay.it/video/2016/11/gazebotraindesi-efebe701-969c-4593-92f3-285f0d1ce750.html?',
  64. 'md5': 'ed4da3d70ccf8129a33ab16b34d20ab8',
  65. 'info_dict': {
  66. 'id': 'efebe701-969c-4593-92f3-285f0d1ce750',
  67. 'ext': 'mp4',
  68. 'title': 'Gazebo - #gazebotraindesi',
  69. 'thumbnail': r're:^https?://.*\.png$',
  70. 'uploader': r're:^Rai.+',
  71. 'description': r're:^[A-Za-z]+'
  72. }
  73. }, {
  74. 'url': 'http://www.raiplay.it/video/2014/04/Report-del-07042014-cb27157f-9dd0-4aee-b788-b1f67643a391.html',
  75. 'md5': '8970abf8caf8aef4696e7b1f2adfc696',
  76. 'info_dict': {
  77. 'id': 'cb27157f-9dd0-4aee-b788-b1f67643a391',
  78. 'ext': 'mp4',
  79. 'title': 'Report - Report del 07/04/2014',
  80. 'thumbnail': r're:^https?://.*\.jpg$',
  81. 'uploader': r're:^Rai.+',
  82. 'description': r're:^[A-Za-z]+'
  83. }
  84. }]
  85. _RESOLUTION = '600x400'
  86. def _real_extract(self, url):
  87. video_id = self._match_id(url)
  88. # remove query and fragment part from url
  89. canonical_url = compat_urlparse.urljoin(url, compat_urlparse.urlparse(url).path)
  90. webpage = self._download_webpage(canonical_url, video_id)
  91. media = self._download_json('%s?json' % canonical_url,
  92. video_id, 'Downloading video JSON')
  93. thumbnails = []
  94. if 'images' in media:
  95. for _, value in media.get('images').items():
  96. if value:
  97. thumbnails.append({
  98. 'url': value.replace('[RESOLUTION]', self._RESOLUTION)
  99. })
  100. if 'video' not in media:
  101. raise ExtractorError('No video found')
  102. video = media.get('video')
  103. duration = parse_duration(video.get('duration')),
  104. formats = self._extract_relinker_formats(video.get('contentUrl'), video_id)
  105. self._sort_formats(formats)
  106. return {
  107. 'id': video_id,
  108. 'title': self._og_search_title(webpage).replace(' - video - RaiPlay', ''),
  109. 'description': self._og_search_description(webpage),
  110. 'uploader': media.get('channel'),
  111. 'duration': duration,
  112. 'thumbnails': thumbnails,
  113. 'formats': formats
  114. }
  115. class RaiIE(RaiBaseIE):
  116. _VALID_URL = r'https?://.+\.(?:rai|rainews)\.it/dl/.+?-(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})(?:-.+?)?\.html'
  117. _TESTS = [{
  118. # subdomain test case
  119. 'url': 'http://www.raisport.rai.it/dl/raiSport/media/rassegna-stampa-04a9f4bd-b563-40cf-82a6-aad3529cb4a9.html',
  120. 'info_dict': {
  121. 'id': '04a9f4bd-b563-40cf-82a6-aad3529cb4a9',
  122. 'ext': 'mp4',
  123. 'title': 'TG PRIMO TEMPO',
  124. 'upload_date': '20140612',
  125. 'duration': 1758,
  126. 'thumbnail': r're:^https?://.*\.jpg$'
  127. }
  128. }, {
  129. # rainews test case
  130. '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',
  131. 'info_dict': {
  132. 'id': '1632c009-c843-4836-bb65-80c33084a64b',
  133. 'ext': 'mp4',
  134. 'title': 'Weekend al cinema, da Hollywood arriva il thriller di Tate Taylor \"La ragazza del treno\" ',
  135. 'upload_date': '20161103',
  136. 'thumbnail': r're:^https?://.*\.png$',
  137. 'description': r're:^[A-Za-z]+'
  138. }
  139. }, {
  140. # with media information
  141. 'url': 'http://www.rai.it/dl/RaiTV/programmi/media/ContentItem-efb17665-691c-45d5-a60c-5301333cbb0c.html',
  142. 'md5': '11959b4e44fa74de47011b5799490adf',
  143. 'info_dict': {
  144. 'id': 'efb17665-691c-45d5-a60c-5301333cbb0c',
  145. 'ext': 'mp4',
  146. 'title': 'TG1 ore 20:00 del 03/11/2016',
  147. 'thumbnail': r're:^https?://.*\.jpg$',
  148. 'upload_date': '20161103',
  149. 'description': r're:^[A-Za-z]+'
  150. }
  151. }, {
  152. # drawMediaRaiTV test case
  153. 'url': 'http://www.report.rai.it/dl/Report/puntata/ContentItem-0c7a664b-d0f4-4b2c-8835-3f82e46f433e.html',
  154. 'md5': '2dd727e61114e1ee9c47f0da6914e178',
  155. 'info_dict': {
  156. 'id': '59d69d28-6bb6-409d-a4b5-ed44096560af',
  157. 'ext': 'mp4',
  158. 'title': 'Il pacco',
  159. 'description': 'md5:4b1afae1364115ce5d78ed83cd2e5b3a',
  160. 'upload_date': '20141221',
  161. },
  162. }, {
  163. # Direct relinker URL
  164. 'url': 'http://www.rai.tv/dl/RaiTV/dirette/PublishingBlock-1912dbbf-3f96-44c3-b4cf-523681fbacbc.html?channel=EuroNews',
  165. # HDS live stream, MD5 is unstable
  166. 'info_dict': {
  167. 'id': '1912dbbf-3f96-44c3-b4cf-523681fbacbc',
  168. 'ext': 'flv',
  169. 'title': 'EuroNews',
  170. },
  171. }, {
  172. # Embedded content item ID
  173. 'url': 'http://www.tg1.rai.it/dl/tg1/2010/edizioni/ContentSet-9b6e0cba-4bef-4aef-8cf0-9f7f665b7dfb-tg1.html?item=undefined',
  174. 'info_dict': {
  175. 'id': 'd80d4b70-3812-4501-a888-92edec729f00',
  176. 'ext': 'mp4',
  177. 'title': r're:TG1 ore \d{2}:\d{2} del \d{2}/\d{2}/\d{4}',
  178. 'upload_date': r're:\d{8}',
  179. 'description': r're:.+',
  180. },
  181. }, {
  182. 'url': 'http://www.rainews.it/dl/rainews/live/ContentItem-3156f2f2-dc70-4953-8e2f-70d7489d4ce9.html',
  183. # HDS live stream, MD5 is unstable
  184. 'info_dict': {
  185. 'id': '3156f2f2-dc70-4953-8e2f-70d7489d4ce9',
  186. 'ext': 'mp4',
  187. 'title': 'La diretta di Rainews24',
  188. },
  189. }]
  190. def _real_extract(self, url):
  191. video_id = self._match_id(url)
  192. webpage = self._download_webpage(url, video_id)
  193. iframe_url = self._search_regex(
  194. [r'<iframe[^>]+src="([^"]*/dl/[^"]+\?iframe\b[^"]*)"',
  195. r'drawMediaRaiTV\(["\'](.+?)["\']'],
  196. webpage, 'iframe', default=None)
  197. if iframe_url:
  198. if not iframe_url.startswith('http'):
  199. iframe_url = compat_urlparse.urljoin(url, iframe_url)
  200. return self.url_result(iframe_url)
  201. content_item_id = self._search_regex(
  202. r'initEdizione\((?P<q1>[\'"])ContentItem-(?P<content_id>[^\'"]+)(?P=q1)',
  203. webpage, 'content item ID', group='content_id', default=None)
  204. if content_item_id:
  205. return self._extract_from_content_id(content_item_id, url)
  206. try:
  207. return self._extract_from_content_id(video_id, url)
  208. except ExtractorError:
  209. # no media data, only direct relinker
  210. pass
  211. relinker_url = compat_urlparse.urljoin(url, self._search_regex(
  212. r'(?:var\s+videoURL|mediaInfo\.mediaUri)\s*=\s*(?P<q1>[\'"])(?P<url>(https?:)?//mediapolis\.rai\.it/relinker/relinkerServlet\.htm\?cont=\d+)(?P=q1)',
  213. webpage, 'relinker URL', group='url'))
  214. formats = self._extract_relinker_formats(relinker_url, video_id)
  215. self._sort_formats(formats)
  216. title = self._search_regex(
  217. r'var\s+videoTitolo\s*=\s*([\'"])(?P<title>[^\'"]+)\1',
  218. webpage, 'title', group='title', default=None) or self._og_search_title(webpage)
  219. return {
  220. 'id': video_id,
  221. 'title': title,
  222. 'formats': formats,
  223. }
  224. def _extract_from_content_id(self, content_id, url):
  225. media = self._download_json(
  226. 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-%s.html?json' % content_id,
  227. content_id, 'Downloading video JSON')
  228. thumbnails = []
  229. for image_type in ('image', 'image_medium', 'image_300'):
  230. thumbnail_url = media.get(image_type)
  231. if thumbnail_url:
  232. thumbnails.append({
  233. 'url': compat_urlparse.urljoin(url, thumbnail_url),
  234. })
  235. formats = []
  236. media_type = media['type']
  237. if 'Audio' in media_type:
  238. formats.append({
  239. 'format_id': media.get('formatoAudio'),
  240. 'url': media['audioUrl'],
  241. 'ext': media.get('formatoAudio'),
  242. })
  243. elif 'Video' in media_type:
  244. formats.extend(self._extract_relinker_formats(media['mediaUri'], content_id))
  245. self._sort_formats(formats)
  246. else:
  247. raise ExtractorError('not a media file')
  248. subtitles = {}
  249. captions = media.get('subtitlesUrl')
  250. if captions:
  251. STL_EXT = '.stl'
  252. SRT_EXT = '.srt'
  253. if captions.endswith(STL_EXT):
  254. captions = captions[:-len(STL_EXT)] + SRT_EXT
  255. subtitles['it'] = [{
  256. 'ext': 'srt',
  257. 'url': captions,
  258. }]
  259. return {
  260. 'id': content_id,
  261. 'title': media['name'],
  262. 'description': media.get('desc'),
  263. 'thumbnails': thumbnails,
  264. 'uploader': media.get('author'),
  265. 'upload_date': unified_strdate(media.get('date')),
  266. 'duration': parse_duration(media.get('length')),
  267. 'formats': formats,
  268. 'subtitles': subtitles,
  269. }