2 from __future__ import unicode_literals
6 from .common import InfoExtractor
7 from ..utils import ExtractorError
8 from ..compat import compat_urlparse
11 class TuneInBaseIE(InfoExtractor):
12 _API_BASE_URL = 'http://tunein.com/tuner/tune/'
14 def _real_extract(self, url):
15 content_id = self._match_id(url)
17 content_info = self._download_json(
18 self._API_BASE_URL + self._API_URL_QUERY % content_id,
19 content_id, note='Downloading JSON metadata')
21 title = content_info['Title']
22 thumbnail = content_info.get('Logo')
23 location = content_info.get('Location')
24 streams_url = content_info.get('StreamUrl')
26 raise ExtractorError('No downloadable streams found', expected=True)
27 if not streams_url.startswith('http://'):
28 streams_url = compat_urlparse.urljoin(url, streams_url)
30 streams = self._download_json(
31 streams_url, content_id, note='Downloading stream data',
32 transform_source=lambda s: re.sub(r'^\s*\((.*)\);\s*$', r'\1', s))['Streams']
36 for stream in streams:
37 if stream.get('Type') == 'Live':
39 reliability = stream.get('Reliability')
41 'Reliability: %d%%' % reliability
42 if reliability is not None else None)
45 0 if reliability is None or reliability > 90
47 'abr': stream.get('Bandwidth'),
48 'ext': stream.get('MediaType').lower(),
49 'acodec': stream.get('MediaType'),
51 'url': stream.get('Url'),
52 'source_preference': reliability,
53 'format_note': format_note,
55 self._sort_formats(formats)
61 'thumbnail': thumbnail,
67 class TuneInClipIE(TuneInBaseIE):
68 IE_NAME = 'tunein:clip'
69 _VALID_URL = r'https?://(?:www\.)?tunein\.com/station/.*?audioClipId\=(?P<id>\d+)'
70 _API_URL_QUERY = '?tuneType=AudioClip&audioclipId=%s'
74 'url': 'http://tunein.com/station/?stationId=246119&audioClipId=816',
75 'md5': '99f00d772db70efc804385c6b47f4e77',
85 class TuneInStationIE(TuneInBaseIE):
86 IE_NAME = 'tunein:station'
87 _VALID_URL = r'https?://(?:www\.)?tunein\.com/(?:radio/.*?-s|station/.*?StationId\=)(?P<id>\d+)'
88 _API_URL_QUERY = '?tuneType=Station&stationId=%s'
91 def suitable(cls, url):
92 return False if TuneInClipIE.suitable(url) else super(TuneInStationIE, cls).suitable(url)
96 'url': 'http://tunein.com/radio/Jazz24-885-s34682/',
99 'title': 'Jazz 24 on 88.5 Jazz24 - KPLU-HD2',
101 'location': 'Tacoma, WA',
104 'skip_download': True, # live stream
110 class TuneInProgramIE(TuneInBaseIE):
111 IE_NAME = 'tunein:program'
112 _VALID_URL = r'https?://(?:www\.)?tunein\.com/(?:radio/.*?-p|program/.*?ProgramId\=)(?P<id>\d+)'
113 _API_URL_QUERY = '?tuneType=Program&programId=%s'
117 'url': 'http://tunein.com/radio/Jazz-24-p2506/',
120 'title': 'Jazz 24 on 91.3 WUKY-HD3',
122 'location': 'Lexington, KY',
125 'skip_download': True, # live stream
131 class TuneInTopicIE(TuneInBaseIE):
132 IE_NAME = 'tunein:topic'
133 _VALID_URL = r'https?://(?:www\.)?tunein\.com/topic/.*?TopicId\=(?P<id>\d+)'
134 _API_URL_QUERY = '?tuneType=Topic&topicId=%s'
138 'url': 'http://tunein.com/topic/?TopicId=101830576',
139 'md5': 'c31a39e6f988d188252eae7af0ef09c9',
142 'title': 'Votez pour moi du 29 octobre 2015 (29/10/15)',
144 'location': 'Belgium',
150 class TuneInShortenerIE(InfoExtractor):
151 IE_NAME = 'tunein:shortener'
152 IE_DESC = False # Do not list
153 _VALID_URL = r'https?://tun\.in/(?P<id>[A-Za-z0-9]+)'
157 'url': 'http://tun.in/ser7s',
160 'title': 'Jazz 24 on 88.5 Jazz24 - KPLU-HD2',
162 'location': 'Tacoma, WA',
165 'skip_download': True, # live stream
169 def _real_extract(self, url):
170 redirect_id = self._match_id(url)
171 # The server doesn't support HEAD requests
172 urlh = self._request_webpage(
173 url, redirect_id, note='Downloading redirect page')
175 self.to_screen('Following redirect: %s' % url)
176 return self.url_result(url)