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