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.

264 lines
9.7 KiB

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