tunein.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import urllib.parse
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. OnDemandPagedList,
  5. determine_ext,
  6. parse_iso8601,
  7. traverse_obj,
  8. )
  9. class TuneInBaseIE(InfoExtractor):
  10. _VALID_URL_BASE = r'https?://(?:www\.)?tunein\.com'
  11. def _extract_metadata(self, webpage, content_id):
  12. return self._search_json(r'window.INITIAL_STATE=', webpage, 'hydration', content_id, fatal=False)
  13. def _extract_formats_and_subtitles(self, content_id):
  14. streams = self._download_json(
  15. f'https://opml.radiotime.com/Tune.ashx?render=json&formats=mp3,aac,ogg,flash,hls&id={content_id}',
  16. content_id)['body']
  17. formats, subtitles = [], {}
  18. for stream in streams:
  19. if stream.get('media_type') == 'hls':
  20. fmts, subs = self._extract_m3u8_formats_and_subtitles(stream['url'], content_id, fatal=False)
  21. formats.extend(fmts)
  22. self._merge_subtitles(subs, target=subtitles)
  23. elif determine_ext(stream['url']) == 'pls':
  24. playlist_content = self._download_webpage(stream['url'], content_id)
  25. formats.append({
  26. 'url': self._search_regex(r'File1=(.*)', playlist_content, 'url', fatal=False),
  27. 'abr': stream.get('bitrate'),
  28. 'ext': stream.get('media_type'),
  29. })
  30. else:
  31. formats.append({
  32. 'url': stream['url'],
  33. 'abr': stream.get('bitrate'),
  34. 'ext': stream.get('media_type'),
  35. })
  36. return formats, subtitles
  37. class TuneInStationIE(TuneInBaseIE):
  38. _VALID_URL = TuneInBaseIE._VALID_URL_BASE + r'(?:/radio/[^?#]+-|/embed/player/)(?P<id>s\d+)'
  39. _EMBED_REGEX = [r'<iframe[^>]+src=["\'](?P<url>(?:https?://)?tunein\.com/embed/player/s\d+)']
  40. _TESTS = [{
  41. 'url': 'https://tunein.com/radio/Jazz24-885-s34682/',
  42. 'info_dict': {
  43. 'id': 's34682',
  44. 'title': 're:^Jazz24',
  45. 'description': 'md5:d6d0b89063fd68d529fa7058ee98619b',
  46. 'thumbnail': 're:^https?://[^?&]+/s34682',
  47. 'location': 'Seattle-Tacoma, US',
  48. 'ext': 'mp3',
  49. 'live_status': 'is_live',
  50. },
  51. 'params': {
  52. 'skip_download': True,
  53. },
  54. }, {
  55. 'url': 'https://tunein.com/embed/player/s6404/',
  56. 'only_matching': True,
  57. }, {
  58. 'url': 'https://tunein.com/radio/BBC-Radio-1-988-s24939/',
  59. 'info_dict': {
  60. 'id': 's24939',
  61. 'title': 're:^BBC Radio 1',
  62. 'description': 'md5:f3f75f7423398d87119043c26e7bfb84',
  63. 'thumbnail': 're:^https?://[^?&]+/s24939',
  64. 'location': 'London, UK',
  65. 'ext': 'mp3',
  66. 'live_status': 'is_live',
  67. },
  68. 'params': {
  69. 'skip_download': True,
  70. },
  71. }]
  72. def _real_extract(self, url):
  73. station_id = self._match_id(url)
  74. webpage = self._download_webpage(url, station_id)
  75. metadata = self._extract_metadata(webpage, station_id)
  76. formats, subtitles = self._extract_formats_and_subtitles(station_id)
  77. return {
  78. 'id': station_id,
  79. 'title': traverse_obj(metadata, ('profiles', station_id, 'title')),
  80. 'description': traverse_obj(metadata, ('profiles', station_id, 'description')),
  81. 'thumbnail': traverse_obj(metadata, ('profiles', station_id, 'image')),
  82. 'timestamp': parse_iso8601(
  83. traverse_obj(metadata, ('profiles', station_id, 'actions', 'play', 'publishTime'))),
  84. 'location': traverse_obj(
  85. metadata, ('profiles', station_id, 'metadata', 'properties', 'location', 'displayName'),
  86. ('profiles', station_id, 'properties', 'location', 'displayName')),
  87. 'formats': formats,
  88. 'subtitles': subtitles,
  89. 'is_live': traverse_obj(metadata, ('profiles', station_id, 'actions', 'play', 'isLive')),
  90. }
  91. class TuneInPodcastIE(TuneInBaseIE):
  92. _VALID_URL = TuneInBaseIE._VALID_URL_BASE + r'/(?:podcasts/[^?#]+-|embed/player/)(?P<id>p\d+)/?(?:#|$)'
  93. _EMBED_REGEX = [r'<iframe[^>]+src=["\'](?P<url>(?:https?://)?tunein\.com/embed/player/p\d+)']
  94. _TESTS = [{
  95. 'url': 'https://tunein.com/podcasts/Technology-Podcasts/Artificial-Intelligence-p1153019',
  96. 'info_dict': {
  97. 'id': 'p1153019',
  98. 'title': 'Lex Fridman Podcast',
  99. 'description': 'md5:bedc4e5f1c94f7dec6e4317b5654b00d',
  100. },
  101. 'playlist_mincount': 200,
  102. }, {
  103. 'url': 'https://tunein.com/embed/player/p191660/',
  104. 'only_matching': True,
  105. }, {
  106. 'url': 'https://tunein.com/podcasts/World-News/BBC-News-p14/',
  107. 'info_dict': {
  108. 'id': 'p14',
  109. 'title': 'BBC News',
  110. 'description': 'md5:1218e575eeaff75f48ed978261fa2068',
  111. },
  112. 'playlist_mincount': 200,
  113. }]
  114. _PAGE_SIZE = 30
  115. def _real_extract(self, url):
  116. podcast_id = self._match_id(url)
  117. webpage = self._download_webpage(url, podcast_id, fatal=False)
  118. metadata = self._extract_metadata(webpage, podcast_id)
  119. def page_func(page_num):
  120. api_response = self._download_json(
  121. f'https://api.tunein.com/profiles/{podcast_id}/contents', podcast_id,
  122. note=f'Downloading page {page_num + 1}', query={
  123. 'filter': 't:free',
  124. 'offset': page_num * self._PAGE_SIZE,
  125. 'limit': self._PAGE_SIZE,
  126. })
  127. return [
  128. self.url_result(
  129. f'https://tunein.com/podcasts/{podcast_id}?topicId={episode["GuideId"][1:]}',
  130. TuneInPodcastEpisodeIE, title=episode.get('Title'))
  131. for episode in api_response['Items']]
  132. entries = OnDemandPagedList(page_func, self._PAGE_SIZE)
  133. return self.playlist_result(
  134. entries, playlist_id=podcast_id, title=traverse_obj(metadata, ('profiles', podcast_id, 'title')),
  135. description=traverse_obj(metadata, ('profiles', podcast_id, 'description')))
  136. class TuneInPodcastEpisodeIE(TuneInBaseIE):
  137. _VALID_URL = TuneInBaseIE._VALID_URL_BASE + r'/podcasts/(?:[^?&]+-)?(?P<podcast_id>p\d+)/?\?topicId=(?P<id>\w\d+)'
  138. _TESTS = [{
  139. 'url': 'https://tunein.com/podcasts/Technology-Podcasts/Artificial-Intelligence-p1153019/?topicId=236404354',
  140. 'info_dict': {
  141. 'id': 't236404354',
  142. 'title': '#351 \u2013 MrBeast: Future of YouTube, Twitter, TikTok, and Instagram',
  143. 'description': 'md5:e1734db6f525e472c0c290d124a2ad77',
  144. 'thumbnail': 're:^https?://[^?&]+/p1153019',
  145. 'timestamp': 1673458571,
  146. 'upload_date': '20230111',
  147. 'series_id': 'p1153019',
  148. 'series': 'Lex Fridman Podcast',
  149. 'ext': 'mp3',
  150. },
  151. }]
  152. def _real_extract(self, url):
  153. podcast_id, episode_id = self._match_valid_url(url).group('podcast_id', 'id')
  154. episode_id = f't{episode_id}'
  155. webpage = self._download_webpage(url, episode_id)
  156. metadata = self._extract_metadata(webpage, episode_id)
  157. formats, subtitles = self._extract_formats_and_subtitles(episode_id)
  158. return {
  159. 'id': episode_id,
  160. 'title': traverse_obj(metadata, ('profiles', episode_id, 'title')),
  161. 'description': traverse_obj(metadata, ('profiles', episode_id, 'description')),
  162. 'thumbnail': traverse_obj(metadata, ('profiles', episode_id, 'image')),
  163. 'timestamp': parse_iso8601(
  164. traverse_obj(metadata, ('profiles', episode_id, 'actions', 'play', 'publishTime'))),
  165. 'series_id': podcast_id,
  166. 'series': traverse_obj(metadata, ('profiles', podcast_id, 'title')),
  167. 'formats': formats,
  168. 'subtitles': subtitles,
  169. }
  170. class TuneInShortenerIE(InfoExtractor):
  171. IE_NAME = 'tunein:shortener'
  172. IE_DESC = False # Do not list
  173. _VALID_URL = r'https?://tun\.in/(?P<id>[A-Za-z0-9]+)'
  174. _TEST = {
  175. # test redirection
  176. 'url': 'http://tun.in/ser7s',
  177. 'info_dict': {
  178. 'id': 's34682',
  179. 'title': 're:^Jazz24',
  180. 'description': 'md5:d6d0b89063fd68d529fa7058ee98619b',
  181. 'thumbnail': 're:^https?://[^?&]+/s34682',
  182. 'location': 'Seattle-Tacoma, US',
  183. 'ext': 'mp3',
  184. 'live_status': 'is_live',
  185. },
  186. 'params': {
  187. 'skip_download': True, # live stream
  188. },
  189. }
  190. def _real_extract(self, url):
  191. redirect_id = self._match_id(url)
  192. # The server doesn't support HEAD requests
  193. urlh = self._request_webpage(
  194. url, redirect_id, note='Downloading redirect page')
  195. url = urlh.url
  196. url_parsed = urllib.parse.urlparse(url)
  197. if url_parsed.port == 443:
  198. url = url_parsed._replace(netloc=url_parsed.hostname).url
  199. self.to_screen(f'Following redirect: {url}')
  200. return self.url_result(url)