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.

252 lines
9.2 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_id,
  9. get_element_by_attribute,
  10. int_or_none,
  11. qualities,
  12. )
  13. # There are different sources of video in arte.tv, the extraction process
  14. # is different for each one. The videos usually expire in 7 days, so we can't
  15. # add tests.
  16. class ArteTvIE(InfoExtractor):
  17. _VALID_URL = r'http://videos\.arte\.tv/(?P<lang>fr|de)/.*-(?P<id>.*?)\.html'
  18. IE_NAME = 'arte.tv'
  19. def _real_extract(self, url):
  20. mobj = re.match(self._VALID_URL, url)
  21. lang = mobj.group('lang')
  22. video_id = mobj.group('id')
  23. ref_xml_url = url.replace('/videos/', '/do_delegate/videos/')
  24. ref_xml_url = ref_xml_url.replace('.html', ',view,asPlayerXml.xml')
  25. ref_xml_doc = self._download_xml(
  26. ref_xml_url, video_id, note='Downloading metadata')
  27. config_node = find_xpath_attr(ref_xml_doc, './/video', 'lang', lang)
  28. config_xml_url = config_node.attrib['ref']
  29. config = self._download_xml(
  30. config_xml_url, video_id, note='Downloading configuration')
  31. formats = [{
  32. 'forma_id': q.attrib['quality'],
  33. # The playpath starts at 'mp4:', if we don't manually
  34. # split the url, rtmpdump will incorrectly parse them
  35. 'url': q.text.split('mp4:', 1)[0],
  36. 'play_path': 'mp4:' + q.text.split('mp4:', 1)[1],
  37. 'ext': 'flv',
  38. 'quality': 2 if q.attrib['quality'] == 'hd' else 1,
  39. } for q in config.findall('./urls/url')]
  40. self._sort_formats(formats)
  41. title = config.find('.//name').text
  42. thumbnail = config.find('.//firstThumbnailUrl').text
  43. return {
  44. 'id': video_id,
  45. 'title': title,
  46. 'thumbnail': thumbnail,
  47. 'formats': formats,
  48. }
  49. class ArteTVPlus7IE(InfoExtractor):
  50. IE_NAME = 'arte.tv:+7'
  51. _VALID_URL = r'https?://(?:www\.)?arte\.tv/guide/(?P<lang>fr|de)/(?:(?:sendungen|emissions)/)?(?P<id>.*?)/(?P<name>.*?)(\?.*)?'
  52. @classmethod
  53. def _extract_url_info(cls, url):
  54. mobj = re.match(cls._VALID_URL, url)
  55. lang = mobj.group('lang')
  56. # This is not a real id, it can be for example AJT for the news
  57. # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
  58. video_id = mobj.group('id')
  59. return video_id, lang
  60. def _real_extract(self, url):
  61. video_id, lang = self._extract_url_info(url)
  62. webpage = self._download_webpage(url, video_id)
  63. return self._extract_from_webpage(webpage, video_id, lang)
  64. def _extract_from_webpage(self, webpage, video_id, lang):
  65. json_url = self._html_search_regex(
  66. [r'arte_vp_url=["\'](.*?)["\']', r'data-url=["\']([^"]+)["\']'],
  67. webpage, 'json vp url')
  68. return self._extract_from_json_url(json_url, video_id, lang)
  69. def _extract_from_json_url(self, json_url, video_id, lang):
  70. info = self._download_json(json_url, video_id)
  71. player_info = info['videoJsonPlayer']
  72. upload_date_str = player_info.get('shootingDate')
  73. if not upload_date_str:
  74. upload_date_str = player_info.get('VDA', '').split(' ')[0]
  75. title = player_info['VTI'].strip()
  76. subtitle = player_info.get('VSU', '').strip()
  77. if subtitle:
  78. title += ' - %s' % subtitle
  79. info_dict = {
  80. 'id': player_info['VID'],
  81. 'title': title,
  82. 'description': player_info.get('VDE'),
  83. 'upload_date': unified_strdate(upload_date_str),
  84. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  85. }
  86. qfunc = qualities(['HQ', 'MQ', 'EQ', 'SQ'])
  87. formats = []
  88. for format_id, format_dict in player_info['VSR'].items():
  89. f = dict(format_dict)
  90. versionCode = f.get('versionCode')
  91. langcode = {
  92. 'fr': 'F',
  93. 'de': 'A',
  94. }.get(lang, lang)
  95. lang_rexs = [r'VO?%s' % langcode, r'VO?.-ST%s' % langcode]
  96. lang_pref = (
  97. None if versionCode is None else (
  98. 10 if any(re.match(r, versionCode) for r in lang_rexs)
  99. else -10))
  100. source_pref = 0
  101. if versionCode is not None:
  102. # The original version with subtitles has lower relevance
  103. if re.match(r'VO-ST(F|A)', versionCode):
  104. source_pref -= 10
  105. # The version with sourds/mal subtitles has also lower relevance
  106. elif re.match(r'VO?(F|A)-STM\1', versionCode):
  107. source_pref -= 9
  108. format = {
  109. 'format_id': format_id,
  110. 'preference': -10 if f.get('videoFormat') == 'M3U8' else None,
  111. 'language_preference': lang_pref,
  112. 'format_note': '%s, %s' % (f.get('versionCode'), f.get('versionLibelle')),
  113. 'width': int_or_none(f.get('width')),
  114. 'height': int_or_none(f.get('height')),
  115. 'tbr': int_or_none(f.get('bitrate')),
  116. 'quality': qfunc(f['quality']),
  117. 'source_preference': source_pref,
  118. }
  119. if f.get('mediaType') == 'rtmp':
  120. format['url'] = f['streamer']
  121. format['play_path'] = 'mp4:' + f['url']
  122. format['ext'] = 'flv'
  123. else:
  124. format['url'] = f['url']
  125. formats.append(format)
  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 = get_element_by_id(anchor_id, webpage)
  167. return self._extract_from_webpage(row, anchor_id, lang)
  168. class ArteTVDDCIE(ArteTVPlus7IE):
  169. IE_NAME = 'arte.tv:ddc'
  170. _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>.+)'
  171. def _real_extract(self, url):
  172. video_id, lang = self._extract_url_info(url)
  173. if lang == 'folge':
  174. lang = 'de'
  175. elif lang == 'emission':
  176. lang = 'fr'
  177. webpage = self._download_webpage(url, video_id)
  178. scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
  179. script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
  180. javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
  181. json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
  182. return self._extract_from_json_url(json_url, video_id, lang)
  183. class ArteTVConcertIE(ArteTVPlus7IE):
  184. IE_NAME = 'arte.tv:concert'
  185. _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>de|fr)/(?P<id>.+)'
  186. _TEST = {
  187. 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
  188. 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
  189. 'info_dict': {
  190. 'id': '186',
  191. 'ext': 'mp4',
  192. 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
  193. 'upload_date': '20140128',
  194. 'description': 'md5:486eb08f991552ade77439fe6d82c305',
  195. },
  196. }
  197. class ArteTVEmbedIE(ArteTVPlus7IE):
  198. IE_NAME = 'arte.tv:embed'
  199. _VALID_URL = r'''(?x)
  200. http://www\.arte\.tv
  201. /playerv2/embed\.php\?json_url=
  202. (?P<json_url>
  203. http://arte\.tv/papi/tvguide/videos/stream/player/
  204. (?P<lang>[^/]+)/(?P<id>[^/]+)[^&]*
  205. )
  206. '''
  207. def _real_extract(self, url):
  208. mobj = re.match(self._VALID_URL, url)
  209. video_id = mobj.group('id')
  210. lang = mobj.group('lang')
  211. json_url = mobj.group('json_url')
  212. return self._extract_from_json_url(json_url, video_id, lang)