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.

201 lines
6.6 KiB

bug fix for extractor xiami.py Before applying this patch, when downloading resources from xiami.com, it crashes with these: Traceback (most recent call last): File "/home/phi/.local/bin/youtube-dl", line 11, in <module> sys.exit(main()) File "/home/phi/.local/lib/python3.5/site-packages/youtube_dl/__init__.py", line 433, in main _real_main(argv) File "/home/phi/.local/lib/python3.5/site-packages/youtube_dl/__init__.py", line 423, in _real_main retcode = ydl.download(all_urls) File "/home/phi/.local/lib/python3.5/site-packages/youtube_dl/YoutubeDL.py", line 1786, in download url, force_generic_extractor=self.params.get('force_generic_extractor', False)) File "/home/phi/.local/lib/python3.5/site-packages/youtube_dl/YoutubeDL.py", line 691, in extract_info ie_result = ie.extract(url) File "/home/phi/.local/lib/python3.5/site-packages/youtube_dl/extractor/common.py", line 347, in extract return self._real_extract(url) File "/home/phi/.local/lib/python3.5/site-packages/youtube_dl/extractor/xiami.py", line 116, in _real_extract return self._extract_tracks(self._match_id(url))[0] File "/home/phi/.local/lib/python3.5/site-packages/youtube_dl/extractor/xiami.py", line 43, in _extract_tracks '%s/%s%s' % (self._API_BASE_URL, item_id, '/type/%s' % typ if typ else ''), item_id) File "/home/phi/.local/lib/python3.5/site-packages/youtube_dl/extractor/common.py", line 562, in _download_json json_string, video_id, transform_source=transform_source, fatal=fatal) File "/home/phi/.local/lib/python3.5/site-packages/youtube_dl/extractor/common.py", line 568, in _parse_json return json.loads(json_string) File "/usr/lib/python3.5/json/__init__.py", line 312, in loads s.__class__.__name__)) TypeError: the JSON object must be str, not 'NoneType' This patch solves exactly this problem.
8 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_urllib_parse_unquote
  5. from ..utils import int_or_none
  6. class XiamiBaseIE(InfoExtractor):
  7. _API_BASE_URL = 'http://www.xiami.com/song/playlist/cat/json/id'
  8. def _download_webpage(self, *args, **kwargs):
  9. webpage = super(XiamiBaseIE, self)._download_webpage(*args, **kwargs)
  10. if '>Xiami is currently not available in your country.<' in webpage:
  11. self.raise_geo_restricted('Xiami is currently not available in your country')
  12. return webpage
  13. def _extract_track(self, track, track_id=None):
  14. track_name = track.get('songName') or track.get('name') or track['subName']
  15. artist = track.get('artist') or track.get('artist_name') or track.get('singers')
  16. title = '%s - %s' % (artist, track_name) if artist else track_name
  17. track_url = self._decrypt(track['location'])
  18. subtitles = {}
  19. lyrics_url = track.get('lyric_url') or track.get('lyric')
  20. if lyrics_url and lyrics_url.startswith('http'):
  21. subtitles['origin'] = [{'url': lyrics_url}]
  22. return {
  23. 'id': track.get('song_id') or track_id,
  24. 'url': track_url,
  25. 'title': title,
  26. 'thumbnail': track.get('pic') or track.get('album_pic'),
  27. 'duration': int_or_none(track.get('length')),
  28. 'creator': track.get('artist', '').split(';')[0],
  29. 'track': track_name,
  30. 'track_number': int_or_none(track.get('track')),
  31. 'album': track.get('album_name') or track.get('title'),
  32. 'artist': artist,
  33. 'subtitles': subtitles,
  34. }
  35. def _extract_tracks(self, item_id, referer, typ=None):
  36. playlist = self._download_json(
  37. '%s/%s%s' % (self._API_BASE_URL, item_id, '/type/%s' % typ if typ else ''),
  38. item_id, headers={
  39. 'Referer': referer,
  40. })
  41. return [
  42. self._extract_track(track, item_id)
  43. for track in playlist['data']['trackList']]
  44. @staticmethod
  45. def _decrypt(origin):
  46. n = int(origin[0])
  47. origin = origin[1:]
  48. short_lenth = len(origin) // n
  49. long_num = len(origin) - short_lenth * n
  50. l = tuple()
  51. for i in range(0, n):
  52. length = short_lenth
  53. if i < long_num:
  54. length += 1
  55. l += (origin[0:length], )
  56. origin = origin[length:]
  57. ans = ''
  58. for i in range(0, short_lenth + 1):
  59. for j in range(0, n):
  60. if len(l[j]) > i:
  61. ans += l[j][i]
  62. return compat_urllib_parse_unquote(ans).replace('^', '0')
  63. class XiamiSongIE(XiamiBaseIE):
  64. IE_NAME = 'xiami:song'
  65. IE_DESC = '虾米音乐'
  66. _VALID_URL = r'https?://(?:www\.)?xiami\.com/song/(?P<id>[^/?#&]+)'
  67. _TESTS = [{
  68. 'url': 'http://www.xiami.com/song/1775610518',
  69. 'md5': '521dd6bea40fd5c9c69f913c232cb57e',
  70. 'info_dict': {
  71. 'id': '1775610518',
  72. 'ext': 'mp3',
  73. 'title': 'HONNE - Woman',
  74. 'thumbnail': r're:http://img\.xiami\.net/images/album/.*\.jpg',
  75. 'duration': 265,
  76. 'creator': 'HONNE',
  77. 'track': 'Woman',
  78. 'album': 'Woman',
  79. 'artist': 'HONNE',
  80. 'subtitles': {
  81. 'origin': [{
  82. 'ext': 'lrc',
  83. }],
  84. },
  85. },
  86. 'skip': 'Georestricted',
  87. }, {
  88. 'url': 'http://www.xiami.com/song/1775256504',
  89. 'md5': '932a3abd45c6aa2b1fdbe028fcb4c4fc',
  90. 'info_dict': {
  91. 'id': '1775256504',
  92. 'ext': 'mp3',
  93. 'title': '戴荃 - 悟空',
  94. 'thumbnail': r're:http://img\.xiami\.net/images/album/.*\.jpg',
  95. 'duration': 200,
  96. 'creator': '戴荃',
  97. 'track': '悟空',
  98. 'album': '悟空',
  99. 'artist': '戴荃',
  100. 'subtitles': {
  101. 'origin': [{
  102. 'ext': 'lrc',
  103. }],
  104. },
  105. },
  106. 'skip': 'Georestricted',
  107. }, {
  108. 'url': 'http://www.xiami.com/song/1775953850',
  109. 'info_dict': {
  110. 'id': '1775953850',
  111. 'ext': 'mp3',
  112. 'title': 'До Скону - Чума Пожирает Землю',
  113. 'thumbnail': r're:http://img\.xiami\.net/images/album/.*\.jpg',
  114. 'duration': 683,
  115. 'creator': 'До Скону',
  116. 'track': 'Чума Пожирает Землю',
  117. 'track_number': 7,
  118. 'album': 'Ад',
  119. 'artist': 'До Скону',
  120. },
  121. 'params': {
  122. 'skip_download': True,
  123. },
  124. }, {
  125. 'url': 'http://www.xiami.com/song/xLHGwgd07a1',
  126. 'only_matching': True,
  127. }]
  128. def _real_extract(self, url):
  129. return self._extract_tracks(self._match_id(url), url)[0]
  130. class XiamiPlaylistBaseIE(XiamiBaseIE):
  131. def _real_extract(self, url):
  132. item_id = self._match_id(url)
  133. return self.playlist_result(self._extract_tracks(item_id, url, self._TYPE), item_id)
  134. class XiamiAlbumIE(XiamiPlaylistBaseIE):
  135. IE_NAME = 'xiami:album'
  136. IE_DESC = '虾米音乐 - 专辑'
  137. _VALID_URL = r'https?://(?:www\.)?xiami\.com/album/(?P<id>[^/?#&]+)'
  138. _TYPE = '1'
  139. _TESTS = [{
  140. 'url': 'http://www.xiami.com/album/2100300444',
  141. 'info_dict': {
  142. 'id': '2100300444',
  143. },
  144. 'playlist_count': 10,
  145. 'skip': 'Georestricted',
  146. }, {
  147. 'url': 'http://www.xiami.com/album/512288?spm=a1z1s.6843761.1110925389.6.hhE9p9',
  148. 'only_matching': True,
  149. }, {
  150. 'url': 'http://www.xiami.com/album/URVDji2a506',
  151. 'only_matching': True,
  152. }]
  153. class XiamiArtistIE(XiamiPlaylistBaseIE):
  154. IE_NAME = 'xiami:artist'
  155. IE_DESC = '虾米音乐 - 歌手'
  156. _VALID_URL = r'https?://(?:www\.)?xiami\.com/artist/(?P<id>[^/?#&]+)'
  157. _TYPE = '2'
  158. _TESTS = [{
  159. 'url': 'http://www.xiami.com/artist/2132?spm=0.0.0.0.dKaScp',
  160. 'info_dict': {
  161. 'id': '2132',
  162. },
  163. 'playlist_count': 20,
  164. 'skip': 'Georestricted',
  165. }, {
  166. 'url': 'http://www.xiami.com/artist/bC5Tk2K6eb99',
  167. 'only_matching': True,
  168. }]
  169. class XiamiCollectionIE(XiamiPlaylistBaseIE):
  170. IE_NAME = 'xiami:collection'
  171. IE_DESC = '虾米音乐 - 精选集'
  172. _VALID_URL = r'https?://(?:www\.)?xiami\.com/collect/(?P<id>[^/?#&]+)'
  173. _TYPE = '3'
  174. _TEST = {
  175. 'url': 'http://www.xiami.com/collect/156527391?spm=a1z1s.2943601.6856193.12.4jpBnr',
  176. 'info_dict': {
  177. 'id': '156527391',
  178. },
  179. 'playlist_mincount': 29,
  180. 'skip': 'Georestricted',
  181. }