]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/sverigesradio.py
d/rules: Run offline tests.
[youtubedl] / 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 (
6 determine_ext,
7 int_or_none,
8 str_or_none,
9 )
10
11
12 class SverigesRadioBaseIE(InfoExtractor):
13 _BASE_URL = 'https://sverigesradio.se/sida/playerajax/'
14 _QUALITIES = ['low', 'medium', 'high']
15 _EXT_TO_CODEC_MAP = {
16 'mp3': 'mp3',
17 'm4a': 'aac',
18 }
19 _CODING_FORMAT_TO_ABR_MAP = {
20 5: 128,
21 11: 192,
22 12: 32,
23 13: 96,
24 }
25
26 def _real_extract(self, url):
27 audio_id = self._match_id(url)
28 query = {
29 'id': audio_id,
30 'type': self._AUDIO_TYPE,
31 }
32
33 item = self._download_json(
34 self._BASE_URL + 'audiometadata', audio_id,
35 'Downloading audio JSON metadata', query=query)['items'][0]
36 title = item['subtitle']
37
38 query['format'] = 'iis'
39 urls = []
40 formats = []
41 for quality in self._QUALITIES:
42 query['quality'] = quality
43 audio_url_data = self._download_json(
44 self._BASE_URL + 'getaudiourl', audio_id,
45 'Downloading %s format JSON metadata' % quality,
46 fatal=False, query=query) or {}
47 audio_url = audio_url_data.get('audioUrl')
48 if not audio_url or audio_url in urls:
49 continue
50 urls.append(audio_url)
51 ext = determine_ext(audio_url)
52 coding_format = audio_url_data.get('codingFormat')
53 abr = int_or_none(self._search_regex(
54 r'_a(\d+)\.m4a', audio_url, 'audio bitrate',
55 default=None)) or self._CODING_FORMAT_TO_ABR_MAP.get(coding_format)
56 formats.append({
57 'abr': abr,
58 'acodec': self._EXT_TO_CODEC_MAP.get(ext),
59 'ext': ext,
60 'format_id': str_or_none(coding_format),
61 'vcodec': 'none',
62 'url': audio_url,
63 })
64 self._sort_formats(formats)
65
66 return {
67 'id': audio_id,
68 'title': title,
69 'formats': formats,
70 'series': item.get('title'),
71 'duration': int_or_none(item.get('duration')),
72 'thumbnail': item.get('displayimageurl'),
73 'description': item.get('description'),
74 }
75
76
77 class SverigesRadioPublicationIE(SverigesRadioBaseIE):
78 IE_NAME = 'sverigesradio:publication'
79 _VALID_URL = r'https?://(?:www\.)?sverigesradio\.se/sida/(?:artikel|gruppsida)\.aspx\?.*?\bartikel=(?P<id>[0-9]+)'
80 _TESTS = [{
81 'url': 'https://sverigesradio.se/sida/artikel.aspx?programid=83&artikel=7038546',
82 'md5': '6a4917e1923fccb080e5a206a5afa542',
83 'info_dict': {
84 'id': '7038546',
85 'ext': 'm4a',
86 'duration': 132,
87 'series': 'Nyheter (Ekot)',
88 'title': 'Esa Teittinen: Sanningen har inte kommit fram',
89 'description': 'md5:daf7ce66a8f0a53d5465a5984d3839df',
90 'thumbnail': r're:^https?://.*\.jpg',
91 },
92 }, {
93 'url': 'https://sverigesradio.se/sida/gruppsida.aspx?programid=3304&grupp=6247&artikel=7146887',
94 'only_matching': True,
95 }]
96 _AUDIO_TYPE = 'publication'
97
98
99 class SverigesRadioEpisodeIE(SverigesRadioBaseIE):
100 IE_NAME = 'sverigesradio:episode'
101 _VALID_URL = r'https?://(?:www\.)?sverigesradio\.se/(?:sida/)?avsnitt/(?P<id>[0-9]+)'
102 _TEST = {
103 'url': 'https://sverigesradio.se/avsnitt/1140922?programid=1300',
104 'md5': '20dc4d8db24228f846be390b0c59a07c',
105 'info_dict': {
106 'id': '1140922',
107 'ext': 'mp3',
108 'duration': 3307,
109 'series': 'Konflikt',
110 'title': 'Metoo och valen',
111 'description': 'md5:fcb5c1f667f00badcc702b196f10a27e',
112 'thumbnail': r're:^https?://.*\.jpg',
113 }
114 }
115 _AUDIO_TYPE = 'episode'