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.

179 lines
7.1 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. parse_duration,
  10. unified_strdate,
  11. )
  12. class RaiIE(InfoExtractor):
  13. _VALID_URL = r'(?P<url>(?P<host>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)'
  14. _TESTS = [
  15. {
  16. 'url': 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-cb27157f-9dd0-4aee-b788-b1f67643a391.html',
  17. 'md5': 'c064c0b2d09c278fb293116ef5d0a32d',
  18. 'info_dict': {
  19. 'id': 'cb27157f-9dd0-4aee-b788-b1f67643a391',
  20. 'ext': 'mp4',
  21. 'title': 'Report del 07/04/2014',
  22. 'description': 'md5:f27c544694cacb46a078db84ec35d2d9',
  23. 'upload_date': '20140407',
  24. 'duration': 6160,
  25. }
  26. },
  27. {
  28. 'url': 'http://www.raisport.rai.it/dl/raiSport/media/rassegna-stampa-04a9f4bd-b563-40cf-82a6-aad3529cb4a9.html',
  29. 'md5': '8bb9c151924ce241b74dd52ef29ceafa',
  30. 'info_dict': {
  31. 'id': '04a9f4bd-b563-40cf-82a6-aad3529cb4a9',
  32. 'ext': 'mp4',
  33. 'title': 'TG PRIMO TEMPO',
  34. 'description': '',
  35. 'upload_date': '20140612',
  36. 'duration': 1758,
  37. },
  38. 'skip': 'Error 404',
  39. },
  40. {
  41. 'url': 'http://www.rainews.it/dl/rainews/media/state-of-the-net-Antonella-La-Carpia-regole-virali-7aafdea9-0e5d-49d5-88a6-7e65da67ae13.html',
  42. 'md5': '35cf7c229f22eeef43e48b5cf923bef0',
  43. 'info_dict': {
  44. 'id': '7aafdea9-0e5d-49d5-88a6-7e65da67ae13',
  45. 'ext': 'mp4',
  46. 'title': 'State of the Net, Antonella La Carpia: regole virali',
  47. 'description': 'md5:b0ba04a324126903e3da7763272ae63c',
  48. 'upload_date': '20140613',
  49. },
  50. 'skip': 'Error 404',
  51. },
  52. {
  53. 'url': 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-b4a49761-e0cc-4b14-8736-2729f6f73132-tg2.html',
  54. 'md5': '35694f062977fe6619943f08ed935730',
  55. 'info_dict': {
  56. 'id': 'b4a49761-e0cc-4b14-8736-2729f6f73132',
  57. 'ext': 'mp4',
  58. 'title': 'Alluvione in Sardegna e dissesto idrogeologico',
  59. 'description': 'Edizione delle ore 20:30 ',
  60. }
  61. },
  62. {
  63. 'url': 'http://www.ilcandidato.rai.it/dl/ray/media/Il-Candidato---Primo-episodio-Le-Primarie-28e5525a-b495-45e8-a7c3-bc48ba45d2b6.html',
  64. 'md5': '02b64456f7cc09f96ff14e7dd489017e',
  65. 'info_dict': {
  66. 'id': '28e5525a-b495-45e8-a7c3-bc48ba45d2b6',
  67. 'ext': 'flv',
  68. 'title': 'Il Candidato - Primo episodio: "Le Primarie"',
  69. 'description': 'Primo appuntamento con "Il candidato" con Filippo Timi, alias Piero Zucca presidente!',
  70. 'uploader': 'RaiTre',
  71. }
  72. },
  73. {
  74. 'url': 'http://www.report.rai.it/dl/Report/puntata/ContentItem-0c7a664b-d0f4-4b2c-8835-3f82e46f433e.html',
  75. 'md5': '037104d2c14132887e5e4cf114569214',
  76. 'info_dict': {
  77. 'id': '0c7a664b-d0f4-4b2c-8835-3f82e46f433e',
  78. 'ext': 'flv',
  79. 'title': 'Il pacco',
  80. 'description': 'md5:4b1afae1364115ce5d78ed83cd2e5b3a',
  81. 'uploader': 'RaiTre',
  82. 'upload_date': '20141221',
  83. },
  84. }
  85. ]
  86. def _extract_relinker_url(self, webpage):
  87. return self._proto_relative_url(self._search_regex(
  88. [r'name="videourl" content="([^"]+)"', r'var\s+videoURL(?:_MP4)?\s*=\s*"([^"]+)"'],
  89. webpage, 'relinker url', default=None))
  90. def _real_extract(self, url):
  91. mobj = re.match(self._VALID_URL, url)
  92. video_id = mobj.group('id')
  93. host = mobj.group('host')
  94. webpage = self._download_webpage(url, video_id)
  95. relinker_url = self._extract_relinker_url(webpage)
  96. if not relinker_url:
  97. iframe_url = self._search_regex(
  98. [r'<iframe[^>]+src="([^"]*/dl/[^"]+\?iframe\b[^"]*)"',
  99. r'drawMediaRaiTV\(["\'](.+?)["\']'],
  100. webpage, 'iframe')
  101. if not iframe_url.startswith('http'):
  102. iframe_url = compat_urlparse.urljoin(url, iframe_url)
  103. webpage = self._download_webpage(
  104. iframe_url, video_id)
  105. relinker_url = self._extract_relinker_url(webpage)
  106. relinker = self._download_json(
  107. '%s&output=47' % relinker_url, video_id)
  108. media_url = relinker['video'][0]
  109. ct = relinker.get('ct')
  110. if ct == 'f4m':
  111. formats = self._extract_f4m_formats(
  112. media_url + '&hdcore=3.7.0&plugin=aasp-3.7.0.39.44', video_id)
  113. else:
  114. formats = [{
  115. 'url': media_url,
  116. 'format_id': ct,
  117. }]
  118. json_link = self._html_search_meta(
  119. 'jsonlink', webpage, 'JSON link', default=None)
  120. if json_link:
  121. media = self._download_json(
  122. host + json_link, video_id, 'Downloading video JSON')
  123. title = media.get('name')
  124. description = media.get('desc')
  125. thumbnail = media.get('image_300') or media.get('image_medium') or media.get('image')
  126. duration = parse_duration(media.get('length'))
  127. uploader = media.get('author')
  128. upload_date = unified_strdate(media.get('date'))
  129. else:
  130. title = (self._search_regex(
  131. r'var\s+videoTitolo\s*=\s*"(.+?)";',
  132. webpage, 'title', default=None) or self._og_search_title(webpage)).replace('\\"', '"')
  133. description = self._og_search_description(webpage)
  134. thumbnail = self._og_search_thumbnail(webpage)
  135. duration = None
  136. uploader = self._html_search_meta('Editore', webpage, 'uploader')
  137. upload_date = unified_strdate(self._html_search_meta(
  138. 'item-date', webpage, 'upload date', default=None))
  139. subtitles = self.extract_subtitles(video_id, webpage)
  140. return {
  141. 'id': video_id,
  142. 'title': title,
  143. 'description': description,
  144. 'thumbnail': thumbnail,
  145. 'uploader': uploader,
  146. 'upload_date': upload_date,
  147. 'duration': duration,
  148. 'formats': formats,
  149. 'subtitles': subtitles,
  150. }
  151. def _get_subtitles(self, video_id, webpage):
  152. subtitles = {}
  153. m = re.search(r'<meta name="closedcaption" content="(?P<captions>[^"]+)"', webpage)
  154. if m:
  155. captions = m.group('captions')
  156. STL_EXT = '.stl'
  157. SRT_EXT = '.srt'
  158. if captions.endswith(STL_EXT):
  159. captions = captions[:-len(STL_EXT)] + SRT_EXT
  160. subtitles['it'] = [{
  161. 'ext': 'srt',
  162. 'url': 'http://www.rai.tv%s' % compat_urllib_parse.quote(captions),
  163. }]
  164. return subtitles