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.

168 lines
5.3 KiB

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