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.

242 lines
9.4 KiB

10 years ago
10 years ago
  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. class RaiTVIE(RaiBaseIE):
  54. _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'
  55. _TESTS = [
  56. {
  57. 'url': 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-cb27157f-9dd0-4aee-b788-b1f67643a391.html',
  58. 'md5': '8970abf8caf8aef4696e7b1f2adfc696',
  59. 'info_dict': {
  60. 'id': 'cb27157f-9dd0-4aee-b788-b1f67643a391',
  61. 'ext': 'mp4',
  62. 'title': 'Report del 07/04/2014',
  63. 'description': 'md5:f27c544694cacb46a078db84ec35d2d9',
  64. 'upload_date': '20140407',
  65. 'duration': 6160,
  66. 'thumbnail': 're:^https?://.*\.jpg$',
  67. }
  68. },
  69. {
  70. # no m3u8 stream
  71. 'url': 'http://www.raisport.rai.it/dl/raiSport/media/rassegna-stampa-04a9f4bd-b563-40cf-82a6-aad3529cb4a9.html',
  72. # HDS download, MD5 is unstable
  73. 'info_dict': {
  74. 'id': '04a9f4bd-b563-40cf-82a6-aad3529cb4a9',
  75. 'ext': 'flv',
  76. 'title': 'TG PRIMO TEMPO',
  77. 'upload_date': '20140612',
  78. 'duration': 1758,
  79. 'thumbnail': 're:^https?://.*\.jpg$',
  80. },
  81. 'skip': 'Geo-restricted to Italy',
  82. },
  83. {
  84. 'url': 'http://www.rainews.it/dl/rainews/media/state-of-the-net-Antonella-La-Carpia-regole-virali-7aafdea9-0e5d-49d5-88a6-7e65da67ae13.html',
  85. 'md5': '35cf7c229f22eeef43e48b5cf923bef0',
  86. 'info_dict': {
  87. 'id': '7aafdea9-0e5d-49d5-88a6-7e65da67ae13',
  88. 'ext': 'mp4',
  89. 'title': 'State of the Net, Antonella La Carpia: regole virali',
  90. 'description': 'md5:b0ba04a324126903e3da7763272ae63c',
  91. 'upload_date': '20140613',
  92. },
  93. 'skip': 'Error 404',
  94. },
  95. {
  96. 'url': 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-b4a49761-e0cc-4b14-8736-2729f6f73132-tg2.html',
  97. 'info_dict': {
  98. 'id': 'b4a49761-e0cc-4b14-8736-2729f6f73132',
  99. 'ext': 'mp4',
  100. 'title': 'Alluvione in Sardegna e dissesto idrogeologico',
  101. 'description': 'Edizione delle ore 20:30 ',
  102. },
  103. 'skip': 'invalid urls',
  104. },
  105. {
  106. 'url': 'http://www.ilcandidato.rai.it/dl/ray/media/Il-Candidato---Primo-episodio-Le-Primarie-28e5525a-b495-45e8-a7c3-bc48ba45d2b6.html',
  107. 'md5': 'e57493e1cb8bc7c564663f363b171847',
  108. 'info_dict': {
  109. 'id': '28e5525a-b495-45e8-a7c3-bc48ba45d2b6',
  110. 'ext': 'mp4',
  111. 'title': 'Il Candidato - Primo episodio: "Le Primarie"',
  112. 'description': 'md5:364b604f7db50594678f483353164fb8',
  113. 'upload_date': '20140923',
  114. 'duration': 386,
  115. 'thumbnail': 're:^https?://.*\.jpg$',
  116. }
  117. },
  118. ]
  119. def _real_extract(self, url):
  120. video_id = self._match_id(url)
  121. media = self._download_json(
  122. 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-%s.html?json' % video_id,
  123. video_id, 'Downloading video JSON')
  124. thumbnails = []
  125. for image_type in ('image', 'image_medium', 'image_300'):
  126. thumbnail_url = media.get(image_type)
  127. if thumbnail_url:
  128. thumbnails.append({
  129. 'url': compat_urlparse.urljoin(url, thumbnail_url),
  130. })
  131. formats = []
  132. media_type = media['type']
  133. if 'Audio' in media_type:
  134. formats.append({
  135. 'format_id': media.get('formatoAudio'),
  136. 'url': media['audioUrl'],
  137. 'ext': media.get('formatoAudio'),
  138. })
  139. elif 'Video' in media_type:
  140. formats.extend(self._extract_relinker_formats(media['mediaUri'], video_id))
  141. self._sort_formats(formats)
  142. else:
  143. raise ExtractorError('not a media file')
  144. subtitles = {}
  145. captions = media.get('subtitlesUrl')
  146. if captions:
  147. STL_EXT = '.stl'
  148. SRT_EXT = '.srt'
  149. if captions.endswith(STL_EXT):
  150. captions = captions[:-len(STL_EXT)] + SRT_EXT
  151. subtitles['it'] = [{
  152. 'ext': 'srt',
  153. 'url': captions,
  154. }]
  155. return {
  156. 'id': video_id,
  157. 'title': media['name'],
  158. 'description': media.get('desc'),
  159. 'thumbnails': thumbnails,
  160. 'uploader': media.get('author'),
  161. 'upload_date': unified_strdate(media.get('date')),
  162. 'duration': parse_duration(media.get('length')),
  163. 'formats': formats,
  164. 'subtitles': subtitles,
  165. }
  166. class RaiIE(RaiBaseIE):
  167. _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'
  168. _TESTS = [
  169. {
  170. 'url': 'http://www.report.rai.it/dl/Report/puntata/ContentItem-0c7a664b-d0f4-4b2c-8835-3f82e46f433e.html',
  171. 'md5': '2dd727e61114e1ee9c47f0da6914e178',
  172. 'info_dict': {
  173. 'id': '59d69d28-6bb6-409d-a4b5-ed44096560af',
  174. 'ext': 'mp4',
  175. 'title': 'Il pacco',
  176. 'description': 'md5:4b1afae1364115ce5d78ed83cd2e5b3a',
  177. 'upload_date': '20141221',
  178. },
  179. },
  180. {
  181. 'url': 'http://www.rai.tv/dl/RaiTV/dirette/PublishingBlock-1912dbbf-3f96-44c3-b4cf-523681fbacbc.html?channel=EuroNews',
  182. # HDS live stream, MD5 is unstable
  183. 'info_dict': {
  184. 'id': '1912dbbf-3f96-44c3-b4cf-523681fbacbc',
  185. 'ext': 'flv',
  186. 'title': 'EuroNews',
  187. },
  188. 'skip': 'Geo-restricted to Italy',
  189. }
  190. ]
  191. @classmethod
  192. def suitable(cls, url):
  193. return False if RaiTVIE.suitable(url) else super(RaiIE, cls).suitable(url)
  194. def _real_extract(self, url):
  195. video_id = self._match_id(url)
  196. webpage = self._download_webpage(url, video_id)
  197. iframe_url = self._search_regex(
  198. [r'<iframe[^>]+src="([^"]*/dl/[^"]+\?iframe\b[^"]*)"',
  199. r'drawMediaRaiTV\(["\'](.+?)["\']'],
  200. webpage, 'iframe', default=None)
  201. if iframe_url:
  202. if not iframe_url.startswith('http'):
  203. iframe_url = compat_urlparse.urljoin(url, iframe_url)
  204. return self.url_result(iframe_url)
  205. relinker_url = compat_urlparse.urljoin(url, self._search_regex(
  206. r'var\s+videoURL\s*=\s*(?P<q1>[\'"])(?P<url>(https?:)?//mediapolis\.rai\.it/relinker/relinkerServlet\.htm\?cont=\d+)(?P=q1)',
  207. webpage, 'relinker URL', group='url'))
  208. formats = self._extract_relinker_formats(relinker_url, video_id)
  209. self._sort_formats(formats)
  210. title = self._search_regex(
  211. r'var\s+videoTitolo\s*=\s*([\'"])(?P<title>[^\'"]+)\1',
  212. webpage, 'title', group='title', default=None) or self._og_search_title(webpage)
  213. return {
  214. 'id': video_id,
  215. 'title': title,
  216. 'formats': formats,
  217. }