]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/arte.py
18d5916589b239c9cf12a7caa629efeea784ce86
[youtubedl] / youtube_dl / extractor / arte.py
1 import re
2 import json
3 import xml.etree.ElementTree
4
5 from .common import InfoExtractor
6 from ..utils import (
7 ExtractorError,
8 find_xpath_attr,
9 unified_strdate,
10 )
11
12 class ArteTvIE(InfoExtractor):
13 """
14 There are two sources of video in arte.tv: videos.arte.tv and
15 www.arte.tv/guide, the extraction process is different for each one.
16 The videos expire in 7 days, so we can't add tests.
17 """
18 _EMISSION_URL = r'(?:http://)?www\.arte.tv/guide/(?P<lang>fr|de)/(?:(?:sendungen|emissions)/)?(?P<id>.*?)/(?P<name>.*?)(\?.*)?'
19 _VIDEOS_URL = r'(?:http://)?videos.arte.tv/(?P<lang>fr|de)/.*-(?P<id>.*?).html'
20 _LIVE_URL = r'index-[0-9]+\.html$'
21
22 IE_NAME = u'arte.tv'
23
24 @classmethod
25 def suitable(cls, url):
26 return any(re.match(regex, url) for regex in (cls._EMISSION_URL, cls._VIDEOS_URL))
27
28 # TODO implement Live Stream
29 # from ..utils import compat_urllib_parse
30 # def extractLiveStream(self, url):
31 # video_lang = url.split('/')[-4]
32 # info = self.grep_webpage(
33 # url,
34 # r'src="(.*?/videothek_js.*?\.js)',
35 # 0,
36 # [
37 # (1, 'url', u'Invalid URL: %s' % url)
38 # ]
39 # )
40 # http_host = url.split('/')[2]
41 # next_url = 'http://%s%s' % (http_host, compat_urllib_parse.unquote(info.get('url')))
42 # info = self.grep_webpage(
43 # next_url,
44 # r'(s_artestras_scst_geoFRDE_' + video_lang + '.*?)\'.*?' +
45 # '(http://.*?\.swf).*?' +
46 # '(rtmp://.*?)\'',
47 # re.DOTALL,
48 # [
49 # (1, 'path', u'could not extract video path: %s' % url),
50 # (2, 'player', u'could not extract video player: %s' % url),
51 # (3, 'url', u'could not extract video url: %s' % url)
52 # ]
53 # )
54 # video_url = u'%s/%s' % (info.get('url'), info.get('path'))
55
56 def _real_extract(self, url):
57 mobj = re.match(self._EMISSION_URL, url)
58 if mobj is not None:
59 lang = mobj.group('lang')
60 # This is not a real id, it can be for example AJT for the news
61 # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
62 video_id = mobj.group('id')
63 return self._extract_emission(url, video_id, lang)
64
65 mobj = re.match(self._VIDEOS_URL, url)
66 if mobj is not None:
67 id = mobj.group('id')
68 lang = mobj.group('lang')
69 return self._extract_video(url, id, lang)
70
71 if re.search(self._LIVE_URL, video_id) is not None:
72 raise ExtractorError(u'Arte live streams are not yet supported, sorry')
73 # self.extractLiveStream(url)
74 # return
75
76 def _extract_emission(self, url, video_id, lang):
77 """Extract from www.arte.tv/guide"""
78 webpage = self._download_webpage(url, video_id)
79 json_url = self._html_search_regex(r'arte_vp_url="(.*?)"', webpage, 'json url')
80
81 json_info = self._download_webpage(json_url, video_id, 'Downloading info json')
82 self.report_extraction(video_id)
83 info = json.loads(json_info)
84 player_info = info['videoJsonPlayer']
85
86 info_dict = {'id': player_info['VID'],
87 'title': player_info['VTI'],
88 'description': player_info['VDE'],
89 'upload_date': unified_strdate(player_info['VDA'].split(' ')[0]),
90 'thumbnail': player_info['programImage'],
91 'ext': 'flv',
92 }
93
94 formats = player_info['VSR'].values()
95 def _match_lang(f):
96 # Return true if that format is in the language of the url
97 if lang == 'fr':
98 l = 'F'
99 elif lang == 'de':
100 l = 'A'
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 formats = filter(_match_lang, formats)
105 # We order the formats by quality
106 formats = sorted(formats, key=lambda f: int(f['height']))
107 # Pick the best quality
108 format_info = formats[-1]
109 if format_info['mediaType'] == u'rtmp':
110 info_dict['url'] = format_info['streamer']
111 info_dict['play_path'] = 'mp4:' + format_info['url']
112 else:
113 info_dict['url'] = format_info['url']
114
115 return info_dict
116
117 def _extract_video(self, url, video_id, lang):
118 """Extract from videos.arte.tv"""
119 ref_xml_url = url.replace('/videos/', '/do_delegate/videos/')
120 ref_xml_url = ref_xml_url.replace('.html', ',view,asPlayerXml.xml')
121 ref_xml = self._download_webpage(ref_xml_url, video_id, note=u'Downloading metadata')
122 ref_xml_doc = xml.etree.ElementTree.fromstring(ref_xml)
123 config_node = find_xpath_attr(ref_xml_doc, './/video', 'lang', lang)
124 config_xml_url = config_node.attrib['ref']
125 config_xml = self._download_webpage(config_xml_url, video_id, note=u'Downloading configuration')
126
127 video_urls = list(re.finditer(r'<url quality="(?P<quality>.*?)">(?P<url>.*?)</url>', config_xml))
128 def _key(m):
129 quality = m.group('quality')
130 if quality == 'hd':
131 return 2
132 else:
133 return 1
134 # We pick the best quality
135 video_urls = sorted(video_urls, key=_key)
136 video_url = list(video_urls)[-1].group('url')
137
138 title = self._html_search_regex(r'<name>(.*?)</name>', config_xml, 'title')
139 thumbnail = self._html_search_regex(r'<firstThumbnailUrl>(.*?)</firstThumbnailUrl>',
140 config_xml, 'thumbnail')
141 return {'id': video_id,
142 'title': title,
143 'thumbnail': thumbnail,
144 'url': video_url,
145 'ext': 'flv',
146 }