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.

210 lines
8.1 KiB

11 years ago
11 years ago
10 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_urllib_parse,
  6. compat_urlparse,
  7. )
  8. from ..utils import (
  9. ExtractorError,
  10. determine_ext,
  11. parse_duration,
  12. unified_strdate,
  13. int_or_none,
  14. xpath_text,
  15. )
  16. class RaiTVIE(InfoExtractor):
  17. _VALID_URL = r'http://(?:.+?\.)?(?: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'
  18. _TESTS = [
  19. {
  20. 'url': 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-cb27157f-9dd0-4aee-b788-b1f67643a391.html',
  21. 'md5': '96382709b61dd64a6b88e0f791e6df4c',
  22. 'info_dict': {
  23. 'id': 'cb27157f-9dd0-4aee-b788-b1f67643a391',
  24. 'ext': 'flv',
  25. 'title': 'Report del 07/04/2014',
  26. 'description': 'md5:f27c544694cacb46a078db84ec35d2d9',
  27. 'upload_date': '20140407',
  28. 'duration': 6160,
  29. }
  30. },
  31. {
  32. 'url': 'http://www.raisport.rai.it/dl/raiSport/media/rassegna-stampa-04a9f4bd-b563-40cf-82a6-aad3529cb4a9.html',
  33. 'md5': 'd9751b78eac9710d62c2447b224dea39',
  34. 'info_dict': {
  35. 'id': '04a9f4bd-b563-40cf-82a6-aad3529cb4a9',
  36. 'ext': 'flv',
  37. 'title': 'TG PRIMO TEMPO',
  38. 'upload_date': '20140612',
  39. 'duration': 1758,
  40. },
  41. },
  42. {
  43. 'url': 'http://www.rainews.it/dl/rainews/media/state-of-the-net-Antonella-La-Carpia-regole-virali-7aafdea9-0e5d-49d5-88a6-7e65da67ae13.html',
  44. 'md5': '35cf7c229f22eeef43e48b5cf923bef0',
  45. 'info_dict': {
  46. 'id': '7aafdea9-0e5d-49d5-88a6-7e65da67ae13',
  47. 'ext': 'mp4',
  48. 'title': 'State of the Net, Antonella La Carpia: regole virali',
  49. 'description': 'md5:b0ba04a324126903e3da7763272ae63c',
  50. 'upload_date': '20140613',
  51. },
  52. 'skip': 'Error 404',
  53. },
  54. {
  55. 'url': 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-b4a49761-e0cc-4b14-8736-2729f6f73132-tg2.html',
  56. 'info_dict': {
  57. 'id': 'b4a49761-e0cc-4b14-8736-2729f6f73132',
  58. 'ext': 'mp4',
  59. 'title': 'Alluvione in Sardegna e dissesto idrogeologico',
  60. 'description': 'Edizione delle ore 20:30 ',
  61. },
  62. 'skip': 'invalid urls',
  63. },
  64. {
  65. 'url': 'http://www.ilcandidato.rai.it/dl/ray/media/Il-Candidato---Primo-episodio-Le-Primarie-28e5525a-b495-45e8-a7c3-bc48ba45d2b6.html',
  66. 'md5': '496ab63e420574447f70d02578333437',
  67. 'info_dict': {
  68. 'id': '28e5525a-b495-45e8-a7c3-bc48ba45d2b6',
  69. 'ext': 'flv',
  70. 'title': 'Il Candidato - Primo episodio: "Le Primarie"',
  71. 'description': 'md5:364b604f7db50594678f483353164fb8',
  72. 'upload_date': '20140923',
  73. 'duration': 386,
  74. }
  75. },
  76. ]
  77. def _real_extract(self, url):
  78. video_id = self._match_id(url)
  79. media = self._download_json(
  80. 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-%s.html?json' % video_id,
  81. video_id, 'Downloading video JSON')
  82. thumbnails = []
  83. for image_type in ('image', 'image_medium', 'image_300'):
  84. thumbnail_url = media.get(image_type)
  85. if thumbnail_url:
  86. thumbnails.append({
  87. 'url': thumbnail_url,
  88. })
  89. subtitles = []
  90. formats = []
  91. media_type = media['type']
  92. if 'Audio' in media_type:
  93. formats.append({
  94. 'format_id': media.get('formatoAudio'),
  95. 'url': media['audioUrl'],
  96. 'ext': media.get('formatoAudio'),
  97. })
  98. elif 'Video' in media_type:
  99. def fix_xml(xml):
  100. return xml.replace(' tag elementi', '').replace('>/', '</')
  101. relinker = self._download_xml(
  102. media['mediaUri'] + '&output=43', video_id, transform_source=fix_xml)
  103. has_subtitle = False
  104. for element in relinker.findall('element'):
  105. media_url = xpath_text(element, 'url')
  106. ext = determine_ext(media_url)
  107. content_type = xpath_text(element, 'content-type')
  108. if ext == 'm3u8':
  109. m3u8_formats = self._extract_m3u8_formats(
  110. media_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls',
  111. fatal=None)
  112. if m3u8_formats:
  113. formats.extend(m3u8_formats)
  114. elif ext == 'f4m':
  115. f4m_formats = self._extract_f4m_formats(
  116. media_url + '?hdcore=3.7.0&plugin=aasp-3.7.0.39.44',
  117. video_id, f4m_id='hds', fatal=None)
  118. if f4m_formats:
  119. formats.extend(f4m_formats)
  120. elif ext == 'stl':
  121. has_subtitle = True
  122. elif content_type.startswith('video/'):
  123. bitrate = int_or_none(xpath_text(element, 'bitrate'))
  124. formats.append({
  125. 'url': media_url,
  126. 'tbr': bitrate if bitrate > 0 else None,
  127. 'format_id': 'http-%d' % bitrate if bitrate > 0 else 'http',
  128. })
  129. elif content_type.startswith('image/'):
  130. thumbnails.append({
  131. 'url': media_url,
  132. })
  133. self._sort_formats(formats)
  134. if has_subtitle:
  135. webpage = self._download_webpage(url, video_id)
  136. subtitles = self._get_subtitles(video_id, webpage)
  137. else:
  138. raise ExtractorError('not a media file')
  139. return {
  140. 'id': video_id,
  141. 'title': media['name'],
  142. 'description': media.get('desc'),
  143. 'thumbnails': thumbnails,
  144. 'uploader': media.get('author'),
  145. 'upload_date': unified_strdate(media.get('date')),
  146. 'duration': parse_duration(media.get('length')),
  147. 'formats': formats,
  148. 'subtitles': subtitles,
  149. }
  150. def _get_subtitles(self, video_id, webpage):
  151. subtitles = {}
  152. m = re.search(r'<meta name="closedcaption" content="(?P<captions>[^"]+)"', webpage)
  153. if m:
  154. captions = m.group('captions')
  155. STL_EXT = '.stl'
  156. SRT_EXT = '.srt'
  157. if captions.endswith(STL_EXT):
  158. captions = captions[:-len(STL_EXT)] + SRT_EXT
  159. subtitles['it'] = [{
  160. 'ext': 'srt',
  161. 'url': 'http://www.rai.tv%s' % compat_urllib_parse.quote(captions),
  162. }]
  163. return subtitles
  164. class RaiIE(InfoExtractor):
  165. _VALID_URL = r'http://(?:.+?\.)?(?: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': 'e0e7a8a131e249d1aa0ebf270d1d8db7',
  170. 'info_dict': {
  171. 'id': '59d69d28-6bb6-409d-a4b5-ed44096560af',
  172. 'ext': 'flv',
  173. 'title': 'Il pacco',
  174. 'description': 'md5:4b1afae1364115ce5d78ed83cd2e5b3a',
  175. 'upload_date': '20141221',
  176. },
  177. }
  178. ]
  179. @classmethod
  180. def suitable(cls, url):
  181. return False if RaiTVIE.suitable(url) else super(RaiIE, cls).suitable(url)
  182. def _real_extract(self, url):
  183. video_id = self._match_id(url)
  184. webpage = self._download_webpage(url, video_id)
  185. iframe_url = self._search_regex(
  186. [r'<iframe[^>]+src="([^"]*/dl/[^"]+\?iframe\b[^"]*)"',
  187. r'drawMediaRaiTV\(["\'](.+?)["\']'],
  188. webpage, 'iframe')
  189. if not iframe_url.startswith('http'):
  190. iframe_url = compat_urlparse.urljoin(url, iframe_url)
  191. return self.url_result(iframe_url)