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.

274 lines
10 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 ..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. query = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
  60. if 'vid' in query:
  61. video_id = query['vid'][0]
  62. else:
  63. # This is not a real id, it can be for example AJT for the news
  64. # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
  65. video_id = mobj.group('id')
  66. return video_id, lang
  67. def _real_extract(self, url):
  68. video_id, lang = self._extract_url_info(url)
  69. webpage = self._download_webpage(url, video_id)
  70. return self._extract_from_webpage(webpage, video_id, lang)
  71. def _extract_from_webpage(self, webpage, video_id, lang):
  72. patterns_templates = (r'arte_vp_url=["\'](.*?%s.*?)["\']', r'data-url=["\']([^"]+%s[^"]+)["\']')
  73. ids = (video_id, '')
  74. # some pages contain multiple videos (like
  75. # http://www.arte.tv/guide/de/sendungen/XEN/xenius/?vid=055918-015_PLUS7-D),
  76. # so we first try to look for json URLs that contain the video id from
  77. # the 'vid' parameter.
  78. patterns = [t % re.escape(_id) for _id in ids for t in patterns_templates]
  79. json_url = self._html_search_regex(
  80. patterns, webpage, 'json vp url', default=None)
  81. if not json_url:
  82. iframe_url = self._html_search_regex(
  83. r'<iframe[^>]+src=(["\'])(?P<url>.+\bjson_url=.+?)\1',
  84. webpage, 'iframe url', group='url')
  85. json_url = compat_parse_qs(
  86. compat_urllib_parse_urlparse(iframe_url).query)['json_url'][0]
  87. return self._extract_from_json_url(json_url, video_id, lang)
  88. def _extract_from_json_url(self, json_url, video_id, lang):
  89. info = self._download_json(json_url, video_id)
  90. player_info = info['videoJsonPlayer']
  91. upload_date_str = player_info.get('shootingDate')
  92. if not upload_date_str:
  93. upload_date_str = player_info.get('VDA', '').split(' ')[0]
  94. title = player_info['VTI'].strip()
  95. subtitle = player_info.get('VSU', '').strip()
  96. if subtitle:
  97. title += ' - %s' % subtitle
  98. info_dict = {
  99. 'id': player_info['VID'],
  100. 'title': title,
  101. 'description': player_info.get('VDE'),
  102. 'upload_date': unified_strdate(upload_date_str),
  103. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  104. }
  105. qfunc = qualities(['HQ', 'MQ', 'EQ', 'SQ'])
  106. formats = []
  107. for format_id, format_dict in player_info['VSR'].items():
  108. f = dict(format_dict)
  109. versionCode = f.get('versionCode')
  110. langcode = {
  111. 'fr': 'F',
  112. 'de': 'A',
  113. }.get(lang, lang)
  114. lang_rexs = [r'VO?%s' % langcode, r'VO?.-ST%s' % langcode]
  115. lang_pref = (
  116. None if versionCode is None else (
  117. 10 if any(re.match(r, versionCode) for r in lang_rexs)
  118. else -10))
  119. source_pref = 0
  120. if versionCode is not None:
  121. # The original version with subtitles has lower relevance
  122. if re.match(r'VO-ST(F|A)', versionCode):
  123. source_pref -= 10
  124. # The version with sourds/mal subtitles has also lower relevance
  125. elif re.match(r'VO?(F|A)-STM\1', versionCode):
  126. source_pref -= 9
  127. format = {
  128. 'format_id': format_id,
  129. 'preference': -10 if f.get('videoFormat') == 'M3U8' else None,
  130. 'language_preference': lang_pref,
  131. 'format_note': '%s, %s' % (f.get('versionCode'), f.get('versionLibelle')),
  132. 'width': int_or_none(f.get('width')),
  133. 'height': int_or_none(f.get('height')),
  134. 'tbr': int_or_none(f.get('bitrate')),
  135. 'quality': qfunc(f.get('quality')),
  136. 'source_preference': source_pref,
  137. }
  138. if f.get('mediaType') == 'rtmp':
  139. format['url'] = f['streamer']
  140. format['play_path'] = 'mp4:' + f['url']
  141. format['ext'] = 'flv'
  142. else:
  143. format['url'] = f['url']
  144. formats.append(format)
  145. self._check_formats(formats, video_id)
  146. self._sort_formats(formats)
  147. info_dict['formats'] = formats
  148. return info_dict
  149. # It also uses the arte_vp_url url from the webpage to extract the information
  150. class ArteTVCreativeIE(ArteTVPlus7IE):
  151. IE_NAME = 'arte.tv:creative'
  152. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de)/(?:magazine?/)?(?P<id>[^?#]+)'
  153. _TESTS = [{
  154. 'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
  155. 'info_dict': {
  156. 'id': '72176',
  157. 'ext': 'mp4',
  158. 'title': 'Folge 2 - Corporate Design',
  159. 'upload_date': '20131004',
  160. },
  161. }, {
  162. 'url': 'http://creative.arte.tv/fr/Monty-Python-Reunion',
  163. 'info_dict': {
  164. 'id': '160676',
  165. 'ext': 'mp4',
  166. 'title': 'Monty Python live (mostly)',
  167. 'description': 'Événement ! Quarante-cinq ans après leurs premiers succès, les légendaires Monty Python remontent sur scène.\n',
  168. 'upload_date': '20140805',
  169. }
  170. }]
  171. class ArteTVFutureIE(ArteTVPlus7IE):
  172. IE_NAME = 'arte.tv:future'
  173. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de)/(thema|sujet)/.*?#article-anchor-(?P<id>\d+)'
  174. _TEST = {
  175. 'url': 'http://future.arte.tv/fr/sujet/info-sciences#article-anchor-7081',
  176. 'info_dict': {
  177. 'id': '5201',
  178. 'ext': 'mp4',
  179. 'title': 'Les champignons au secours de la planète',
  180. 'upload_date': '20131101',
  181. },
  182. }
  183. def _real_extract(self, url):
  184. anchor_id, lang = self._extract_url_info(url)
  185. webpage = self._download_webpage(url, anchor_id)
  186. row = self._search_regex(
  187. r'(?s)id="%s"[^>]*>.+?(<div[^>]*arte_vp_url[^>]*>)' % anchor_id,
  188. webpage, 'row')
  189. return self._extract_from_webpage(row, anchor_id, lang)
  190. class ArteTVDDCIE(ArteTVPlus7IE):
  191. IE_NAME = 'arte.tv:ddc'
  192. _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>.+)'
  193. def _real_extract(self, url):
  194. video_id, lang = self._extract_url_info(url)
  195. if lang == 'folge':
  196. lang = 'de'
  197. elif lang == 'emission':
  198. lang = 'fr'
  199. webpage = self._download_webpage(url, video_id)
  200. scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
  201. script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
  202. javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
  203. json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
  204. return self._extract_from_json_url(json_url, video_id, lang)
  205. class ArteTVConcertIE(ArteTVPlus7IE):
  206. IE_NAME = 'arte.tv:concert'
  207. _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>de|fr)/(?P<id>.+)'
  208. _TEST = {
  209. 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
  210. 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
  211. 'info_dict': {
  212. 'id': '186',
  213. 'ext': 'mp4',
  214. 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
  215. 'upload_date': '20140128',
  216. 'description': 'md5:486eb08f991552ade77439fe6d82c305',
  217. },
  218. }
  219. class ArteTVEmbedIE(ArteTVPlus7IE):
  220. IE_NAME = 'arte.tv:embed'
  221. _VALID_URL = r'''(?x)
  222. http://www\.arte\.tv
  223. /playerv2/embed\.php\?json_url=
  224. (?P<json_url>
  225. http://arte\.tv/papi/tvguide/videos/stream/player/
  226. (?P<lang>[^/]+)/(?P<id>[^/]+)[^&]*
  227. )
  228. '''
  229. def _real_extract(self, url):
  230. mobj = re.match(self._VALID_URL, url)
  231. video_id = mobj.group('id')
  232. lang = mobj.group('lang')
  233. json_url = mobj.group('json_url')
  234. return self._extract_from_json_url(json_url, video_id, lang)