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.

169 lines
5.4 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. title = track['title']
  15. track_url = self._decrypt(track['location'])
  16. subtitles = {}
  17. lyrics_url = track.get('lyric_url') or track.get('lyric')
  18. if lyrics_url and lyrics_url.startswith('http'):
  19. subtitles['origin'] = [{'url': lyrics_url}]
  20. return {
  21. 'id': track.get('song_id') or track_id,
  22. 'url': track_url,
  23. 'title': title,
  24. 'thumbnail': track.get('pic') or track.get('album_pic'),
  25. 'duration': int_or_none(track.get('length')),
  26. 'creator': track.get('artist', '').split(';')[0],
  27. 'track': title,
  28. 'album': track.get('album_name'),
  29. 'artist': track.get('artist'),
  30. 'subtitles': subtitles,
  31. }
  32. def _extract_tracks(self, item_id, typ=None):
  33. playlist = self._download_json(
  34. '%s/%s%s' % (self._API_BASE_URL, item_id, '/type/%s' % typ if typ else ''), item_id)
  35. return [
  36. self._extract_track(track, item_id)
  37. for track in playlist['data']['trackList']]
  38. @staticmethod
  39. def _decrypt(origin):
  40. n = int(origin[0])
  41. origin = origin[1:]
  42. short_lenth = len(origin) // n
  43. long_num = len(origin) - short_lenth * n
  44. l = tuple()
  45. for i in range(0, n):
  46. length = short_lenth
  47. if i < long_num:
  48. length += 1
  49. l += (origin[0:length], )
  50. origin = origin[length:]
  51. ans = ''
  52. for i in range(0, short_lenth + 1):
  53. for j in range(0, n):
  54. if len(l[j]) > i:
  55. ans += l[j][i]
  56. return compat_urllib_parse_unquote(ans).replace('^', '0')
  57. class XiamiSongIE(XiamiBaseIE):
  58. IE_NAME = 'xiami:song'
  59. IE_DESC = '虾米音乐'
  60. _VALID_URL = r'https?://(?:www\.)?xiami\.com/song/(?P<id>[0-9]+)'
  61. _TESTS = [{
  62. 'url': 'http://www.xiami.com/song/1775610518',
  63. 'md5': '521dd6bea40fd5c9c69f913c232cb57e',
  64. 'info_dict': {
  65. 'id': '1775610518',
  66. 'ext': 'mp3',
  67. 'title': 'Woman',
  68. 'thumbnail': r're:http://img\.xiami\.net/images/album/.*\.jpg',
  69. 'duration': 265,
  70. 'creator': 'HONNE',
  71. 'track': 'Woman',
  72. 'album': 'Woman',
  73. 'artist': 'HONNE',
  74. 'subtitles': {
  75. 'origin': [{
  76. 'ext': 'lrc',
  77. }],
  78. },
  79. },
  80. 'skip': 'Georestricted',
  81. }, {
  82. 'url': 'http://www.xiami.com/song/1775256504',
  83. 'md5': '932a3abd45c6aa2b1fdbe028fcb4c4fc',
  84. 'info_dict': {
  85. 'id': '1775256504',
  86. 'ext': 'mp3',
  87. 'title': '悟空',
  88. 'thumbnail': r're:http://img\.xiami\.net/images/album/.*\.jpg',
  89. 'duration': 200,
  90. 'creator': '戴荃',
  91. 'track': '悟空',
  92. 'album': '悟空',
  93. 'artist': '戴荃',
  94. 'subtitles': {
  95. 'origin': [{
  96. 'ext': 'lrc',
  97. }],
  98. },
  99. },
  100. 'skip': 'Georestricted',
  101. }]
  102. def _real_extract(self, url):
  103. return self._extract_tracks(self._match_id(url))[0]
  104. class XiamiPlaylistBaseIE(XiamiBaseIE):
  105. def _real_extract(self, url):
  106. item_id = self._match_id(url)
  107. return self.playlist_result(self._extract_tracks(item_id, self._TYPE), item_id)
  108. class XiamiAlbumIE(XiamiPlaylistBaseIE):
  109. IE_NAME = 'xiami:album'
  110. IE_DESC = '虾米音乐 - 专辑'
  111. _VALID_URL = r'https?://(?:www\.)?xiami\.com/album/(?P<id>[0-9]+)'
  112. _TYPE = '1'
  113. _TESTS = [{
  114. 'url': 'http://www.xiami.com/album/2100300444',
  115. 'info_dict': {
  116. 'id': '2100300444',
  117. },
  118. 'playlist_count': 10,
  119. 'skip': 'Georestricted',
  120. }, {
  121. 'url': 'http://www.xiami.com/album/512288?spm=a1z1s.6843761.1110925389.6.hhE9p9',
  122. 'only_matching': True,
  123. }]
  124. class XiamiArtistIE(XiamiPlaylistBaseIE):
  125. IE_NAME = 'xiami:artist'
  126. IE_DESC = '虾米音乐 - 歌手'
  127. _VALID_URL = r'https?://(?:www\.)?xiami\.com/artist/(?P<id>[0-9]+)'
  128. _TYPE = '2'
  129. _TEST = {
  130. 'url': 'http://www.xiami.com/artist/2132?spm=0.0.0.0.dKaScp',
  131. 'info_dict': {
  132. 'id': '2132',
  133. },
  134. 'playlist_count': 20,
  135. 'skip': 'Georestricted',
  136. }
  137. class XiamiCollectionIE(XiamiPlaylistBaseIE):
  138. IE_NAME = 'xiami:collection'
  139. IE_DESC = '虾米音乐 - 精选集'
  140. _VALID_URL = r'https?://(?:www\.)?xiami\.com/collect/(?P<id>[0-9]+)'
  141. _TYPE = '3'
  142. _TEST = {
  143. 'url': 'http://www.xiami.com/collect/156527391?spm=a1z1s.2943601.6856193.12.4jpBnr',
  144. 'info_dict': {
  145. 'id': '156527391',
  146. },
  147. 'playlist_mincount': 29,
  148. 'skip': 'Georestricted',
  149. }