logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

xiami.py (6816B)


  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 = 'https://emumo.xiami.com/song/playlist/cat/json/id'
  8. def _download_webpage_handle(self, *args, **kwargs):
  9. webpage = super(XiamiBaseIE, self)._download_webpage_handle(*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_length = len(origin) // n
  49. long_num = len(origin) - short_length * n
  50. l = tuple()
  51. for i in range(0, n):
  52. length = short_length
  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_length + 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. }