Skip to content

Commit

Permalink
重新增加虾米音乐支持
Browse files Browse the repository at this point in the history
  • Loading branch information
0xHJK committed Jun 13, 2019
1 parent 43d73e0 commit 02de87d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
84 changes: 84 additions & 0 deletions music_dl/addons/xiami.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author: HJK
@file: xiami.py
@time: 2019-06-13
"""

import re
import copy
import json
import hashlib
from .. import config
from ..api import MusicApi
from ..song import BasicSong
from ..exceptions import DataError

__all__ = ["search"]


class XiamiApi(MusicApi):
session = copy.deepcopy(MusicApi.session)
session.headers.update({"referer": "http://www.xiami.com/song/play"})

@classmethod
def encrypted_params(cls, keyword):
number = config.get("number") or 5
_q = dict(key=keyword, pagingVO=dict(page=1, pageSize=number))
_q = json.dumps(_q)
url = "https://www.xiami.com/search?key={}".format(keyword)
res = cls.session.get(url)
cookie = res.cookies.get("xm_sg_tk", "").split("_")[0]
origin_str = "%s_xmMain_/api/search/searchSongs_%s" % (cookie, _q)
_s = hashlib.md5(origin_str.encode()).hexdigest()
return dict(_q=_q, _s=_s)


class XiamiSong(BasicSong):
def __init__(self):
super(XiamiSong, self).__init__()


def xiami_search(keyword) -> list:
""" search music from xiami """
params = XiamiApi.encrypted_params(keyword=keyword)
print(params)
res_data = (
XiamiApi.request(
"https://www.xiami.com/api/search/searchSongs", method="GET", data=params
)
.get("result", {})
.get("data", {})
.get("songs", [])
)
if not res_data:
raise DataError("Get xiami data failed.")

songs_list = []
for item in res_data:
song = XiamiSong()
song.source = "xiami"
song.id = item.get("songId", "")
song.title = item.get("songName", "")
song.singer = item.get("singers", "")
song.album = item.get("albumName", "")
song.cover_url = item.get("albumLogo", "")
song.lyrics_url = item.get("lyricInfo", {}).get("lyricFile", "")

listen_files = sorted(
item.get("listenFiles", []),
key=lambda x: x.get("downloadFileSize", 0),
reverse=True,
)
song.song_url = listen_files[0].get("listenFile", "")
song.duration = int(listen_files[0].get("length", 0) / 1000)
song.ext = listen_files[0].get("format", "mp3")
song.rate = re.findall("https?://s(\d+)", song.song_url)[0]

songs_list.append(song)

return songs_list


search = xiami_search
2 changes: 1 addition & 1 deletion music_dl/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def search(self, keyword, sources_list) -> list:
"netease": "netease",
"163": "netease",
"qq": "qq",
# "xiami": "xiami",
"xiami": "xiami",
}
thread_pool = []
ret_songs_list = []
Expand Down
15 changes: 15 additions & 0 deletions tests/test_addon_xiami.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author: HJK
@file: test_addon_xiami.py
@time: 2019-06-13
"""


from music_dl.addons import xiami


def test_xiami():
songs_list = xiami.search("好妹妹乐队")
assert songs_list is not None

0 comments on commit 02de87d

Please sign in to comment.