logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

ximalaya.py (9912B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import itertools
  4. import re
  5. from .common import InfoExtractor
  6. class XimalayaBaseIE(InfoExtractor):
  7. _GEO_COUNTRIES = ['CN']
  8. class XimalayaIE(XimalayaBaseIE):
  9. IE_NAME = 'ximalaya'
  10. IE_DESC = '喜马拉雅FM'
  11. _VALID_URL = r'https?://(?:www\.|m\.)?ximalaya\.com/(?P<uid>[0-9]+)/sound/(?P<id>[0-9]+)'
  12. _USER_URL_FORMAT = '%s://www.ximalaya.com/zhubo/%i/'
  13. _TESTS = [
  14. {
  15. 'url': 'http://www.ximalaya.com/61425525/sound/47740352/',
  16. 'info_dict': {
  17. 'id': '47740352',
  18. 'ext': 'm4a',
  19. 'uploader': '小彬彬爱听书',
  20. 'uploader_id': 61425525,
  21. 'uploader_url': 'http://www.ximalaya.com/zhubo/61425525/',
  22. 'title': '261.唐诗三百首.卷八.送孟浩然之广陵.李白',
  23. 'description': "contains:《送孟浩然之广陵》\n作者:李白\n故人西辞黄鹤楼,烟花三月下扬州。\n孤帆远影碧空尽,惟见长江天际流。",
  24. 'thumbnails': [
  25. {
  26. 'name': 'cover_url',
  27. 'url': r're:^https?://.*\.jpg$',
  28. },
  29. {
  30. 'name': 'cover_url_142',
  31. 'url': r're:^https?://.*\.jpg$',
  32. 'width': 180,
  33. 'height': 180
  34. }
  35. ],
  36. 'categories': ['renwen', '人文'],
  37. 'duration': 93,
  38. 'view_count': int,
  39. 'like_count': int,
  40. }
  41. },
  42. {
  43. 'url': 'http://m.ximalaya.com/61425525/sound/47740352/',
  44. 'info_dict': {
  45. 'id': '47740352',
  46. 'ext': 'm4a',
  47. 'uploader': '小彬彬爱听书',
  48. 'uploader_id': 61425525,
  49. 'uploader_url': 'http://www.ximalaya.com/zhubo/61425525/',
  50. 'title': '261.唐诗三百首.卷八.送孟浩然之广陵.李白',
  51. 'description': "contains:《送孟浩然之广陵》\n作者:李白\n故人西辞黄鹤楼,烟花三月下扬州。\n孤帆远影碧空尽,惟见长江天际流。",
  52. 'thumbnails': [
  53. {
  54. 'name': 'cover_url',
  55. 'url': r're:^https?://.*\.jpg$',
  56. },
  57. {
  58. 'name': 'cover_url_142',
  59. 'url': r're:^https?://.*\.jpg$',
  60. 'width': 180,
  61. 'height': 180
  62. }
  63. ],
  64. 'categories': ['renwen', '人文'],
  65. 'duration': 93,
  66. 'view_count': int,
  67. 'like_count': int,
  68. }
  69. },
  70. {
  71. 'url': 'https://www.ximalaya.com/11045267/sound/15705996/',
  72. 'info_dict': {
  73. 'id': '15705996',
  74. 'ext': 'm4a',
  75. 'uploader': '李延隆老师',
  76. 'uploader_id': 11045267,
  77. 'uploader_url': 'https://www.ximalaya.com/zhubo/11045267/',
  78. 'title': 'Lesson 1 Excuse me!',
  79. 'description': "contains:Listen to the tape then answer\xa0this question. Whose handbag is it?\n"
  80. "听录音,然后回答问题,这是谁的手袋?",
  81. 'thumbnails': [
  82. {
  83. 'name': 'cover_url',
  84. 'url': r're:^https?://.*\.jpg$',
  85. },
  86. {
  87. 'name': 'cover_url_142',
  88. 'url': r're:^https?://.*\.jpg$',
  89. 'width': 180,
  90. 'height': 180
  91. }
  92. ],
  93. 'categories': ['train', '外语'],
  94. 'duration': 40,
  95. 'view_count': int,
  96. 'like_count': int,
  97. }
  98. },
  99. ]
  100. def _real_extract(self, url):
  101. is_m = 'm.ximalaya' in url
  102. scheme = 'https' if url.startswith('https') else 'http'
  103. audio_id = self._match_id(url)
  104. webpage = self._download_webpage(url, audio_id,
  105. note='Download sound page for %s' % audio_id,
  106. errnote='Unable to get sound page')
  107. audio_info_file = '%s://m.ximalaya.com/tracks/%s.json' % (scheme, audio_id)
  108. audio_info = self._download_json(audio_info_file, audio_id,
  109. 'Downloading info json %s' % audio_info_file,
  110. 'Unable to download info file')
  111. formats = []
  112. for bps, k in (('24k', 'play_path_32'), ('64k', 'play_path_64')):
  113. if audio_info.get(k):
  114. formats.append({
  115. 'format_id': bps,
  116. 'url': audio_info[k],
  117. })
  118. thumbnails = []
  119. for k in audio_info.keys():
  120. # cover pics kyes like: cover_url', 'cover_url_142'
  121. if k.startswith('cover_url'):
  122. thumbnail = {'name': k, 'url': audio_info[k]}
  123. if k == 'cover_url_142':
  124. thumbnail['width'] = 180
  125. thumbnail['height'] = 180
  126. thumbnails.append(thumbnail)
  127. audio_uploader_id = audio_info.get('uid')
  128. if is_m:
  129. audio_description = self._html_search_regex(r'(?s)<section\s+class=["\']content[^>]+>(.+?)</section>',
  130. webpage, 'audio_description', fatal=False)
  131. else:
  132. audio_description = self._html_search_regex(r'(?s)<div\s+class=["\']rich_intro[^>]*>(.+?</article>)',
  133. webpage, 'audio_description', fatal=False)
  134. if not audio_description:
  135. audio_description_file = '%s://www.ximalaya.com/sounds/%s/rich_intro' % (scheme, audio_id)
  136. audio_description = self._download_webpage(audio_description_file, audio_id,
  137. note='Downloading description file %s' % audio_description_file,
  138. errnote='Unable to download descrip file',
  139. fatal=False)
  140. audio_description = audio_description.strip() if audio_description else None
  141. return {
  142. 'id': audio_id,
  143. 'uploader': audio_info.get('nickname'),
  144. 'uploader_id': audio_uploader_id,
  145. 'uploader_url': self._USER_URL_FORMAT % (scheme, audio_uploader_id) if audio_uploader_id else None,
  146. 'title': audio_info['title'],
  147. 'thumbnails': thumbnails,
  148. 'description': audio_description,
  149. 'categories': list(filter(None, (audio_info.get('category_name'), audio_info.get('category_title')))),
  150. 'duration': audio_info.get('duration'),
  151. 'view_count': audio_info.get('play_count'),
  152. 'like_count': audio_info.get('favorites_count'),
  153. 'formats': formats,
  154. }
  155. class XimalayaAlbumIE(XimalayaBaseIE):
  156. IE_NAME = 'ximalaya:album'
  157. IE_DESC = '喜马拉雅FM 专辑'
  158. _VALID_URL = r'https?://(?:www\.|m\.)?ximalaya\.com/(?P<uid>[0-9]+)/album/(?P<id>[0-9]+)'
  159. _TEMPLATE_URL = '%s://www.ximalaya.com/%s/album/%s/'
  160. _BASE_URL_TEMPL = '%s://www.ximalaya.com%s'
  161. _LIST_VIDEO_RE = r'<a[^>]+?href="(?P<url>/%s/sound/(?P<id>\d+)/?)"[^>]+?title="(?P<title>[^>]+)">'
  162. _TESTS = [{
  163. 'url': 'http://www.ximalaya.com/61425525/album/5534601/',
  164. 'info_dict': {
  165. 'title': '唐诗三百首(含赏析)',
  166. 'id': '5534601',
  167. },
  168. 'playlist_count': 312,
  169. }, {
  170. 'url': 'http://m.ximalaya.com/61425525/album/5534601',
  171. 'info_dict': {
  172. 'title': '唐诗三百首(含赏析)',
  173. 'id': '5534601',
  174. },
  175. 'playlist_count': 312,
  176. },
  177. ]
  178. def _real_extract(self, url):
  179. self.scheme = scheme = 'https' if url.startswith('https') else 'http'
  180. mobj = re.match(self._VALID_URL, url)
  181. uid, playlist_id = mobj.group('uid'), mobj.group('id')
  182. webpage = self._download_webpage(self._TEMPLATE_URL % (scheme, uid, playlist_id), playlist_id,
  183. note='Download album page for %s' % playlist_id,
  184. errnote='Unable to get album info')
  185. title = self._html_search_regex(r'detailContent_title[^>]*><h1(?:[^>]+)?>([^<]+)</h1>',
  186. webpage, 'title', fatal=False)
  187. return self.playlist_result(self._entries(webpage, playlist_id, uid), playlist_id, title)
  188. def _entries(self, page, playlist_id, uid):
  189. html = page
  190. for page_num in itertools.count(1):
  191. for entry in self._process_page(html, uid):
  192. yield entry
  193. next_url = self._search_regex(r'<a\s+href=(["\'])(?P<more>[\S]+)\1[^>]+rel=(["\'])next\3',
  194. html, 'list_next_url', default=None, group='more')
  195. if not next_url:
  196. break
  197. next_full_url = self._BASE_URL_TEMPL % (self.scheme, next_url)
  198. html = self._download_webpage(next_full_url, playlist_id)
  199. def _process_page(self, html, uid):
  200. find_from = html.index('album_soundlist')
  201. for mobj in re.finditer(self._LIST_VIDEO_RE % uid, html[find_from:]):
  202. yield self.url_result(self._BASE_URL_TEMPL % (self.scheme, mobj.group('url')),
  203. XimalayaIE.ie_key(),
  204. mobj.group('id'),
  205. mobj.group('title'))