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