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.

253 lines
9.5 KiB

11 years ago
11 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. 'url': q.text,
  35. 'ext': 'flv',
  36. 'quality': 2 if q.attrib['quality'] == 'hd' else 1,
  37. } for q in config.findall('./urls/url')]
  38. self._sort_formats(formats)
  39. title = config.find('.//name').text
  40. thumbnail = config.find('.//firstThumbnailUrl').text
  41. return {
  42. 'id': video_id,
  43. 'title': title,
  44. 'thumbnail': thumbnail,
  45. 'formats': formats,
  46. }
  47. class ArteTVPlus7IE(InfoExtractor):
  48. IE_NAME = 'arte.tv:+7'
  49. _VALID_URL = r'https?://(?:www\.)?arte\.tv/guide/(?P<lang>fr|de)/(?:(?:sendungen|emissions)/)?(?P<id>.*?)/(?P<name>.*?)(\?.*)?'
  50. @classmethod
  51. def _extract_url_info(cls, url):
  52. mobj = re.match(cls._VALID_URL, url)
  53. lang = mobj.group('lang')
  54. # This is not a real id, it can be for example AJT for the news
  55. # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
  56. video_id = mobj.group('id')
  57. return video_id, lang
  58. def _real_extract(self, url):
  59. video_id, lang = self._extract_url_info(url)
  60. webpage = self._download_webpage(url, video_id)
  61. return self._extract_from_webpage(webpage, video_id, lang)
  62. def _extract_from_webpage(self, webpage, video_id, lang):
  63. json_url = self._html_search_regex(
  64. r'arte_vp_url="(.*?)"', webpage, 'json vp url')
  65. return self._extract_from_json_url(json_url, video_id, lang)
  66. def _extract_from_json_url(self, json_url, video_id, lang):
  67. info = self._download_json(json_url, video_id)
  68. player_info = info['videoJsonPlayer']
  69. info_dict = {
  70. 'id': player_info['VID'],
  71. 'title': player_info['VTI'],
  72. 'description': player_info.get('VDE'),
  73. 'upload_date': unified_strdate(player_info.get('VDA', '').split(' ')[0]),
  74. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  75. }
  76. all_formats = player_info['VSR'].values()
  77. # Some formats use the m3u8 protocol
  78. all_formats = list(filter(lambda f: f.get('videoFormat') != 'M3U8', all_formats))
  79. def _match_lang(f):
  80. if f.get('versionCode') is None:
  81. return True
  82. # Return true if that format is in the language of the url
  83. if lang == 'fr':
  84. l = 'F'
  85. elif lang == 'de':
  86. l = 'A'
  87. else:
  88. l = lang
  89. regexes = [r'VO?%s' % l, r'VO?.-ST%s' % l]
  90. return any(re.match(r, f['versionCode']) for r in regexes)
  91. # Some formats may not be in the same language as the url
  92. formats = filter(_match_lang, all_formats)
  93. formats = list(formats) # in python3 filter returns an iterator
  94. if not formats:
  95. # Some videos are only available in the 'Originalversion'
  96. # they aren't tagged as being in French or German
  97. if all(f['versionCode'] == 'VO' for f in all_formats):
  98. formats = all_formats
  99. else:
  100. raise ExtractorError(u'The formats list is empty')
  101. if re.match(r'[A-Z]Q', formats[0]['quality']) is not None:
  102. def sort_key(f):
  103. return ['HQ', 'MQ', 'EQ', 'SQ'].index(f['quality'])
  104. else:
  105. def sort_key(f):
  106. return (
  107. # Sort first by quality
  108. int(f.get('height',-1)),
  109. int(f.get('bitrate',-1)),
  110. # The original version with subtitles has lower relevance
  111. re.match(r'VO-ST(F|A)', f.get('versionCode', '')) is None,
  112. # The version with sourds/mal subtitles has also lower relevance
  113. re.match(r'VO?(F|A)-STM\1', f.get('versionCode', '')) is None,
  114. # Prefer http downloads over m3u8
  115. 0 if f['url'].endswith('m3u8') else 1,
  116. )
  117. formats = sorted(formats, key=sort_key)
  118. def _format(format_info):
  119. quality = ''
  120. height = format_info.get('height')
  121. if height is not None:
  122. quality = compat_str(height)
  123. bitrate = format_info.get('bitrate')
  124. if bitrate is not None:
  125. quality += '-%d' % bitrate
  126. if format_info.get('versionCode') is not None:
  127. format_id = '%s-%s' % (quality, format_info['versionCode'])
  128. else:
  129. format_id = quality
  130. info = {
  131. 'format_id': format_id,
  132. 'format_note': format_info.get('versionLibelle'),
  133. 'width': format_info.get('width'),
  134. 'height': height,
  135. }
  136. if format_info['mediaType'] == 'rtmp':
  137. info['url'] = format_info['streamer']
  138. info['play_path'] = 'mp4:' + format_info['url']
  139. info['ext'] = 'flv'
  140. else:
  141. info['url'] = format_info['url']
  142. info['ext'] = determine_ext(info['url'])
  143. return info
  144. info_dict['formats'] = [_format(f) for f in formats]
  145. return info_dict
  146. # It also uses the arte_vp_url url from the webpage to extract the information
  147. class ArteTVCreativeIE(ArteTVPlus7IE):
  148. IE_NAME = 'arte.tv:creative'
  149. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de)/magazine?/(?P<id>.+)'
  150. _TEST = {
  151. 'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
  152. 'info_dict': {
  153. 'id': '050489-002',
  154. 'ext': 'mp4',
  155. 'title': 'Agentur Amateur / Agence Amateur #2 : Corporate Design',
  156. },
  157. }
  158. class ArteTVFutureIE(ArteTVPlus7IE):
  159. IE_NAME = 'arte.tv:future'
  160. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de)/(thema|sujet)/.*?#article-anchor-(?P<id>\d+)'
  161. _TEST = {
  162. 'url': 'http://future.arte.tv/fr/sujet/info-sciences#article-anchor-7081',
  163. 'info_dict': {
  164. 'id': '050940-003',
  165. 'ext': 'mp4',
  166. 'title': 'Les champignons au secours de la planète',
  167. },
  168. }
  169. def _real_extract(self, url):
  170. anchor_id, lang = self._extract_url_info(url)
  171. webpage = self._download_webpage(url, anchor_id)
  172. row = get_element_by_id(anchor_id, webpage)
  173. return self._extract_from_webpage(row, anchor_id, lang)
  174. class ArteTVDDCIE(ArteTVPlus7IE):
  175. IE_NAME = 'arte.tv:ddc'
  176. _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>.+)'
  177. def _real_extract(self, url):
  178. video_id, lang = self._extract_url_info(url)
  179. if lang == 'folge':
  180. lang = 'de'
  181. elif lang == 'emission':
  182. lang = 'fr'
  183. webpage = self._download_webpage(url, video_id)
  184. scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
  185. script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
  186. javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
  187. json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
  188. return self._extract_from_json_url(json_url, video_id, lang)
  189. class ArteTVConcertIE(ArteTVPlus7IE):
  190. IE_NAME = 'arte.tv:concert'
  191. _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>de|fr)/(?P<id>.+)'
  192. _TEST = {
  193. 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
  194. 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
  195. 'info_dict': {
  196. 'id': '186',
  197. 'ext': 'mp4',
  198. 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
  199. 'upload_date': '20140128',
  200. 'description': 'md5:486eb08f991552ade77439fe6d82c305',
  201. },
  202. }
  203. class ArteTVEmbedIE(ArteTVPlus7IE):
  204. IE_NAME = 'arte.tv:embed'
  205. _VALID_URL = r'''(?x)
  206. http://www\.arte\.tv
  207. /playerv2/embed\.php\?json_url=
  208. (?P<json_url>
  209. http://arte\.tv/papi/tvguide/videos/stream/player/
  210. (?P<lang>[^/]+)/(?P<id>[^/]+)[^&]*
  211. )
  212. '''
  213. def _real_extract(self, url):
  214. mobj = re.match(self._VALID_URL, url)
  215. video_id = mobj.group('id')
  216. lang = mobj.group('lang')
  217. json_url = mobj.group('json_url')
  218. return self._extract_from_json_url(json_url, video_id, lang)