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