]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/digitalconcerthall.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / digitalconcerthall.py
CommitLineData
8bcd4048 1from .common import InfoExtractor
2
3from ..utils import (
4 ExtractorError,
5 parse_resolution,
6 traverse_obj,
7 try_get,
8 urlencode_postdata,
9)
10
11
12class DigitalConcertHallIE(InfoExtractor):
13 IE_DESC = 'DigitalConcertHall extractor'
14 _VALID_URL = r'https?://(?:www\.)?digitalconcerthall\.com/(?P<language>[a-z]+)/concert/(?P<id>[0-9]+)'
15 _OAUTH_URL = 'https://api.digitalconcerthall.com/v2/oauth2/token'
16 _ACCESS_TOKEN = None
17 _NETRC_MACHINE = 'digitalconcerthall'
18 _TESTS = [{
19 'note': 'Playlist with only one video',
20 'url': 'https://www.digitalconcerthall.com/en/concert/53201',
21 'info_dict': {
22 'id': '53201-1',
23 'ext': 'mp4',
24 'composer': 'Kurt Weill',
25 'title': '[Magic Night]',
26 'thumbnail': r're:^https?://images.digitalconcerthall.com/cms/thumbnails.*\.jpg$',
27 'upload_date': '20210624',
28 'timestamp': 1624548600,
29 'duration': 2798,
30 'album_artist': 'Members of the Berliner Philharmoniker / Simon Rössler',
31 },
32 'params': {'skip_download': 'm3u8'},
33 }, {
34 'note': 'Concert with several works and an interview',
35 'url': 'https://www.digitalconcerthall.com/en/concert/53785',
36 'info_dict': {
37 'id': '53785',
38 'album_artist': 'Berliner Philharmoniker / Kirill Petrenko',
39 'title': 'Kirill Petrenko conducts Mendelssohn and Shostakovich',
40 },
41 'params': {'skip_download': 'm3u8'},
42 'playlist_count': 3,
43 }]
44
52efa4b3 45 def _perform_login(self, username, password):
8bcd4048 46 token_response = self._download_json(
47 self._OAUTH_URL,
48 None, 'Obtaining token', errnote='Unable to obtain token', data=urlencode_postdata({
49 'affiliate': 'none',
50 'grant_type': 'device',
51 'device_vendor': 'unknown',
52 'app_id': 'dch.webapp',
53 'app_version': '1.0.0',
54 'client_secret': '2ySLN+2Fwb',
55 }), headers={
56 'Content-Type': 'application/x-www-form-urlencoded',
57 })
58 self._ACCESS_TOKEN = token_response['access_token']
59 try:
60 self._download_json(
61 self._OAUTH_URL,
62 None, note='Logging in', errnote='Unable to login', data=urlencode_postdata({
63 'grant_type': 'password',
64 'username': username,
65 'password': password,
66 }), headers={
67 'Content-Type': 'application/x-www-form-urlencoded',
68 'Referer': 'https://www.digitalconcerthall.com',
69 'Authorization': f'Bearer {self._ACCESS_TOKEN}'
70 })
71 except ExtractorError:
72 self.raise_login_required(msg='Login info incorrect')
73
74 def _real_initialize(self):
52efa4b3 75 if not self._ACCESS_TOKEN:
76 self.raise_login_required(method='password')
8bcd4048 77
78 def _entries(self, items, language, **kwargs):
79 for item in items:
80 video_id = item['id']
81 stream_info = self._download_json(
82 self._proto_relative_url(item['_links']['streams']['href']), video_id, headers={
83 'Accept': 'application/json',
84 'Authorization': f'Bearer {self._ACCESS_TOKEN}',
85 'Accept-Language': language
86 })
87
88 m3u8_url = traverse_obj(
a79cba0c 89 stream_info, ('channel', lambda k, _: k.startswith('vod_mixed'), 'stream', 0, 'url'), get_all=False)
8bcd4048 90 formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4', 'm3u8_native', fatal=False)
8bcd4048 91
92 yield {
93 'id': video_id,
94 'title': item.get('title'),
95 'composer': item.get('name_composer'),
96 'url': m3u8_url,
97 'formats': formats,
98 'duration': item.get('duration_total'),
99 'timestamp': traverse_obj(item, ('date', 'published')),
100 'description': item.get('short_description') or stream_info.get('short_description'),
101 **kwargs,
102 'chapters': [{
103 'start_time': chapter.get('time'),
104 'end_time': try_get(chapter, lambda x: x['time'] + x['duration']),
105 'title': chapter.get('text'),
106 } for chapter in item['cuepoints']] if item.get('cuepoints') else None,
107 }
108
109 def _real_extract(self, url):
110 language, video_id = self._match_valid_url(url).group('language', 'id')
111 if not language:
112 language = 'en'
113
114 thumbnail_url = self._html_search_regex(
115 r'(https?://images\.digitalconcerthall\.com/cms/thumbnails/.*\.jpg)',
116 self._download_webpage(url, video_id), 'thumbnail')
117 thumbnails = [{
118 'url': thumbnail_url,
119 **parse_resolution(thumbnail_url)
120 }]
121
122 vid_info = self._download_json(
123 f'https://api.digitalconcerthall.com/v2/concert/{video_id}', video_id, headers={
124 'Accept': 'application/json',
125 'Accept-Language': language
126 })
127 album_artist = ' / '.join(traverse_obj(vid_info, ('_links', 'artist', ..., 'name')) or '')
128
129 return {
130 '_type': 'playlist',
131 'id': video_id,
132 'title': vid_info.get('title'),
133 'entries': self._entries(traverse_obj(vid_info, ('_embedded', ..., ...)), language,
134 thumbnails=thumbnails, album_artist=album_artist),
135 'thumbnails': thumbnails,
136 'album_artist': album_artist,
137 }