You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.8 KiB

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