xiami.py 4.9 KB

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