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.

254 lines
9.3 KiB

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