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.

207 lines
7.9 KiB

10 years ago
10 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'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'
  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',
  103. video_id, transform_source=fix_xml)
  104. has_subtitle = False
  105. for element in relinker.findall('element'):
  106. media_url = xpath_text(element, 'url')
  107. ext = determine_ext(media_url)
  108. content_type = xpath_text(element, 'content-type')
  109. if ext == 'm3u8':
  110. formats.extend(self._extract_m3u8_formats(
  111. media_url, video_id, 'mp4', 'm3u8_native',
  112. m3u8_id='hls', fatal=False))
  113. elif ext == 'f4m':
  114. formats.extend(self._extract_f4m_formats(
  115. media_url + '?hdcore=3.7.0&plugin=aasp-3.7.0.39.44',
  116. video_id, f4m_id='hds', fatal=False))
  117. elif ext == 'stl':
  118. has_subtitle = True
  119. elif content_type.startswith('video/'):
  120. bitrate = int_or_none(xpath_text(element, 'bitrate'))
  121. formats.append({
  122. 'url': media_url,
  123. 'tbr': bitrate if bitrate > 0 else None,
  124. 'format_id': 'http-%d' % bitrate if bitrate > 0 else 'http',
  125. })
  126. elif content_type.startswith('image/'):
  127. thumbnails.append({
  128. 'url': media_url,
  129. })
  130. self._sort_formats(formats)
  131. if has_subtitle:
  132. webpage = self._download_webpage(url, video_id)
  133. subtitles = self._get_subtitles(video_id, webpage)
  134. else:
  135. raise ExtractorError('not a media file')
  136. return {
  137. 'id': video_id,
  138. 'title': media['name'],
  139. 'description': media.get('desc'),
  140. 'thumbnails': thumbnails,
  141. 'uploader': media.get('author'),
  142. 'upload_date': unified_strdate(media.get('date')),
  143. 'duration': parse_duration(media.get('length')),
  144. 'formats': formats,
  145. 'subtitles': subtitles,
  146. }
  147. def _get_subtitles(self, video_id, webpage):
  148. subtitles = {}
  149. m = re.search(r'<meta name="closedcaption" content="(?P<captions>[^"]+)"', webpage)
  150. if m:
  151. captions = m.group('captions')
  152. STL_EXT = '.stl'
  153. SRT_EXT = '.srt'
  154. if captions.endswith(STL_EXT):
  155. captions = captions[:-len(STL_EXT)] + SRT_EXT
  156. subtitles['it'] = [{
  157. 'ext': 'srt',
  158. 'url': 'http://www.rai.tv%s' % compat_urllib_parse.quote(captions),
  159. }]
  160. return subtitles
  161. class RaiIE(InfoExtractor):
  162. _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'
  163. _TESTS = [
  164. {
  165. 'url': 'http://www.report.rai.it/dl/Report/puntata/ContentItem-0c7a664b-d0f4-4b2c-8835-3f82e46f433e.html',
  166. 'md5': 'e0e7a8a131e249d1aa0ebf270d1d8db7',
  167. 'info_dict': {
  168. 'id': '59d69d28-6bb6-409d-a4b5-ed44096560af',
  169. 'ext': 'flv',
  170. 'title': 'Il pacco',
  171. 'description': 'md5:4b1afae1364115ce5d78ed83cd2e5b3a',
  172. 'upload_date': '20141221',
  173. },
  174. }
  175. ]
  176. @classmethod
  177. def suitable(cls, url):
  178. return False if RaiTVIE.suitable(url) else super(RaiIE, cls).suitable(url)
  179. def _real_extract(self, url):
  180. video_id = self._match_id(url)
  181. webpage = self._download_webpage(url, video_id)
  182. iframe_url = self._search_regex(
  183. [r'<iframe[^>]+src="([^"]*/dl/[^"]+\?iframe\b[^"]*)"',
  184. r'drawMediaRaiTV\(["\'](.+?)["\']'],
  185. webpage, 'iframe')
  186. if not iframe_url.startswith('http'):
  187. iframe_url = compat_urlparse.urljoin(url, iframe_url)
  188. return self.url_result(iframe_url)