]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/tunein.py
e3d3f2a96bfd4ee5ca196b1ad90e755b0ff6b274
[yt-dlp.git] / yt_dlp / extractor / tunein.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import ExtractorError
5 from ..compat import compat_urlparse
6
7
8 class TuneInBaseIE(InfoExtractor):
9 _API_BASE_URL = 'http://tunein.com/tuner/tune/'
10
11 @staticmethod
12 def _extract_urls(webpage):
13 return re.findall(
14 r'<iframe[^>]+src=["\'](?P<url>(?:https?://)?tunein\.com/embed/player/[pst]\d+)',
15 webpage)
16
17 def _real_extract(self, url):
18 content_id = self._match_id(url)
19
20 content_info = self._download_json(
21 self._API_BASE_URL + self._API_URL_QUERY % content_id,
22 content_id, note='Downloading JSON metadata')
23
24 title = content_info['Title']
25 thumbnail = content_info.get('Logo')
26 location = content_info.get('Location')
27 streams_url = content_info.get('StreamUrl')
28 if not streams_url:
29 raise ExtractorError('No downloadable streams found', expected=True)
30 if not streams_url.startswith('http://'):
31 streams_url = compat_urlparse.urljoin(url, streams_url)
32
33 streams = self._download_json(
34 streams_url, content_id, note='Downloading stream data',
35 transform_source=lambda s: re.sub(r'^\s*\((.*)\);\s*$', r'\1', s))['Streams']
36
37 is_live = None
38 formats = []
39 for stream in streams:
40 if stream.get('Type') == 'Live':
41 is_live = True
42 reliability = stream.get('Reliability')
43 format_note = (
44 'Reliability: %d%%' % reliability
45 if reliability is not None else None)
46 formats.append({
47 'preference': (
48 0 if reliability is None or reliability > 90
49 else 1),
50 'abr': stream.get('Bandwidth'),
51 'ext': stream.get('MediaType').lower(),
52 'acodec': stream.get('MediaType'),
53 'vcodec': 'none',
54 'url': stream.get('Url'),
55 'source_preference': reliability,
56 'format_note': format_note,
57 })
58 self._sort_formats(formats)
59
60 return {
61 'id': content_id,
62 'title': title,
63 'formats': formats,
64 'thumbnail': thumbnail,
65 'location': location,
66 'is_live': is_live,
67 }
68
69
70 class TuneInClipIE(TuneInBaseIE):
71 IE_NAME = 'tunein:clip'
72 _VALID_URL = r'https?://(?:www\.)?tunein\.com/station/.*?audioClipId\=(?P<id>\d+)'
73 _API_URL_QUERY = '?tuneType=AudioClip&audioclipId=%s'
74
75 _TESTS = [{
76 'url': 'http://tunein.com/station/?stationId=246119&audioClipId=816',
77 'md5': '99f00d772db70efc804385c6b47f4e77',
78 'info_dict': {
79 'id': '816',
80 'title': '32m',
81 'ext': 'mp3',
82 },
83 }]
84
85
86 class TuneInStationIE(TuneInBaseIE):
87 IE_NAME = 'tunein:station'
88 _VALID_URL = r'https?://(?:www\.)?tunein\.com/(?:radio/.*?-s|station/.*?StationId=|embed/player/s)(?P<id>\d+)'
89 _API_URL_QUERY = '?tuneType=Station&stationId=%s'
90
91 @classmethod
92 def suitable(cls, url):
93 return False if TuneInClipIE.suitable(url) else super(TuneInStationIE, cls).suitable(url)
94
95 _TESTS = [{
96 'url': 'http://tunein.com/radio/Jazz24-885-s34682/',
97 'info_dict': {
98 'id': '34682',
99 'title': 'Jazz 24 on 88.5 Jazz24 - KPLU-HD2',
100 'ext': 'mp3',
101 'location': 'Tacoma, WA',
102 },
103 'params': {
104 'skip_download': True, # live stream
105 },
106 }, {
107 'url': 'http://tunein.com/embed/player/s6404/',
108 'only_matching': True,
109 }]
110
111
112 class TuneInProgramIE(TuneInBaseIE):
113 IE_NAME = 'tunein:program'
114 _VALID_URL = r'https?://(?:www\.)?tunein\.com/(?:radio/.*?-p|program/.*?ProgramId=|embed/player/p)(?P<id>\d+)'
115 _API_URL_QUERY = '?tuneType=Program&programId=%s'
116
117 _TESTS = [{
118 'url': 'http://tunein.com/radio/Jazz-24-p2506/',
119 'info_dict': {
120 'id': '2506',
121 'title': 'Jazz 24 on 91.3 WUKY-HD3',
122 'ext': 'mp3',
123 'location': 'Lexington, KY',
124 },
125 'params': {
126 'skip_download': True, # live stream
127 },
128 }, {
129 'url': 'http://tunein.com/embed/player/p191660/',
130 'only_matching': True,
131 }]
132
133
134 class TuneInTopicIE(TuneInBaseIE):
135 IE_NAME = 'tunein:topic'
136 _VALID_URL = r'https?://(?:www\.)?tunein\.com/(?:topic/.*?TopicId=|embed/player/t)(?P<id>\d+)'
137 _API_URL_QUERY = '?tuneType=Topic&topicId=%s'
138
139 _TESTS = [{
140 'url': 'http://tunein.com/topic/?TopicId=101830576',
141 'md5': 'c31a39e6f988d188252eae7af0ef09c9',
142 'info_dict': {
143 'id': '101830576',
144 'title': 'Votez pour moi du 29 octobre 2015 (29/10/15)',
145 'ext': 'mp3',
146 'location': 'Belgium',
147 },
148 }, {
149 'url': 'http://tunein.com/embed/player/t101830576/',
150 'only_matching': True,
151 }]
152
153
154 class TuneInShortenerIE(InfoExtractor):
155 IE_NAME = 'tunein:shortener'
156 IE_DESC = False # Do not list
157 _VALID_URL = r'https?://tun\.in/(?P<id>[A-Za-z0-9]+)'
158
159 _TEST = {
160 # test redirection
161 'url': 'http://tun.in/ser7s',
162 'info_dict': {
163 'id': '34682',
164 'title': 'Jazz 24 on 88.5 Jazz24 - KPLU-HD2',
165 'ext': 'mp3',
166 'location': 'Tacoma, WA',
167 },
168 'params': {
169 'skip_download': True, # live stream
170 },
171 }
172
173 def _real_extract(self, url):
174 redirect_id = self._match_id(url)
175 # The server doesn't support HEAD requests
176 urlh = self._request_webpage(
177 url, redirect_id, note='Downloading redirect page')
178 url = urlh.geturl()
179 self.to_screen('Following redirect: %s' % url)
180 return self.url_result(url)