arte.py 11 KB

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