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.

282 lines
11 KiB

12 years ago
12 years ago
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. find_xpath_attr,
  8. unified_strdate,
  9. determine_ext,
  10. get_element_by_id,
  11. compat_str,
  12. get_element_by_attribute,
  13. )
  14. # There are different sources of video in arte.tv, the extraction process
  15. # is different for each one. The videos usually expire in 7 days, so we can't
  16. # add tests.
  17. class ArteTvIE(InfoExtractor):
  18. _VALID_URL = r'http://videos\.arte\.tv/(?P<lang>fr|de)/.*-(?P<id>.*?)\.html'
  19. IE_NAME = 'arte.tv'
  20. def _real_extract(self, url):
  21. mobj = re.match(self._VALID_URL, url)
  22. lang = mobj.group('lang')
  23. video_id = mobj.group('id')
  24. ref_xml_url = url.replace('/videos/', '/do_delegate/videos/')
  25. ref_xml_url = ref_xml_url.replace('.html', ',view,asPlayerXml.xml')
  26. ref_xml_doc = self._download_xml(
  27. ref_xml_url, video_id, note='Downloading metadata')
  28. config_node = find_xpath_attr(ref_xml_doc, './/video', 'lang', lang)
  29. config_xml_url = config_node.attrib['ref']
  30. config = self._download_xml(
  31. config_xml_url, video_id, note='Downloading configuration')
  32. formats = [{
  33. 'forma_id': q.attrib['quality'],
  34. # The playpath starts at 'mp4:', if we don't manually
  35. # split the url, rtmpdump will incorrectly parse them
  36. 'url': q.text.split('mp4:', 1)[0],
  37. 'play_path': 'mp4:' + q.text.split('mp4:', 1)[1],
  38. 'ext': 'flv',
  39. 'quality': 2 if q.attrib['quality'] == 'hd' else 1,
  40. } for q in config.findall('./urls/url')]
  41. self._sort_formats(formats)
  42. title = config.find('.//name').text
  43. thumbnail = config.find('.//firstThumbnailUrl').text
  44. return {
  45. 'id': video_id,
  46. 'title': title,
  47. 'thumbnail': thumbnail,
  48. 'formats': formats,
  49. }
  50. class ArteTVPlus7IE(InfoExtractor):
  51. IE_NAME = 'arte.tv:+7'
  52. _VALID_URL = r'https?://(?:www\.)?arte\.tv/guide/(?P<lang>fr|de)/(?:(?:sendungen|emissions)/)?(?P<id>.*?)/(?P<name>.*?)(\?.*)?'
  53. @classmethod
  54. def _extract_url_info(cls, url):
  55. mobj = re.match(cls._VALID_URL, url)
  56. lang = mobj.group('lang')
  57. # This is not a real id, it can be for example AJT for the news
  58. # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
  59. video_id = mobj.group('id')
  60. return video_id, lang
  61. def _real_extract(self, url):
  62. video_id, lang = self._extract_url_info(url)
  63. webpage = self._download_webpage(url, video_id)
  64. return self._extract_from_webpage(webpage, video_id, lang)
  65. def _extract_from_webpage(self, webpage, video_id, lang):
  66. json_url = self._html_search_regex(
  67. [r'arte_vp_url=["\'](.*?)["\']', r'data-url=["\']([^"]+)["\']'],
  68. webpage, 'json vp url')
  69. return self._extract_from_json_url(json_url, video_id, lang)
  70. def _extract_from_json_url(self, json_url, video_id, lang):
  71. info = self._download_json(json_url, video_id)
  72. player_info = info['videoJsonPlayer']
  73. upload_date_str = player_info.get('shootingDate')
  74. if not upload_date_str:
  75. upload_date_str = player_info.get('VDA', '').split(' ')[0]
  76. info_dict = {
  77. 'id': player_info['VID'],
  78. 'title': player_info['VTI'],
  79. 'description': player_info.get('VDE'),
  80. 'upload_date': unified_strdate(upload_date_str),
  81. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  82. }
  83. all_formats = player_info['VSR'].values()
  84. # Some formats use the m3u8 protocol
  85. all_formats = list(filter(lambda f: f.get('videoFormat') != 'M3U8', all_formats))
  86. def _match_lang(f):
  87. if f.get('versionCode') is None:
  88. return True
  89. # Return true if that format is in the language of the url
  90. if lang == 'fr':
  91. l = 'F'
  92. elif lang == 'de':
  93. l = 'A'
  94. else:
  95. l = lang
  96. regexes = [r'VO?%s' % l, r'VO?.-ST%s' % l]
  97. return any(re.match(r, f['versionCode']) for r in regexes)
  98. # Some formats may not be in the same language as the url
  99. # TODO: Might want not to drop videos that does not match requested language
  100. # but to process those formats with lower precedence
  101. formats = filter(_match_lang, all_formats)
  102. formats = list(formats) # in python3 filter returns an iterator
  103. if not formats:
  104. # Some videos are only available in the 'Originalversion'
  105. # they aren't tagged as being in French or German
  106. # Sometimes there are neither videos of requested lang code
  107. # nor original version videos available
  108. # For such cases we just take all_formats as is
  109. formats = all_formats
  110. if not formats:
  111. raise ExtractorError('The formats list is empty')
  112. if re.match(r'[A-Z]Q', formats[0]['quality']) is not None:
  113. def sort_key(f):
  114. return ['HQ', 'MQ', 'EQ', 'SQ'].index(f['quality'])
  115. else:
  116. def sort_key(f):
  117. versionCode = f.get('versionCode')
  118. if versionCode is None:
  119. versionCode = ''
  120. return (
  121. # Sort first by quality
  122. int(f.get('height', -1)),
  123. int(f.get('bitrate', -1)),
  124. # The original version with subtitles has lower relevance
  125. re.match(r'VO-ST(F|A)', versionCode) is None,
  126. # The version with sourds/mal subtitles has also lower relevance
  127. re.match(r'VO?(F|A)-STM\1', versionCode) is None,
  128. # Prefer http downloads over m3u8
  129. 0 if f['url'].endswith('m3u8') else 1,
  130. )
  131. formats = sorted(formats, key=sort_key)
  132. def _format(format_info):
  133. quality = ''
  134. height = format_info.get('height')
  135. if height is not None:
  136. quality = compat_str(height)
  137. bitrate = format_info.get('bitrate')
  138. if bitrate is not None:
  139. quality += '-%d' % bitrate
  140. if format_info.get('versionCode') is not None:
  141. format_id = '%s-%s' % (quality, format_info['versionCode'])
  142. else:
  143. format_id = quality
  144. media_type = format_info.get('mediaType')
  145. if media_type is not None:
  146. format_id += '-%s' % media_type
  147. info = {
  148. 'format_id': format_id,
  149. 'format_note': format_info.get('versionLibelle'),
  150. 'width': format_info.get('width'),
  151. 'height': height,
  152. }
  153. if format_info['mediaType'] == 'rtmp':
  154. info['url'] = format_info['streamer']
  155. info['play_path'] = 'mp4:' + format_info['url']
  156. info['ext'] = 'flv'
  157. else:
  158. info['url'] = format_info['url']
  159. info['ext'] = determine_ext(info['url'])
  160. return info
  161. info_dict['formats'] = [_format(f) for f in formats]
  162. return info_dict
  163. # It also uses the arte_vp_url url from the webpage to extract the information
  164. class ArteTVCreativeIE(ArteTVPlus7IE):
  165. IE_NAME = 'arte.tv:creative'
  166. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de)/(?:magazine?/)?(?P<id>[^?#]+)'
  167. _TESTS = [{
  168. 'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
  169. 'info_dict': {
  170. 'id': '72176',
  171. 'ext': 'mp4',
  172. 'title': 'Folge 2 - Corporate Design',
  173. 'upload_date': '20131004',
  174. },
  175. }, {
  176. 'url': 'http://creative.arte.tv/fr/Monty-Python-Reunion',
  177. 'info_dict': {
  178. 'id': '160676',
  179. 'ext': 'mp4',
  180. 'title': 'Monty Python live (mostly)',
  181. 'description': 'Événement ! Quarante-cinq ans après leurs premiers succès, les légendaires Monty Python remontent sur scène.\n',
  182. 'upload_date': '20140805',
  183. }
  184. }]
  185. class ArteTVFutureIE(ArteTVPlus7IE):
  186. IE_NAME = 'arte.tv:future'
  187. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de)/(thema|sujet)/.*?#article-anchor-(?P<id>\d+)'
  188. _TEST = {
  189. 'url': 'http://future.arte.tv/fr/sujet/info-sciences#article-anchor-7081',
  190. 'info_dict': {
  191. 'id': '5201',
  192. 'ext': 'mp4',
  193. 'title': 'Les champignons au secours de la planète',
  194. 'upload_date': '20131101',
  195. },
  196. }
  197. def _real_extract(self, url):
  198. anchor_id, lang = self._extract_url_info(url)
  199. webpage = self._download_webpage(url, anchor_id)
  200. row = get_element_by_id(anchor_id, webpage)
  201. return self._extract_from_webpage(row, anchor_id, lang)
  202. class ArteTVDDCIE(ArteTVPlus7IE):
  203. IE_NAME = 'arte.tv:ddc'
  204. _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>.+)'
  205. def _real_extract(self, url):
  206. video_id, lang = self._extract_url_info(url)
  207. if lang == 'folge':
  208. lang = 'de'
  209. elif lang == 'emission':
  210. lang = 'fr'
  211. webpage = self._download_webpage(url, video_id)
  212. scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
  213. script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
  214. javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
  215. json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
  216. return self._extract_from_json_url(json_url, video_id, lang)
  217. class ArteTVConcertIE(ArteTVPlus7IE):
  218. IE_NAME = 'arte.tv:concert'
  219. _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>de|fr)/(?P<id>.+)'
  220. _TEST = {
  221. 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
  222. 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
  223. 'info_dict': {
  224. 'id': '186',
  225. 'ext': 'mp4',
  226. 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
  227. 'upload_date': '20140128',
  228. 'description': 'md5:486eb08f991552ade77439fe6d82c305',
  229. },
  230. }
  231. class ArteTVEmbedIE(ArteTVPlus7IE):
  232. IE_NAME = 'arte.tv:embed'
  233. _VALID_URL = r'''(?x)
  234. http://www\.arte\.tv
  235. /playerv2/embed\.php\?json_url=
  236. (?P<json_url>
  237. http://arte\.tv/papi/tvguide/videos/stream/player/
  238. (?P<lang>[^/]+)/(?P<id>[^/]+)[^&]*
  239. )
  240. '''
  241. def _real_extract(self, url):
  242. mobj = re.match(self._VALID_URL, url)
  243. video_id = mobj.group('id')
  244. lang = mobj.group('lang')
  245. json_url = mobj.group('json_url')
  246. return self._extract_from_json_url(json_url, video_id, lang)