rai.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 RaiTVIE(InfoExtractor):
  16. _VALID_URL = r'https?://(?:.+?\.)?(?:rai\.it|rai\.tv|rainews\.it)/dl/(?:[^/]+/)+media/.+?-(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})(?:-.+?)?\.html'
  17. _TESTS = [
  18. {
  19. 'url': 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-cb27157f-9dd0-4aee-b788-b1f67643a391.html',
  20. 'md5': '8970abf8caf8aef4696e7b1f2adfc696',
  21. 'info_dict': {
  22. 'id': 'cb27157f-9dd0-4aee-b788-b1f67643a391',
  23. 'ext': 'mp4',
  24. 'title': 'Report del 07/04/2014',
  25. 'description': 'md5:f27c544694cacb46a078db84ec35d2d9',
  26. 'upload_date': '20140407',
  27. 'duration': 6160,
  28. 'thumbnail': 're:^https?://.*\.jpg$',
  29. }
  30. },
  31. {
  32. # no m3u8 stream
  33. 'url': 'http://www.raisport.rai.it/dl/raiSport/media/rassegna-stampa-04a9f4bd-b563-40cf-82a6-aad3529cb4a9.html',
  34. # HDS download, MD5 is unstable
  35. 'info_dict': {
  36. 'id': '04a9f4bd-b563-40cf-82a6-aad3529cb4a9',
  37. 'ext': 'flv',
  38. 'title': 'TG PRIMO TEMPO',
  39. 'upload_date': '20140612',
  40. 'duration': 1758,
  41. 'thumbnail': 're:^https?://.*\.jpg$',
  42. },
  43. 'skip': 'Geo-restricted to Italy',
  44. },
  45. {
  46. 'url': 'http://www.rainews.it/dl/rainews/media/state-of-the-net-Antonella-La-Carpia-regole-virali-7aafdea9-0e5d-49d5-88a6-7e65da67ae13.html',
  47. 'md5': '35cf7c229f22eeef43e48b5cf923bef0',
  48. 'info_dict': {
  49. 'id': '7aafdea9-0e5d-49d5-88a6-7e65da67ae13',
  50. 'ext': 'mp4',
  51. 'title': 'State of the Net, Antonella La Carpia: regole virali',
  52. 'description': 'md5:b0ba04a324126903e3da7763272ae63c',
  53. 'upload_date': '20140613',
  54. },
  55. 'skip': 'Error 404',
  56. },
  57. {
  58. 'url': 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-b4a49761-e0cc-4b14-8736-2729f6f73132-tg2.html',
  59. 'info_dict': {
  60. 'id': 'b4a49761-e0cc-4b14-8736-2729f6f73132',
  61. 'ext': 'mp4',
  62. 'title': 'Alluvione in Sardegna e dissesto idrogeologico',
  63. 'description': 'Edizione delle ore 20:30 ',
  64. },
  65. 'skip': 'invalid urls',
  66. },
  67. {
  68. 'url': 'http://www.ilcandidato.rai.it/dl/ray/media/Il-Candidato---Primo-episodio-Le-Primarie-28e5525a-b495-45e8-a7c3-bc48ba45d2b6.html',
  69. 'md5': 'e57493e1cb8bc7c564663f363b171847',
  70. 'info_dict': {
  71. 'id': '28e5525a-b495-45e8-a7c3-bc48ba45d2b6',
  72. 'ext': 'mp4',
  73. 'title': 'Il Candidato - Primo episodio: "Le Primarie"',
  74. 'description': 'md5:364b604f7db50594678f483353164fb8',
  75. 'upload_date': '20140923',
  76. 'duration': 386,
  77. 'thumbnail': 're:^https?://.*\.jpg$',
  78. }
  79. },
  80. ]
  81. def _real_extract(self, url):
  82. video_id = self._match_id(url)
  83. media = self._download_json(
  84. 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-%s.html?json' % video_id,
  85. video_id, 'Downloading video JSON')
  86. thumbnails = []
  87. for image_type in ('image', 'image_medium', 'image_300'):
  88. thumbnail_url = media.get(image_type)
  89. if thumbnail_url:
  90. thumbnails.append({
  91. 'url': compat_urlparse.urljoin(url, thumbnail_url),
  92. })
  93. formats = []
  94. media_type = media['type']
  95. if 'Audio' in media_type:
  96. formats.append({
  97. 'format_id': media.get('formatoAudio'),
  98. 'url': media['audioUrl'],
  99. 'ext': media.get('formatoAudio'),
  100. })
  101. elif 'Video' in media_type:
  102. for platform in ('mon', 'flash', 'native'):
  103. headers = {}
  104. # TODO: rename --cn-verification-proxy
  105. cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
  106. if cn_verification_proxy:
  107. headers['Ytdl-request-proxy'] = cn_verification_proxy
  108. relinker = self._download_xml(
  109. media['mediaUri'], video_id,
  110. note='Downloading XML metadata for platform %s' % platform,
  111. transform_source=fix_xml_ampersands,
  112. query={'output': 45, 'pl': platform}, headers=headers)
  113. media_url = find_xpath_attr(relinker, './url', 'type', 'content').text
  114. if media_url == 'http://download.rai.it/video_no_available.mp4':
  115. self.raise_geo_restricted()
  116. ext = determine_ext(media_url)
  117. if (platform == 'mon' and ext != 'm3u8') or (platform == 'flash' and ext != 'f4m'):
  118. continue
  119. if ext == 'm3u8':
  120. formats.extend(self._extract_m3u8_formats(
  121. media_url, video_id, 'mp4', 'm3u8_native',
  122. m3u8_id='hls', fatal=False))
  123. elif ext == 'f4m':
  124. manifest_url = update_url_query(
  125. media_url, {'hdcore': '3.7.0', 'plugin': 'aasp-3.7.0.39.44'})
  126. formats.extend(self._extract_f4m_formats(
  127. manifest_url, video_id, f4m_id='hds', fatal=False))
  128. else:
  129. bitrate = int_or_none(xpath_text(relinker, 'bitrate'))
  130. formats.append({
  131. 'url': media_url,
  132. 'tbr': bitrate if bitrate > 0 else None,
  133. 'format_id': 'http-%d' % bitrate if bitrate > 0 else 'http',
  134. })
  135. self._sort_formats(formats)
  136. else:
  137. raise ExtractorError('not a media file')
  138. subtitles = {}
  139. captions = media.get('subtitlesUrl')
  140. if captions:
  141. STL_EXT = '.stl'
  142. SRT_EXT = '.srt'
  143. if captions.endswith(STL_EXT):
  144. captions = captions[:-len(STL_EXT)] + SRT_EXT
  145. subtitles['it'] = [{
  146. 'ext': 'srt',
  147. 'url': captions,
  148. }]
  149. return {
  150. 'id': video_id,
  151. 'title': media['name'],
  152. 'description': media.get('desc'),
  153. 'thumbnails': thumbnails,
  154. 'uploader': media.get('author'),
  155. 'upload_date': unified_strdate(media.get('date')),
  156. 'duration': parse_duration(media.get('length')),
  157. 'formats': formats,
  158. 'subtitles': subtitles,
  159. }
  160. class RaiIE(InfoExtractor):
  161. _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'
  162. _TESTS = [
  163. {
  164. 'url': 'http://www.report.rai.it/dl/Report/puntata/ContentItem-0c7a664b-d0f4-4b2c-8835-3f82e46f433e.html',
  165. 'md5': '2dd727e61114e1ee9c47f0da6914e178',
  166. 'info_dict': {
  167. 'id': '59d69d28-6bb6-409d-a4b5-ed44096560af',
  168. 'ext': 'mp4',
  169. 'title': 'Il pacco',
  170. 'description': 'md5:4b1afae1364115ce5d78ed83cd2e5b3a',
  171. 'upload_date': '20141221',
  172. },
  173. }
  174. ]
  175. @classmethod
  176. def suitable(cls, url):
  177. return False if RaiTVIE.suitable(url) else super(RaiIE, cls).suitable(url)
  178. def _real_extract(self, url):
  179. video_id = self._match_id(url)
  180. webpage = self._download_webpage(url, video_id)
  181. iframe_url = self._search_regex(
  182. [r'<iframe[^>]+src="([^"]*/dl/[^"]+\?iframe\b[^"]*)"',
  183. r'drawMediaRaiTV\(["\'](.+?)["\']'],
  184. webpage, 'iframe')
  185. if not iframe_url.startswith('http'):
  186. iframe_url = compat_urlparse.urljoin(url, iframe_url)
  187. return self.url_result(iframe_url)