feat: 增加了根据歌单下载的功能,修复了多个音乐一块下载时文件名格式无效的问题,优化代码
增加了根据歌单下载的功能,修复了多个音乐一块下载时文件名格式无效的问题,优化代码
This commit is contained in:
15
modules/functions/mainly/download_multiple_songs.py
Normal file
15
modules/functions/mainly/download_multiple_songs.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from colorama import Fore
|
||||
|
||||
from modules.utils.bar import CompactArrowBar
|
||||
from modules.functions.mainly.get_song import get_song_lyric
|
||||
|
||||
def donload_multiple_songs(self, lyric_path: str, ids):
|
||||
with CompactArrowBar(f"进度: %(index){len(str(len(ids)))}d/%(max)d",
|
||||
suffix="", max=len(ids), color="yellow", width=9999) as bar:
|
||||
for i in range(0, len(ids)):
|
||||
print(ids[i])
|
||||
r = get_song_lyric(ids[i], lyric_path, self.settings.lyric_format, bar=bar, save_lyrics_time = self.settings.save_lyrics_time)
|
||||
if r == "dl_err_connection":
|
||||
bar.print_onto_bar(Fore.RED + "下载发生错误!可能是连接被拒绝!请检查网络后再试\n按回车键继续任务(该任务会被跳过)...")
|
||||
input()
|
||||
bar.next()
|
28
modules/functions/mainly/get_playlist.py
Normal file
28
modules/functions/mainly/get_playlist.py
Normal file
@@ -0,0 +1,28 @@
|
||||
"""获取歌单中歌曲id"""
|
||||
from typing import Dict, List
|
||||
from requests import get
|
||||
from json import loads
|
||||
|
||||
headers = {
|
||||
'Referer':'http://music.163.com/',
|
||||
'Host':'music.163.com',
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36',
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
|
||||
}
|
||||
|
||||
def getplaylist(playlist_id: int | str, get_count: int | str = "all") -> Dict[str, str | List[str]]:
|
||||
url1 = f"http://play.onlyacat233.top:10002/playlist/track/all?id={playlist_id}"
|
||||
url2 = f"http://play.onlyacat233.top:10002/playlist/detail?id={playlist_id}"
|
||||
context2 = loads(get(url2).text)
|
||||
|
||||
context1 = loads(get(url1).text)
|
||||
|
||||
playlist = []
|
||||
|
||||
for song in context1['songs']:
|
||||
playlist.append(song['id'])
|
||||
|
||||
return {"name": context2['playlist']['name'], "playlist": playlist}
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(getplaylist("8134802489"))
|
@@ -103,6 +103,8 @@ def get_song_lyric(identify: str | int | dict,
|
||||
|
||||
bprint(Fore.YELLOW + "\t-> 歌曲:" + Style.RESET_ALL + f"{name} - {artists}", bar)
|
||||
filename = f"{lyric_format % {'name': name, 'artists': artists}}.lrc"
|
||||
if len(filename) >= 128:
|
||||
filename = filename[:123] + ".lrc"
|
||||
|
||||
try:
|
||||
info = post(f"https://music.163.com/api/song/media?id={identify}").json()
|
||||
|
@@ -3,8 +3,7 @@ from colorama import Fore
|
||||
|
||||
from modules.utils.clear_screen import cls_stay
|
||||
from modules.utils.inputs import rinput
|
||||
from modules.functions.mainly.get_song import get_song_lyric
|
||||
from modules.utils.bar import CompactArrowBar
|
||||
from modules.functions.mainly.download_multiple_songs import donload_multiple_songs
|
||||
|
||||
|
||||
def mdl(self):
|
||||
@@ -31,12 +30,6 @@ def mdl(self):
|
||||
ids.append(int(r))
|
||||
print("\t#%d id:%s - 已添加!" % (len(ids), r))
|
||||
cls_stay(self, "[手动-多个下载]")
|
||||
with CompactArrowBar(f"进度: %(index){len(str(len(ids)))}d/%(max)d",
|
||||
suffix="", max=len(ids), color="yellow", width=9999) as bar:
|
||||
for i in range(0, len(ids)):
|
||||
r = get_song_lyric(ids[i], self.settings.lyric_path, self.settings.lyric_path, bar=bar)
|
||||
if r == "dl_err_connection":
|
||||
bar.print_onto_bar(Fore.RED + "下载发生错误!可能是连接被拒绝!请检查网络后再试\n按回车键继续任务(该任务会被跳过)...")
|
||||
input()
|
||||
bar.next()
|
||||
donload_multiple_songs(self, self.settings.lyric_path, ids)
|
||||
|
||||
input("按回车键返回...")
|
||||
|
45
modules/functions/mainly/playlist_download.py
Normal file
45
modules/functions/mainly/playlist_download.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import re
|
||||
from modules.utils.inputs import rinput
|
||||
from modules.functions.mainly.get_playlist import getplaylist
|
||||
from modules.utils.clear_screen import clear
|
||||
from modules.functions.mainly.download_multiple_songs import donload_multiple_songs
|
||||
from os.path import sep
|
||||
from modules.utils.initapp import mkdir
|
||||
|
||||
def download_by_playlist(self) -> None:
|
||||
"""
|
||||
按照歌单id获取歌词
|
||||
|
||||
:params: path: str 存储歌词的路径
|
||||
|
||||
:return: None
|
||||
|
||||
"""
|
||||
clear()
|
||||
playlist_id = rinput(
|
||||
f"[NeteaseMusicLyricDownloader] {self.version}\n"
|
||||
"[手动-歌单下载]\n"
|
||||
"请输入歌曲id:")
|
||||
try:
|
||||
int(playlist_id)
|
||||
except ValueError:
|
||||
r = re.search("playlist\?id=[0-9]*", playlist_id)
|
||||
if r:
|
||||
playlist_id = r.group()[12:]
|
||||
else:
|
||||
input("不合法的形式.\n按回车键返回...")
|
||||
return
|
||||
|
||||
playlist = getplaylist(playlist_id)
|
||||
|
||||
if not playlist["playlist"]:
|
||||
print("改歌单为空集,下载停止!")
|
||||
input()
|
||||
return
|
||||
|
||||
if mkdir(self.settings.lyric_path+sep+playlist["name"]):
|
||||
print("歌单文件夹已存在")
|
||||
|
||||
donload_multiple_songs(self, self.settings.lyric_path+sep+playlist["name"], playlist["playlist"])
|
||||
input("按回车键返回...")
|
||||
|
Reference in New Issue
Block a user