]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/arte.py
ArteTVIE: extract the video with the correct language
[yt-dlp.git] / youtube_dl / extractor / arte.py
1 import re
2 import json
3
4 from .common import InfoExtractor
5 from ..utils import (
6 # This is used by the not implemented extractLiveStream method
7 compat_urllib_parse,
8
9 ExtractorError,
10 unified_strdate,
11 )
12
13 class ArteTvIE(InfoExtractor):
14 """
15 There are two sources of video in arte.tv: videos.arte.tv and
16 www.arte.tv/guide, the extraction process is different for each one.
17 The videos expire in 7 days, so we can't add tests.
18 """
19 _EMISSION_URL = r'(?:http://)?www\.arte.tv/guide/(?P<lang>fr|de)/(?:(?:sendungen|emissions)/)?(?P<id>.*?)/(?P<name>.*?)(\?.*)?'
20 _VIDEOS_URL = r'(?:http://)?videos.arte.tv/(?:fr|de)/.*-(?P<id>.*?).html'
21 _LIVE_URL = r'index-[0-9]+\.html$'
22
23 IE_NAME = u'arte.tv'
24
25 @classmethod
26 def suitable(cls, url):
27 return any(re.match(regex, url) for regex in (cls._EMISSION_URL, cls._VIDEOS_URL))
28
29 # TODO implement Live Stream
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 name = mobj.group('name')
60 lang = mobj.group('lang')
61 # This is not a real id, it can be for example AJT for the news
62 # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
63 video_id = mobj.group('id')
64 return self._extract_emission(url, video_id, lang)
65
66 mobj = re.match(self._VIDEOS_URL, url)
67 if mobj is not None:
68 id = mobj.group('id')
69 return self._extract_video(url, id)
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 json_url = 'http://org-www.arte.tv/papi/tvguide/videos/stream/player/F/%s_PLUS7-F/ALL/ALL.json' % video_id
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):
117 """Extract from videos.arte.tv"""
118 config_xml_url = url.replace('/videos/', '/do_delegate/videos/')
119 config_xml_url = config_xml_url.replace('.html', ',view,asPlayerXml.xml')
120 config_xml = self._download_webpage(config_xml_url, video_id)
121 config_xml_url = self._html_search_regex(r'<video lang=".*?" ref="(.*?)"', config_xml, 'config xml url')
122 config_xml = self._download_webpage(config_xml_url, video_id)
123
124 video_urls = list(re.finditer(r'<url quality="(?P<quality>.*?)">(?P<url>.*?)</url>', config_xml))
125 def _key(m):
126 quality = m.group('quality')
127 if quality == 'hd':
128 return 2
129 else:
130 return 1
131 # We pick the best quality
132 video_urls = sorted(video_urls, key=_key)
133 video_url = list(video_urls)[-1].group('url')
134
135 title = self._html_search_regex(r'<name>(.*?)</name>', config_xml, 'title')
136 thumbnail = self._html_search_regex(r'<firstThumbnailUrl>(.*?)</firstThumbnailUrl>',
137 config_xml, 'thumbnail')
138 return {'id': video_id,
139 'title': title,
140 'thumbnail': thumbnail,
141 'url': video_url,
142 'ext': 'flv',
143 }