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.

267 lines
10 KiB

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