Bug fix and optimized some function

This commit is contained in:
1826013250 2022-07-17 18:59:58 +08:00 committed by GitHub
parent 35cd36bad9
commit 9336133002
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 24 deletions

View File

@ -1,11 +1,13 @@
"""多文件下载"""
import re
from modules.clear_screen import clear
from modules.raw_input import rinput
from modules.get_song import get_song_lyric
def mdl(path: str):
"""多个歌词文件的下载
``path: str`` 传入歌词文件保存的路径"""
clear()
ids = []
print("输入歌曲id,用回车分开,输入s停止")
@ -16,13 +18,20 @@ def mdl(path: str):
else:
try:
int(r)
except ValueError:
print("该输入不合法")
tmp = re.search("song\?id=[0-9]*", r)
if tmp:
r = tmp.group()[8:]
else:
print("不合法的形式.\n")
continue
ids.append(r)
print("\t#%d id:%s - 已添加!" % (len(ids), r))
clear()
for i in range(0, len(ids)):
print("进度: %d/%d" % (i+1, len(ids)))
print("\n进度: %d/%d" % (i+1, len(ids)))
if get_song_lyric(ids[i], path) == "dl_err_connection":
input("下载发生错误!可能是连接被拒绝!请检查网络后再试\n按回车键继续任务(该任务会被跳过)...")
input("按回车键返回...")

View File

@ -1,12 +1,10 @@
# 导入自定义模块使用try原因是如果在外部单独运行文件无法通过modules索引到依赖的模块
# 直接单独运行会出现 ModuleNotFoundError 报错
import re
from modules.raw_input import rinput
from modules.get_song import get_song_lyric
from modules.clear_screen import clear
def download_one_lyric(path: str):
def download_one_lyric(song_id, path: str):
"""单次下载歌词
``path: str`` 存储歌词的路径"""
@ -15,8 +13,13 @@ def download_one_lyric(path: str):
try:
int(song_id)
except ValueError:
input("不合法的id形式.\n按回车键返回...")
r = re.search("song\?id=[0-9]*", song_id)
if r:
song_id = r.group()[8:]
else:
input("不合法的形式.\n按回车键返回...")
return
if get_song_lyric(song_id, path) == "dl_err_connection":
input("下载发生错误!可能是连接被拒绝!请检查网络后再试\n按回车键返回...")
input("按回车键返回...")

View File

@ -4,7 +4,7 @@ import json
import os
class Settings(object): # 设定一个基础的存储设置信息的 class
class Settings(object): # 设定一个基础的存储设置信息的 class ,并设置形参用于 json 导入设置
def __init__(self, l_p="./out/", lang="en"):
self.lyric_path = l_p
self.language = lang
@ -31,13 +31,16 @@ def load_settings(): # 加载 的函数
if os.path.exists("settings.json"): # 判断目录下是否存在 settings.json ,若没有则创建,若有则读取
with open("settings.json", 'r', encoding="utf-8") as f:
try:
return json.load(f, object_hook=dict2class)
settings = json.load(f, object_hook=dict2class) # 尝试转换 json 为 dict
if not os.path.exists(settings.lyric_path): # 检测输出文件夹,若文件夹不存在则在启动时创建
os.mkdir(settings.lyric_path)
return settings
except json.decoder.JSONDecodeError: # 如果检测到文件无法读取,将会删除设置文件并重新创建
print("设置文件损坏,重新创建...")
os.remove("settings.json")
return load_settings()
else:
with open("settings.json", 'w', encoding="utf-8") as f:
with open("settings.json", 'w', encoding="utf-8") as f: # 当 settings.json 不存在时新建一个 settings.json 并写入默认配置
f.write(json.dumps(Settings(), default=class2dict))
return Settings()

View File

@ -20,11 +20,11 @@ def settings_menu(self):
elif r == "1":
__set_lyric_path(self)
elif r == "2":
__save_settings(self)
elif r == "s":
__remove_lyric_files(self.settings.lyric_path)
elif r == "s":
__save_settings(self)
else:
input("输入")
input("输入无效!按回车键继续...")
def __remove_lyric_files(path):
@ -37,17 +37,19 @@ def __remove_lyric_files(path):
for i in range(0, len(files)):
print("正在删除(%d/%d): %s" % (i+1, len(files), files[i]))
os.remove(path+files[i])
print("删除完毕!\n按回车继续...")
input("删除完毕!\n按回车继续...")
else:
print("文件夹内没有要删除的东西\n按回车继续...")
input("文件夹内没有要删除的东西\n按回车继续...")
def __set_lyric_path(self):
print("允许使用相对路径和绝对路径,默认为\"./out/\"\n请避免使用反斜杠来确保通用性\n"
clear()
print("允许使用相对路径和绝对路径,默认为\"./out/\"\n请*不要*使用反斜杠来确保通用性\n"
"当前值:%s\n请输入新的歌词保存路径:" % self.settings.lyric_path)
r = cinput()
if not r:
input("输入为空!\n按回车继续...")
return
if r[-1] != "/":
r += "/"
path = ""