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.

198 lines
6.5 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, typ=None):
  36. playlist = self._download_json(
  37. '%s/%s%s' % (self._API_BASE_URL, item_id, '/type/%s' % typ if typ else ''), item_id)
  38. return [
  39. self._extract_track(track, item_id)
  40. for track in playlist['data']['trackList']]
  41. @staticmethod
  42. def _decrypt(origin):
  43. n = int(origin[0])
  44. origin = origin[1:]
  45. short_lenth = len(origin) // n
  46. long_num = len(origin) - short_lenth * n
  47. l = tuple()
  48. for i in range(0, n):
  49. length = short_lenth
  50. if i < long_num:
  51. length += 1
  52. l += (origin[0:length], )
  53. origin = origin[length:]
  54. ans = ''
  55. for i in range(0, short_lenth + 1):
  56. for j in range(0, n):
  57. if len(l[j]) > i:
  58. ans += l[j][i]
  59. return compat_urllib_parse_unquote(ans).replace('^', '0')
  60. class XiamiSongIE(XiamiBaseIE):
  61. IE_NAME = 'xiami:song'
  62. IE_DESC = '虾米音乐'
  63. _VALID_URL = r'https?://(?:www\.)?xiami\.com/song/(?P<id>[^/?#&]+)'
  64. _TESTS = [{
  65. 'url': 'http://www.xiami.com/song/1775610518',
  66. 'md5': '521dd6bea40fd5c9c69f913c232cb57e',
  67. 'info_dict': {
  68. 'id': '1775610518',
  69. 'ext': 'mp3',
  70. 'title': 'HONNE - Woman',
  71. 'thumbnail': r're:http://img\.xiami\.net/images/album/.*\.jpg',
  72. 'duration': 265,
  73. 'creator': 'HONNE',
  74. 'track': 'Woman',
  75. 'album': 'Woman',
  76. 'artist': 'HONNE',
  77. 'subtitles': {
  78. 'origin': [{
  79. 'ext': 'lrc',
  80. }],
  81. },
  82. },
  83. 'skip': 'Georestricted',
  84. }, {
  85. 'url': 'http://www.xiami.com/song/1775256504',
  86. 'md5': '932a3abd45c6aa2b1fdbe028fcb4c4fc',
  87. 'info_dict': {
  88. 'id': '1775256504',
  89. 'ext': 'mp3',
  90. 'title': '戴荃 - 悟空',
  91. 'thumbnail': r're:http://img\.xiami\.net/images/album/.*\.jpg',
  92. 'duration': 200,
  93. 'creator': '戴荃',
  94. 'track': '悟空',
  95. 'album': '悟空',
  96. 'artist': '戴荃',
  97. 'subtitles': {
  98. 'origin': [{
  99. 'ext': 'lrc',
  100. }],
  101. },
  102. },
  103. 'skip': 'Georestricted',
  104. }, {
  105. 'url': 'http://www.xiami.com/song/1775953850',
  106. 'info_dict': {
  107. 'id': '1775953850',
  108. 'ext': 'mp3',
  109. 'title': 'До Скону - Чума Пожирает Землю',
  110. 'thumbnail': r're:http://img\.xiami\.net/images/album/.*\.jpg',
  111. 'duration': 683,
  112. 'creator': 'До Скону',
  113. 'track': 'Чума Пожирает Землю',
  114. 'track_number': 7,
  115. 'album': 'Ад',
  116. 'artist': 'До Скону',
  117. },
  118. 'params': {
  119. 'skip_download': True,
  120. },
  121. }, {
  122. 'url': 'http://www.xiami.com/song/xLHGwgd07a1',
  123. 'only_matching': True,
  124. }]
  125. def _real_extract(self, url):
  126. return self._extract_tracks(self._match_id(url))[0]
  127. class XiamiPlaylistBaseIE(XiamiBaseIE):
  128. def _real_extract(self, url):
  129. item_id = self._match_id(url)
  130. return self.playlist_result(self._extract_tracks(item_id, self._TYPE), item_id)
  131. class XiamiAlbumIE(XiamiPlaylistBaseIE):
  132. IE_NAME = 'xiami:album'
  133. IE_DESC = '虾米音乐 - 专辑'
  134. _VALID_URL = r'https?://(?:www\.)?xiami\.com/album/(?P<id>[^/?#&]+)'
  135. _TYPE = '1'
  136. _TESTS = [{
  137. 'url': 'http://www.xiami.com/album/2100300444',
  138. 'info_dict': {
  139. 'id': '2100300444',
  140. },
  141. 'playlist_count': 10,
  142. 'skip': 'Georestricted',
  143. }, {
  144. 'url': 'http://www.xiami.com/album/512288?spm=a1z1s.6843761.1110925389.6.hhE9p9',
  145. 'only_matching': True,
  146. }, {
  147. 'url': 'http://www.xiami.com/album/URVDji2a506',
  148. 'only_matching': True,
  149. }]
  150. class XiamiArtistIE(XiamiPlaylistBaseIE):
  151. IE_NAME = 'xiami:artist'
  152. IE_DESC = '虾米音乐 - 歌手'
  153. _VALID_URL = r'https?://(?:www\.)?xiami\.com/artist/(?P<id>[^/?#&]+)'
  154. _TYPE = '2'
  155. _TESTS = [{
  156. 'url': 'http://www.xiami.com/artist/2132?spm=0.0.0.0.dKaScp',
  157. 'info_dict': {
  158. 'id': '2132',
  159. },
  160. 'playlist_count': 20,
  161. 'skip': 'Georestricted',
  162. }, {
  163. 'url': 'http://www.xiami.com/artist/bC5Tk2K6eb99',
  164. 'only_matching': True,
  165. }]
  166. class XiamiCollectionIE(XiamiPlaylistBaseIE):
  167. IE_NAME = 'xiami:collection'
  168. IE_DESC = '虾米音乐 - 精选集'
  169. _VALID_URL = r'https?://(?:www\.)?xiami\.com/collect/(?P<id>[^/?#&]+)'
  170. _TYPE = '3'
  171. _TEST = {
  172. 'url': 'http://www.xiami.com/collect/156527391?spm=a1z1s.2943601.6856193.12.4jpBnr',
  173. 'info_dict': {
  174. 'id': '156527391',
  175. },
  176. 'playlist_mincount': 29,
  177. 'skip': 'Georestricted',
  178. }