feat: 增加了根据歌单下载的功能,修复了多个音乐一块下载时文件名格式无效的问题,优化代码

增加了根据歌单下载的功能,修复了多个音乐一块下载时文件名格式无效的问题,优化代码
This commit is contained in:
Onlyacat233 2023-08-12 00:37:05 +08:00
parent f03d578d9d
commit 806e6e6ea4
9 changed files with 156 additions and 55 deletions

4
.gitignore vendored
View File

@ -12,3 +12,7 @@ modules/functions/mainly/__pycache__
modules/functions/settings/__pycache__ modules/functions/settings/__pycache__
modules/submenus/__pycache__ modules/submenus/__pycache__
modules/utils/__pycache__ modules/utils/__pycache__
node_modules
package.json
yarn.lock

37
main.py
View File

@ -15,6 +15,7 @@ from modules.submenus.settings import settings_menu
from modules.functions.settings.save_load_settings import load_settings from modules.functions.settings.save_load_settings import load_settings
from modules.utils.clear_screen import cls_stay from modules.utils.clear_screen import cls_stay
from modules.functions.mainly.load_file_song import get_lyric_from_folder from modules.functions.mainly.load_file_song import get_lyric_from_folder
from modules.functions.mainly.playlist_download import download_by_playlist
class MainProcess(object): class MainProcess(object):
@ -29,27 +30,31 @@ class MainProcess(object):
print_menu({ print_menu({
"0": "退出程序", "0": "退出程序",
"1": "单个歌曲的歌词下载", "1": "单个歌曲的歌词下载",
"2": "多个歌曲的歌词下载", "2": "通过歌单下载",
"3": "从网易云下载的歌曲中获取歌词", "3": "多个歌曲的歌词下载",
"4": "从网易云下载的歌曲中获取歌词",
"s": "进入设置", "s": "进入设置",
"i": "程序信息", "i": "程序信息",
}) })
r = rinput("请选择:") r = rinput("请选择:")
if r == "1": match r:
download_one_lyric(self) case "1":
elif r == "2": download_one_lyric(self)
mdl(self) case "2":
elif r == "3": download_by_playlist(self)
get_lyric_from_folder(self) case "3":
elif r == "0": mdl(self)
exit(0) case "4":
elif r == "i": get_lyric_from_folder(self)
print_info(self) case "0":
elif r == "s": exit(0)
settings_menu(self) case "i":
else: print_info(self)
input("请输入正确的选项\n按回车键继续...") case "s":
settings_menu(self)
case _:
input("请输入正确的选项\n按回车键继续...")
if __name__ == "__main__": if __name__ == "__main__":

View 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()

View 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"))

View File

@ -103,6 +103,8 @@ def get_song_lyric(identify: str | int | dict,
bprint(Fore.YELLOW + "\t-> 歌曲:" + Style.RESET_ALL + f"{name} - {artists}", bar) bprint(Fore.YELLOW + "\t-> 歌曲:" + Style.RESET_ALL + f"{name} - {artists}", bar)
filename = f"{lyric_format % {'name': name, 'artists': artists}}.lrc" filename = f"{lyric_format % {'name': name, 'artists': artists}}.lrc"
if len(filename) >= 128:
filename = filename[:123] + ".lrc"
try: try:
info = post(f"https://music.163.com/api/song/media?id={identify}").json() info = post(f"https://music.163.com/api/song/media?id={identify}").json()

View File

@ -3,8 +3,7 @@ from colorama import Fore
from modules.utils.clear_screen import cls_stay from modules.utils.clear_screen import cls_stay
from modules.utils.inputs import rinput from modules.utils.inputs import rinput
from modules.functions.mainly.get_song import get_song_lyric from modules.functions.mainly.download_multiple_songs import donload_multiple_songs
from modules.utils.bar import CompactArrowBar
def mdl(self): def mdl(self):
@ -31,12 +30,6 @@ def mdl(self):
ids.append(int(r)) ids.append(int(r))
print("\t#%d id:%s - 已添加!" % (len(ids), r)) print("\t#%d id:%s - 已添加!" % (len(ids), r))
cls_stay(self, "[手动-多个下载]") cls_stay(self, "[手动-多个下载]")
with CompactArrowBar(f"进度: %(index){len(str(len(ids)))}d/%(max)d", donload_multiple_songs(self, self.settings.lyric_path, ids)
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()
input("按回车键返回...") input("按回车键返回...")

View 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("按回车键返回...")

View File

@ -56,19 +56,21 @@ def __remove_output_files(self):
"2": "清除歌曲文件", "2": "清除歌曲文件",
"a": "清除所有文件", "a": "清除所有文件",
}) # 选择清除的文件格式 }) # 选择清除的文件格式
if r == "0":
return match r:
elif r == "1": case "0":
dellist = [".lrc"] return
break case "1":
elif r == "2": dellist = [".lrc"]
dellist = [".mp3", ".flac"] break
break case "2":
elif r == "a": dellist = [".mp3", ".flac"]
dellist = ["ALL"] break
break case "a":
else: dellist = ["ALL"]
input("输入无效!\n按回车键继续...") break
case _:
input("输入无效!\n按回车键继续...")
files = [] files = []
for i in os.listdir(self.settings.lyric_path): # 列出所有文件 for i in os.listdir(self.settings.lyric_path): # 列出所有文件
if dellist[0] == "ALL": if dellist[0] == "ALL":
@ -133,18 +135,19 @@ def __set_lyric_filename_format(self):
"2": "%(artists)s - %(name)s" % {"name": "曲名", "artists": "歌手名"}, "2": "%(artists)s - %(name)s" % {"name": "曲名", "artists": "歌手名"},
"3": "%(name)s" % {"name": "曲名", "artists": "歌手名"}, "3": "%(name)s" % {"name": "曲名", "artists": "歌手名"},
}) })
if r == "0": match r:
return case "0":
elif r == "1": return
self.settings.lyric_format = "%(name)s - %(artists)s" case "1":
break self.settings.lyric_format = "%(name)s - %(artists)s"
elif r == "2": break
self.settings.lyric_format = "%(artists)s - %(name)s" case "2":
break self.settings.lyric_format = "%(artists)s - %(name)s"
elif r == "3": break
self.settings.lyric_format = "%(name)s" case "3":
break self.settings.lyric_format = "%(name)s"
else: break
input("输入无效!\n按回车继续...") case _:
input("输入无效!\n按回车继续...")
input("修改成功! \n按回车返回...") input("修改成功! \n按回车返回...")
return return

View File

@ -1,11 +1,17 @@
from os import mkdir from os import mkdir as raw_mkdir
from os.path import exists from os.path import exists
INIT_DIRECTORIES = [ INIT_DIRECTORIES = [
'out' 'out'
] ]
def mkdir(dirpath: str) -> bool:
if not exists(dirpath):
raw_mkdir(dirpath)
return [True if exists(dirpath) else False][0]
def init_directories(): def init_directories():
for dir_name in INIT_DIRECTORIES: for dir_name in INIT_DIRECTORIES:
if not exists(dir_name): mkdir(dir_name)
mkdir(dir_name)