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

23
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,26 +30,30 @@ 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:
case "1":
download_one_lyric(self) download_one_lyric(self)
elif r == "2": case "2":
download_by_playlist(self)
case "3":
mdl(self) mdl(self)
elif r == "3": case "4":
get_lyric_from_folder(self) get_lyric_from_folder(self)
elif r == "0": case "0":
exit(0) exit(0)
elif r == "i": case "i":
print_info(self) print_info(self)
elif r == "s": case "s":
settings_menu(self) settings_menu(self)
else: case _:
input("请输入正确的选项\n按回车键继续...") input("请输入正确的选项\n按回车键继续...")

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,18 +56,20 @@ def __remove_output_files(self):
"2": "清除歌曲文件", "2": "清除歌曲文件",
"a": "清除所有文件", "a": "清除所有文件",
}) # 选择清除的文件格式 }) # 选择清除的文件格式
if r == "0":
match r:
case "0":
return return
elif r == "1": case "1":
dellist = [".lrc"] dellist = [".lrc"]
break break
elif r == "2": case "2":
dellist = [".mp3", ".flac"] dellist = [".mp3", ".flac"]
break break
elif r == "a": case "a":
dellist = ["ALL"] dellist = ["ALL"]
break break
else: case _:
input("输入无效!\n按回车键继续...") input("输入无效!\n按回车键继续...")
files = [] files = []
for i in os.listdir(self.settings.lyric_path): # 列出所有文件 for i in os.listdir(self.settings.lyric_path): # 列出所有文件
@ -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:
case "0":
return return
elif r == "1": case "1":
self.settings.lyric_format = "%(name)s - %(artists)s" self.settings.lyric_format = "%(name)s - %(artists)s"
break break
elif r == "2": case "2":
self.settings.lyric_format = "%(artists)s - %(name)s" self.settings.lyric_format = "%(artists)s - %(name)s"
break break
elif r == "3": case "3":
self.settings.lyric_format = "%(name)s" self.settings.lyric_format = "%(name)s"
break break
else: case _:
input("输入无效!\n按回车继续...") 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)