ximalaya.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import math
  2. from .common import InfoExtractor
  3. from ..utils import InAdvancePagedList, str_or_none, traverse_obj, try_call
  4. class XimalayaBaseIE(InfoExtractor):
  5. _GEO_COUNTRIES = ['CN']
  6. class XimalayaIE(XimalayaBaseIE):
  7. IE_NAME = 'ximalaya'
  8. IE_DESC = '喜马拉雅FM'
  9. _VALID_URL = r'https?://(?:www\.|m\.)?ximalaya\.com/(:?(?P<uid>\d+)/)?sound/(?P<id>[0-9]+)'
  10. _TESTS = [
  11. {
  12. 'url': 'http://www.ximalaya.com/sound/47740352/',
  13. 'info_dict': {
  14. 'id': '47740352',
  15. 'ext': 'm4a',
  16. 'uploader': '小彬彬爱听书',
  17. 'uploader_id': '61425525',
  18. 'uploader_url': 'http://www.ximalaya.com/zhubo/61425525/',
  19. 'title': '261.唐诗三百首.卷八.送孟浩然之广陵.李白',
  20. 'description': 'contains:《送孟浩然之广陵》\n作者:李白\n故人西辞黄鹤楼,烟花三月下扬州。\n孤帆远影碧空尽,惟见长江天际流。',
  21. 'thumbnail': r're:^https?://.*\.jpg',
  22. 'thumbnails': [
  23. {
  24. 'name': 'cover_url',
  25. 'url': r're:^https?://.*\.jpg',
  26. },
  27. {
  28. 'name': 'cover_url_142',
  29. 'url': r're:^https?://.*\.jpg',
  30. 'width': 180,
  31. 'height': 180,
  32. },
  33. ],
  34. 'categories': ['其他'],
  35. 'duration': 93,
  36. 'view_count': int,
  37. 'like_count': int,
  38. },
  39. },
  40. {
  41. 'url': 'http://m.ximalaya.com/61425525/sound/47740352/',
  42. 'info_dict': {
  43. 'id': '47740352',
  44. 'ext': 'm4a',
  45. 'uploader': '小彬彬爱听书',
  46. 'uploader_id': '61425525',
  47. 'uploader_url': 'http://www.ximalaya.com/zhubo/61425525/',
  48. 'title': '261.唐诗三百首.卷八.送孟浩然之广陵.李白',
  49. 'description': 'contains:《送孟浩然之广陵》\n作者:李白\n故人西辞黄鹤楼,烟花三月下扬州。\n孤帆远影碧空尽,惟见长江天际流。',
  50. 'thumbnail': r're:^https?://.*\.jpg',
  51. 'thumbnails': [
  52. {
  53. 'name': 'cover_url',
  54. 'url': r're:^https?://.*\.jpg',
  55. },
  56. {
  57. 'name': 'cover_url_142',
  58. 'url': r're:^https?://.*\.jpg',
  59. 'width': 180,
  60. 'height': 180,
  61. },
  62. ],
  63. 'categories': ['人文'],
  64. 'duration': 93,
  65. 'view_count': int,
  66. 'like_count': int,
  67. },
  68. },
  69. ]
  70. def _real_extract(self, url):
  71. scheme = 'https' if url.startswith('https') else 'http'
  72. audio_id = self._match_id(url)
  73. audio_info_file = f'{scheme}://m.ximalaya.com/tracks/{audio_id}.json'
  74. audio_info = self._download_json(
  75. audio_info_file, audio_id,
  76. f'Downloading info json {audio_info_file}', 'Unable to download info file')
  77. formats = [{
  78. 'format_id': f'{bps}k',
  79. 'url': audio_info[k],
  80. 'abr': bps,
  81. 'vcodec': 'none',
  82. } for bps, k in ((24, 'play_path_32'), (64, 'play_path_64')) if audio_info.get(k)]
  83. thumbnails = []
  84. for k in audio_info:
  85. # cover pics kyes like: cover_url', 'cover_url_142'
  86. if k.startswith('cover_url'):
  87. thumbnail = {'name': k, 'url': audio_info[k]}
  88. if k == 'cover_url_142':
  89. thumbnail['width'] = 180
  90. thumbnail['height'] = 180
  91. thumbnails.append(thumbnail)
  92. audio_uploader_id = audio_info.get('uid')
  93. audio_description = try_call(
  94. lambda: audio_info['intro'].replace('\r\n\r\n\r\n ', '\n').replace('\r\n', '\n'))
  95. return {
  96. 'id': audio_id,
  97. 'uploader': audio_info.get('nickname'),
  98. 'uploader_id': str_or_none(audio_uploader_id),
  99. 'uploader_url': f'{scheme}://www.ximalaya.com/zhubo/{audio_uploader_id}/' if audio_uploader_id else None,
  100. 'title': audio_info['title'],
  101. 'thumbnails': thumbnails,
  102. 'description': audio_description,
  103. 'categories': list(filter(None, [audio_info.get('category_name')])),
  104. 'duration': audio_info.get('duration'),
  105. 'view_count': audio_info.get('play_count'),
  106. 'like_count': audio_info.get('favorites_count'),
  107. 'formats': formats,
  108. }
  109. class XimalayaAlbumIE(XimalayaBaseIE):
  110. IE_NAME = 'ximalaya:album'
  111. IE_DESC = '喜马拉雅FM 专辑'
  112. _VALID_URL = r'https?://(?:www\.|m\.)?ximalaya\.com/(?:\d+/)?album/(?P<id>[0-9]+)'
  113. _TESTS = [{
  114. 'url': 'http://www.ximalaya.com/61425525/album/5534601/',
  115. 'info_dict': {
  116. 'title': '唐诗三百首(含赏析)',
  117. 'id': '5534601',
  118. },
  119. 'playlist_mincount': 323,
  120. }, {
  121. 'url': 'https://www.ximalaya.com/album/6912905',
  122. 'info_dict': {
  123. 'title': '埃克哈特《修炼当下的力量》',
  124. 'id': '6912905',
  125. },
  126. 'playlist_mincount': 41,
  127. }]
  128. def _real_extract(self, url):
  129. playlist_id = self._match_id(url)
  130. first_page = self._fetch_page(playlist_id, 1)
  131. page_count = math.ceil(first_page['trackTotalCount'] / first_page['pageSize'])
  132. entries = InAdvancePagedList(
  133. lambda idx: self._get_entries(self._fetch_page(playlist_id, idx + 1) if idx else first_page),
  134. page_count, first_page['pageSize'])
  135. title = traverse_obj(first_page, ('tracks', 0, 'albumTitle'), expected_type=str)
  136. return self.playlist_result(entries, playlist_id, title)
  137. def _fetch_page(self, playlist_id, page_idx):
  138. return self._download_json(
  139. 'https://www.ximalaya.com/revision/album/v1/getTracksList',
  140. playlist_id, note=f'Downloading tracks list page {page_idx}',
  141. query={'albumId': playlist_id, 'pageNum': page_idx})['data']
  142. def _get_entries(self, page_data):
  143. for e in page_data['tracks']:
  144. yield self.url_result(
  145. self._proto_relative_url(f'//www.ximalaya.com{e["url"]}'),
  146. XimalayaIE, e.get('trackId'), e.get('title'))