[sverigesradio] Add extractor
[youtube-dl] / youtube_dl / extractor / sverigesradio.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import int_or_none
6
7
8 class SverigesRadioBaseIE(InfoExtractor):
9     _BASE_URL = 'https://sverigesradio.se/sida/playerajax'
10     _QUALITIES = ['high', 'medium', 'low']
11     _CODING_FORMATS = {
12         5: {'acodec': 'mp3', 'abr': 128},
13         11: {'acodec': 'aac', 'abr': 192},
14         12: {'acodec': 'aac', 'abr': 32},
15         13: {'acodec': 'aac', 'abr': 96},
16     }
17
18     def _extract_formats(self, query, audio_id, audio_type):
19         audiourls = {}
20         for quality in self._QUALITIES:
21             audiourl = self._download_json(
22                 self._BASE_URL + '/getaudiourl', audio_id,
23                 fatal=True,
24                 query=dict(query, type=audio_type, quality=quality, format='iis'))
25             if audiourl is None:
26                 continue
27
28             # for some reason url can be empty, skip if so
29             # also skip if url has already been seen (quality parameter is ignored?)
30             url = audiourl.get('audioUrl')
31             if url is None or url == "" or url in audiourls:
32                 continue
33
34             audioformat = {'vcodec': 'none', 'url': url}
35             # add codec and bitrate if known coding format
36             codingformat = audiourl.get('codingFormat')
37             if codingformat:
38                 audioformat.update(self._CODING_FORMATS.get(codingformat, {}))
39
40             audiourls[url] = audioformat
41
42         return audiourls.values()
43
44     def _extract_audio(self, audio_type, url):
45         audio_id = self._match_id(url)
46         query = {'id': audio_id, 'type': audio_type}
47
48         metadata = self._download_json(self._BASE_URL + '/audiometadata', audio_id, query=query)
49         item = metadata['items'][0]
50
51         formats = self._extract_formats(query, audio_id, audio_type)
52         self._sort_formats(formats)
53
54         return {
55             'id': audio_id,
56             'title': item['subtitle'],
57             'formats': formats,
58             'series': item.get('title'),
59             'duration': int_or_none(item.get('duration')),
60             'thumbnail': item.get('displayimageurl'),
61             'description': item.get('description'),
62         }
63
64
65 class SverigesRadioPublicationIE(SverigesRadioBaseIE):
66     _VALID_URL = r'https?://(?:www\.)?sverigesradio\.se/sida/(?:artikel|gruppsida)\.aspx\?.*artikel=(?P<id>[0-9]+)'
67     _TESTS = [{
68         'url': 'https://sverigesradio.se/sida/artikel.aspx?programid=83&artikel=7038546',
69         'md5': '6a4917e1923fccb080e5a206a5afa542',
70         'info_dict': {
71             'id': '7038546',
72             'ext': 'm4a',
73             'duration': 132,
74             'series': 'Nyheter (Ekot)',
75             'title': 'Esa Teittinen: Sanningen har inte kommit fram',
76             'description': 'md5:daf7ce66a8f0a53d5465a5984d3839df',
77             'thumbnail': 're:^https://static-cdn.sr.se/sida/images/',
78         },
79     }, {
80         'url': 'https://sverigesradio.se/sida/gruppsida.aspx?programid=3304&grupp=6247&artikel=7146887',
81         'only_matching': True,
82     }]
83
84     def _real_extract(self, url):
85         return self._extract_audio('publication', url)
86
87
88 class SverigesRadioEpisodeIE(SverigesRadioBaseIE):
89     _VALID_URL = r'https?://(?:www\.)?sverigesradio\.se/(?:sida/)?avsnitt/(?P<id>[0-9]+)'
90     _TEST = {
91         'url': 'https://sverigesradio.se/avsnitt/1140922?programid=1300',
92         'md5': '20dc4d8db24228f846be390b0c59a07c',
93         'info_dict': {
94             'id': '1140922',
95             'ext': 'mp3',
96             'duration': 3307,
97             'series': 'Konflikt',
98             'title': 'Metoo och valen',
99             'description': 'md5:fcb5c1f667f00badcc702b196f10a27e',
100             'thumbnail': 're:^https://static-cdn.sr.se/sida/images/'
101         }
102     }
103
104     def _real_extract(self, url):
105         return self._extract_audio('episode', url)