-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathneteasecloud_music_downloader.py
More file actions
103 lines (91 loc) · 3.11 KB
/
Copy pathneteasecloud_music_downloader.py
File metadata and controls
103 lines (91 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- encoding: utf-8 -*-
"""
@File : neteasecloud_music_downloader.py
@Time : 2024/3/12 13:15:33
@Author : lh9171338
@Version : 1.0
@Contact : 2909171338@qq.com
"""
import os
import sys
import urllib.parse
import requests
from yacs.config import CfgNode
import logging
from typing import Type
from PyQt5.QtWidgets import *
from util import printTime
from item import Item
from search import SearchWorker
from music_downloader import MusicDownloader
class NeteaseCloudSearchWorker(SearchWorker):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Variables
self.search_url = "https://music.163.com/api/search/get"
self.download_url = "https://music.163.com/api/song/enhance/player/url?ids=%s&csrf_token=&br=999000"
self.search_data = {
"s": "",
"type": 1,
"offset": 0,
"sub": "false",
"limit": self.cfg.search.pagesize,
}
@printTime
def run(self) -> None:
self.items = []
# Search
data = self.search_data
data["s"] = self.keyword
data = urllib.parse.urlencode(data).encode("utf-8")
try:
res = requests.get(self.search_url, data)
js = res.json()
results1 = js["result"]["songs"]
results1 = sorted(results1, key=lambda x: x["id"])
song_ids = [result["id"] for result in results1]
download_url = self.download_url % str(song_ids)
res = requests.get(download_url)
js = res.json()
results2 = js["data"]
results2 = sorted(results2, key=lambda x: x["id"])
for result1, result2 in zip(results1, results2):
singer = result1["artists"][0]["name"]
song = result1["name"]
album = result1["album"]["name"]
duration = int(round(result2["time"] / 1000))
size = result2["size"]
url = result2["url"]
if url is None or url == "":
continue
item = Item(
song=song,
singer=singer,
album=album,
ext=".mp3",
duration=duration,
size=size,
url=url,
)
self.items.append(item)
except Exception as e:
logging.error(str(e))
logging.info(f"Found {len(self.items)} songs")
self.finished.emit()
class NeteaseCloudMusicDownloader(MusicDownloader):
def __init__(
self, searcher: Type[SearchWorker] = NeteaseCloudSearchWorker, **kwargs
):
super().__init__(searcher=searcher, **kwargs)
if __name__ == "__main__":
logging.basicConfig(
level=logging.INFO,
format="%(levelname)s:%(name)s:%(threadName)s:%(message)s",
)
root_path = sys.path[0]
cfg_file = os.path.join(root_path, "..", "config", "default.yaml")
cfg = CfgNode.load_cfg(open(cfg_file))
cfg.freeze()
app = QApplication(sys.argv)
window = NeteaseCloudMusicDownloader(cfg=cfg)
sys.exit(app.exec_())