aboutsummaryrefslogtreecommitdiffstats
path: root/hypervideo_dl/extractor/agora.py
blob: abb2d3ff27e3fd1b15cdca3517a11baf25637b60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import functools
import uuid

from .common import InfoExtractor
from ..utils import (
    ExtractorError,
    OnDemandPagedList,
    int_or_none,
    month_by_name,
    parse_duration,
    try_call,
)


class WyborczaVideoIE(InfoExtractor):
    # this id is not an article id, it has to be extracted from the article
    _VALID_URL = r'(?:wyborcza:video:|https?://wyborcza\.pl/(?:api-)?video/)(?P<id>\d+)'
    IE_NAME = 'wyborcza:video'
    _TESTS = [{
        'url': 'wyborcza:video:26207634',
        'info_dict': {
            'id': '26207634',
            'ext': 'mp4',
            'title': '- Polska w 2020 r. jest innym państwem niż w 2015 r. Nie zmieniła się konstytucja, ale jest to już inny ustrój - mówi Adam Bodnar',
            'description': ' ',
            'uploader': 'Dorota Roman',
            'duration': 2474,
            'thumbnail': r're:https://.+\.jpg',
        },
    }, {
        'url': 'https://wyborcza.pl/video/26207634',
        'only_matching': True,
    }, {
        'url': 'https://wyborcza.pl/api-video/26207634',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        meta = self._download_json(f'https://wyborcza.pl/api-video/{video_id}', video_id)

        formats = []
        base_url = meta['redirector'].replace('http://', 'https://') + meta['basePath']
        for quality in ('standard', 'high'):
            if not meta['files'].get(quality):
                continue
            formats.append({
                'url': base_url + meta['files'][quality],
                'height': int_or_none(
                    self._search_regex(
                        r'p(\d+)[a-z]+\.mp4$', meta['files'][quality],
                        'mp4 video height', default=None)),
                'format_id': quality,
            })
        if meta['files'].get('dash'):
            formats.extend(self._extract_mpd_formats(base_url + meta['files']['dash'], video_id))

        return {
            'id': video_id,
            'formats': formats,
            'title': meta.get('title'),
            'description': meta.get('lead'),
            'uploader': meta.get('signature'),
            'thumbnail': meta.get('imageUrl'),
            'duration': meta.get('duration'),
        }


class WyborczaPodcastIE(InfoExtractor):
    _VALID_URL = r'''(?x)
        https?://(?:www\.)?(?:
            wyborcza\.pl/podcast(?:/0,172673\.html)?|
            wysokieobcasy\.pl/wysokie-obcasy/0,176631\.html
        )(?:\?(?:[^&#]+?&)*podcast=(?P<id>\d+))?
    '''
    _TESTS = [{
        'url': 'https://wyborcza.pl/podcast/0,172673.html?podcast=100720#S.main_topic-K.C-B.6-L.1.podcast',
        'info_dict': {
            'id': '100720',
            'ext': 'mp3',
            'title': 'Cyfrodziewczyny. Kim były pionierki polskiej informatyki ',
            'uploader': 'Michał Nogaś ',
            'upload_date': '20210117',
            'description': 'md5:49f0a06ffc4c1931210d3ab1416a651d',
            'duration': 3684.0,
            'thumbnail': r're:https://.+\.jpg',
        },
    }, {
        'url': 'https://www.wysokieobcasy.pl/wysokie-obcasy/0,176631.html?podcast=100673',
        'info_dict': {
            'id': '100673',
            'ext': 'mp3',
            'title': 'Czym jest ubóstwo menstruacyjne i dlaczego dotyczy każdej i każdego z nas?',
            'uploader': 'Agnieszka Urazińska ',
            'upload_date': '20210115',
            'description': 'md5:c161dc035f8dbb60077011fc41274899',
            'duration': 1803.0,
            'thumbnail': r're:https://.+\.jpg',
        },
    }, {
        'url': 'https://wyborcza.pl/podcast',
        'info_dict': {
            'id': '334',
            'title': 'Gościnnie: Wyborcza, 8:10',
            'series': 'Gościnnie: Wyborcza, 8:10',
        },
        'playlist_mincount': 370,
    }, {
        'url': 'https://www.wysokieobcasy.pl/wysokie-obcasy/0,176631.html',
        'info_dict': {
            'id': '395',
            'title': 'Gościnnie: Wysokie Obcasy',
            'series': 'Gościnnie: Wysokie Obcasy',
        },
        'playlist_mincount': 12,
    }]

    def _real_extract(self, url):
        podcast_id = self._match_id(url)

        if not podcast_id:  # playlist
            podcast_id = '395' if 'wysokieobcasy.pl/' in url else '334'
            return self.url_result(TokFMAuditionIE._create_url(podcast_id), TokFMAuditionIE, podcast_id)

        meta = self._download_json('https://wyborcza.pl/api/podcast', podcast_id,
                                   query={'guid': podcast_id, 'type': 'wo' if 'wysokieobcasy.pl/' in url else None})

        day, month, year = self._search_regex(r'^(\d\d?) (\w+) (\d{4})$', meta.get('publishedDate'),
                                              'upload date', group=(1, 2, 3), default=(None, None, None))
        return {
            'id': podcast_id,
            'url': meta['url'],
            'title': meta.get('title'),
            'description': meta.get('description'),
            'thumbnail': meta.get('imageUrl'),
            'duration': parse_duration(meta.get('duration')),
            'uploader': meta.get('author'),
            'upload_date': try_call(lambda: f'{year}{month_by_name(month, lang="pl"):0>2}{day:0>2}'),
        }


class TokFMPodcastIE(InfoExtractor):
    _VALID_URL = r'(?:https?://audycje\.tokfm\.pl/podcast/|tokfm:podcast:)(?P<id>\d+),?'
    IE_NAME = 'tokfm:podcast'
    _TESTS = [{
        'url': 'https://audycje.tokfm.pl/podcast/91275,-Systemowy-rasizm-Czy-zamieszki-w-USA-po-morderstwie-w-Minneapolis-doprowadza-do-zmian-w-sluzbach-panstwowych',
        'info_dict': {
            'id': '91275',
            'ext': 'aac',
            'title': 'md5:a9b15488009065556900169fb8061cce',
            'episode': 'md5:a9b15488009065556900169fb8061cce',
            'series': 'Analizy',
        },
    }]

    def _real_extract(self, url):
        media_id = self._match_id(url)

        # in case it breaks see this but it returns a lot of useless data
        # https://api.podcast.radioagora.pl/api4/getPodcasts?podcast_id=100091&with_guests=true&with_leaders_for_mobile=true
        metadata = self._download_json(
            f'https://audycje.tokfm.pl/getp/3{media_id}', media_id, 'Downloading podcast metadata')
        if not metadata:
            raise ExtractorError('No such podcast', expected=True)
        metadata = metadata[0]

        formats = []
        for ext in ('aac', 'mp3'):
            url_data = self._download_json(
                f'https://api.podcast.radioagora.pl/api4/getSongUrl?podcast_id={media_id}&device_id={uuid.uuid4()}&ppre=false&audio={ext}',
                media_id, 'Downloading podcast %s URL' % ext)
            # prevents inserting the mp3 (default) multiple times
            if 'link_ssl' in url_data and f'.{ext}' in url_data['link_ssl']:
                formats.append({
                    'url': url_data['link_ssl'],
                    'ext': ext,
                    'vcodec': 'none',
                    'acodec': ext,
                })

        return {
            'id': media_id,
            'formats': formats,
            'title': metadata.get('podcast_name'),
            'series': metadata.get('series_name'),
            'episode': metadata.get('podcast_name'),
        }


class TokFMAuditionIE(InfoExtractor):
    _VALID_URL = r'(?:https?://audycje\.tokfm\.pl/audycja/|tokfm:audition:)(?P<id>\d+),?'
    IE_NAME = 'tokfm:audition'
    _TESTS = [{
        'url': 'https://audycje.tokfm.pl/audycja/218,Analizy',
        'info_dict': {
            'id': '218',
            'title': 'Analizy',
            'series': 'Analizy',
        },
        'playlist_count': 1635,
    }]

    _PAGE_SIZE = 30
    _HEADERS = {
        'User-Agent': 'Mozilla/5.0 (Linux; Android 9; Redmi 3S Build/PQ3A.190801.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/87.0.4280.101 Mobile Safari/537.36',
    }

    @staticmethod
    def _create_url(id):
        return f'https://audycje.tokfm.pl/audycja/{id}'

    def _real_extract(self, url):
        audition_id = self._match_id(url)

        data = self._download_json(
            f'https://api.podcast.radioagora.pl/api4/getSeries?series_id={audition_id}',
            audition_id, 'Downloading audition metadata', headers=self._HEADERS)
        if not data:
            raise ExtractorError('No such audition', expected=True)
        data = data[0]

        entries = OnDemandPagedList(functools.partial(
            self._fetch_page, audition_id, data), self._PAGE_SIZE)

        return {
            '_type': 'playlist',
            'id': audition_id,
            'title': data.get('series_name'),
            'series': data.get('series_name'),
            'entries': entries,
        }

    def _fetch_page(self, audition_id, data, page):
        for retry in self.RetryManager():
            podcast_page = self._download_json(
                f'https://api.podcast.radioagora.pl/api4/getPodcasts?series_id={audition_id}&limit=30&offset={page}&with_guests=true&with_leaders_for_mobile=true',
                audition_id, f'Downloading podcast list page {page + 1}', headers=self._HEADERS)
            if not podcast_page:
                retry.error = ExtractorError('Agora returned empty page', expected=True)

        for podcast in podcast_page:
            yield {
                '_type': 'url_transparent',
                'url': podcast['podcast_sharing_url'],
                'ie_key': TokFMPodcastIE.ie_key(),
                'title': podcast.get('podcast_name'),
                'episode': podcast.get('podcast_name'),
                'description': podcast.get('podcast_description'),
                'timestamp': int_or_none(podcast.get('podcast_timestamp')),
                'series': data.get('series_name'),
            }