rai.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..compat import compat_urlparse
  4. from ..utils import (
  5. determine_ext,
  6. ExtractorError,
  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. headers = {}
  20. # TODO: rename --cn-verification-proxy
  21. cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
  22. if cn_verification_proxy:
  23. headers['Ytdl-request-proxy'] = cn_verification_proxy
  24. relinker = self._download_xml(
  25. relinker_url, video_id,
  26. note='Downloading XML metadata for platform %s' % platform,
  27. transform_source=fix_xml_ampersands,
  28. query={'output': 45, 'pl': platform}, headers=headers)
  29. media_url = find_xpath_attr(relinker, './url', 'type', 'content').text
  30. if media_url == 'http://download.rai.it/video_no_available.mp4':
  31. self.raise_geo_restricted()
  32. ext = determine_ext(media_url)
  33. if (ext == 'm3u8' and platform != 'mon') or (ext == 'f4m' and platform != 'flash'):
  34. continue
  35. if ext == 'm3u8':
  36. formats.extend(self._extract_m3u8_formats(
  37. media_url, video_id, 'mp4', 'm3u8_native',
  38. m3u8_id='hls', fatal=False))
  39. elif ext == 'f4m':
  40. manifest_url = update_url_query(
  41. media_url.replace('manifest#live_hds.f4m', 'manifest.f4m'),
  42. {'hdcore': '3.7.0', 'plugin': 'aasp-3.7.0.39.44'})
  43. formats.extend(self._extract_f4m_formats(
  44. manifest_url, video_id, f4m_id='hds', fatal=False))
  45. else:
  46. bitrate = int_or_none(xpath_text(relinker, 'bitrate'))
  47. formats.append({
  48. 'url': media_url,
  49. 'tbr': bitrate if bitrate > 0 else None,
  50. 'format_id': 'http-%d' % bitrate if bitrate > 0 else 'http',
  51. })
  52. return formats
  53. def _extract_from_content_id(self, content_id, base_url):
  54. media = self._download_json(
  55. 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-%s.html?json' % content_id,
  56. content_id, 'Downloading video JSON')
  57. thumbnails = []
  58. for image_type in ('image', 'image_medium', 'image_300'):
  59. thumbnail_url = media.get(image_type)
  60. if thumbnail_url:
  61. thumbnails.append({
  62. 'url': compat_urlparse.urljoin(base_url, thumbnail_url),
  63. })
  64. formats = []
  65. media_type = media['type']
  66. if 'Audio' in media_type:
  67. formats.append({
  68. 'format_id': media.get('formatoAudio'),
  69. 'url': media['audioUrl'],
  70. 'ext': media.get('formatoAudio'),
  71. })
  72. elif 'Video' in media_type:
  73. formats.extend(self._extract_relinker_formats(media['mediaUri'], content_id))
  74. self._sort_formats(formats)
  75. else:
  76. raise ExtractorError('not a media file')
  77. subtitles = {}
  78. captions = media.get('subtitlesUrl')
  79. if captions:
  80. STL_EXT = '.stl'
  81. SRT_EXT = '.srt'
  82. if captions.endswith(STL_EXT):
  83. captions = captions[:-len(STL_EXT)] + SRT_EXT
  84. subtitles['it'] = [{
  85. 'ext': 'srt',
  86. 'url': captions,
  87. }]
  88. return {
  89. 'id': content_id,
  90. 'title': media['name'],
  91. 'description': media.get('desc'),
  92. 'thumbnails': thumbnails,
  93. 'uploader': media.get('author'),
  94. 'upload_date': unified_strdate(media.get('date')),
  95. 'duration': parse_duration(media.get('length')),
  96. 'formats': formats,
  97. 'subtitles': subtitles,
  98. }
  99. class RaiTVIE(RaiBaseIE):
  100. _VALID_URL = r'https?://(?:.+?\.)?(?:rai\.it|rai\.tv|rainews\.it)/dl/(?:[^/]+/)+(?:media|ondemand)/.+?-(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})(?:-.+?)?\.html'
  101. _TESTS = [
  102. {
  103. 'url': 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-cb27157f-9dd0-4aee-b788-b1f67643a391.html',
  104. 'md5': '8970abf8caf8aef4696e7b1f2adfc696',
  105. 'info_dict': {
  106. 'id': 'cb27157f-9dd0-4aee-b788-b1f67643a391',
  107. 'ext': 'mp4',
  108. 'title': 'Report del 07/04/2014',
  109. 'description': 'md5:f27c544694cacb46a078db84ec35d2d9',
  110. 'upload_date': '20140407',
  111. 'duration': 6160,
  112. 'thumbnail': 're:^https?://.*\.jpg$',
  113. }
  114. },
  115. {
  116. # no m3u8 stream
  117. 'url': 'http://www.raisport.rai.it/dl/raiSport/media/rassegna-stampa-04a9f4bd-b563-40cf-82a6-aad3529cb4a9.html',
  118. # HDS download, MD5 is unstable
  119. 'info_dict': {
  120. 'id': '04a9f4bd-b563-40cf-82a6-aad3529cb4a9',
  121. 'ext': 'flv',
  122. 'title': 'TG PRIMO TEMPO',
  123. 'upload_date': '20140612',
  124. 'duration': 1758,
  125. 'thumbnail': 're:^https?://.*\.jpg$',
  126. },
  127. 'skip': 'Geo-restricted to Italy',
  128. },
  129. {
  130. 'url': 'http://www.rainews.it/dl/rainews/media/state-of-the-net-Antonella-La-Carpia-regole-virali-7aafdea9-0e5d-49d5-88a6-7e65da67ae13.html',
  131. 'md5': '35cf7c229f22eeef43e48b5cf923bef0',
  132. 'info_dict': {
  133. 'id': '7aafdea9-0e5d-49d5-88a6-7e65da67ae13',
  134. 'ext': 'mp4',
  135. 'title': 'State of the Net, Antonella La Carpia: regole virali',
  136. 'description': 'md5:b0ba04a324126903e3da7763272ae63c',
  137. 'upload_date': '20140613',
  138. },
  139. 'skip': 'Error 404',
  140. },
  141. {
  142. 'url': 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-b4a49761-e0cc-4b14-8736-2729f6f73132-tg2.html',
  143. 'info_dict': {
  144. 'id': 'b4a49761-e0cc-4b14-8736-2729f6f73132',
  145. 'ext': 'mp4',
  146. 'title': 'Alluvione in Sardegna e dissesto idrogeologico',
  147. 'description': 'Edizione delle ore 20:30 ',
  148. },
  149. 'skip': 'invalid urls',
  150. },
  151. {
  152. 'url': 'http://www.ilcandidato.rai.it/dl/ray/media/Il-Candidato---Primo-episodio-Le-Primarie-28e5525a-b495-45e8-a7c3-bc48ba45d2b6.html',
  153. 'md5': 'e57493e1cb8bc7c564663f363b171847',
  154. 'info_dict': {
  155. 'id': '28e5525a-b495-45e8-a7c3-bc48ba45d2b6',
  156. 'ext': 'mp4',
  157. 'title': 'Il Candidato - Primo episodio: "Le Primarie"',
  158. 'description': 'md5:364b604f7db50594678f483353164fb8',
  159. 'upload_date': '20140923',
  160. 'duration': 386,
  161. 'thumbnail': 're:^https?://.*\.jpg$',
  162. }
  163. },
  164. ]
  165. def _real_extract(self, url):
  166. video_id = self._match_id(url)
  167. return self._extract_from_content_id(video_id, url)
  168. class RaiIE(RaiBaseIE):
  169. _VALID_URL = r'https?://(?:.+?\.)?(?:rai\.it|rai\.tv|rainews\.it)/dl/.+?-(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})(?:-.+?)?\.html'
  170. _TESTS = [
  171. {
  172. 'url': 'http://www.report.rai.it/dl/Report/puntata/ContentItem-0c7a664b-d0f4-4b2c-8835-3f82e46f433e.html',
  173. 'md5': '2dd727e61114e1ee9c47f0da6914e178',
  174. 'info_dict': {
  175. 'id': '59d69d28-6bb6-409d-a4b5-ed44096560af',
  176. 'ext': 'mp4',
  177. 'title': 'Il pacco',
  178. 'description': 'md5:4b1afae1364115ce5d78ed83cd2e5b3a',
  179. 'upload_date': '20141221',
  180. },
  181. },
  182. {
  183. # Direct relinker URL
  184. 'url': 'http://www.rai.tv/dl/RaiTV/dirette/PublishingBlock-1912dbbf-3f96-44c3-b4cf-523681fbacbc.html?channel=EuroNews',
  185. # HDS live stream, MD5 is unstable
  186. 'info_dict': {
  187. 'id': '1912dbbf-3f96-44c3-b4cf-523681fbacbc',
  188. 'ext': 'flv',
  189. 'title': 'EuroNews',
  190. },
  191. 'skip': 'Geo-restricted to Italy',
  192. },
  193. {
  194. # Embedded content item ID
  195. 'url': 'http://www.tg1.rai.it/dl/tg1/2010/edizioni/ContentSet-9b6e0cba-4bef-4aef-8cf0-9f7f665b7dfb-tg1.html?item=undefined',
  196. 'md5': '84c1135ce960e8822ae63cec34441d63',
  197. 'info_dict': {
  198. 'id': '0960e765-62c8-474a-ac4b-7eb3e2be39c8',
  199. 'ext': 'mp4',
  200. 'title': 'TG1 ore 20:00 del 02/07/2016',
  201. 'upload_date': '20160702',
  202. },
  203. },
  204. ]
  205. @classmethod
  206. def suitable(cls, url):
  207. return False if RaiTVIE.suitable(url) else super(RaiIE, cls).suitable(url)
  208. def _real_extract(self, url):
  209. video_id = self._match_id(url)
  210. webpage = self._download_webpage(url, video_id)
  211. iframe_url = self._search_regex(
  212. [r'<iframe[^>]+src="([^"]*/dl/[^"]+\?iframe\b[^"]*)"',
  213. r'drawMediaRaiTV\(["\'](.+?)["\']'],
  214. webpage, 'iframe', default=None)
  215. if iframe_url:
  216. if not iframe_url.startswith('http'):
  217. iframe_url = compat_urlparse.urljoin(url, iframe_url)
  218. return self.url_result(iframe_url)
  219. content_item_id = self._search_regex(
  220. r'initEdizione\((?P<q1>[\'"])ContentItem-(?P<content_id>[^\'"]+)(?P=q1)',
  221. webpage, 'content item ID', group='content_id', default=None)
  222. if content_item_id:
  223. return self._extract_from_content_id(content_item_id, url)
  224. relinker_url = compat_urlparse.urljoin(url, self._search_regex(
  225. r'var\s+videoURL\s*=\s*(?P<q1>[\'"])(?P<url>(https?:)?//mediapolis\.rai\.it/relinker/relinkerServlet\.htm\?cont=\d+)(?P=q1)',
  226. webpage, 'relinker URL', group='url'))
  227. formats = self._extract_relinker_formats(relinker_url, video_id)
  228. self._sort_formats(formats)
  229. title = self._search_regex(
  230. r'var\s+videoTitolo\s*=\s*([\'"])(?P<title>[^\'"]+)\1',
  231. webpage, 'title', group='title', default=None) or self._og_search_title(webpage)
  232. return {
  233. 'id': video_id,
  234. 'title': title,
  235. 'formats': formats,
  236. }