Skip to content

Commit

Permalink
Version 1.6.9.4.3
Browse files Browse the repository at this point in the history
* 完善 `歌单同步.py` demo 实现
* 默认转换文件名半角逗号`.`为全角版本
  • Loading branch information
mos9527 committed Aug 10, 2023
1 parent 73a29b8 commit 54628d1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
47 changes: 43 additions & 4 deletions demos/歌单同步.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
from pyncm.__main__ import parse_args,logger
from sys import argv
from os import walk,path,remove
from pyncm.__main__ import parse_args ,PLACEHOLDER_URL,__main__
from pyncm import GetCurrentSession,SetCurrentSession,LoadSessionFromString
from pyncm.utils.helper import UserHelper
args, _ = parse_args()
logger.info("读取登录信息 : %s" % args.load)
# 指定 quit_on_empty_args=False 便于传入空 ID
args, _ = parse_args(quit_on_empty_args=False)
print("[-] 读取登录信息 : %s" % args.load)
SetCurrentSession(LoadSessionFromString(open(args.load).read()))
logger.info("用户 : %s" % UserHelper(GetCurrentSession().uid).UserName)
print("[*] 用户 : %s" % UserHelper(GetCurrentSession().uid).UserName)
try:
# 使用未模板化的最高级目录做输出目录起点
output = args.output[:args.output.index('{')]
except IndexError:
output = args.output
print("[*] 输出文件夹起点 : %s" % output)
def normalize(path):
return path.replace('\\','/') # 统一 Windows/Unix 路径格式
# 平面化目录结构,准备后期比对
file_tree = [normalize(path.join(root,file)) for root, dirs, files in walk(output,topdown=False) for file in files]
# 调用 pyncm 下载
if args.url == PLACEHOLDER_URL:
# 未填入 ID 则使用用户本人歌单
argv.append('https://music.163.com/#/user/home?id=%s' % GetCurrentSession().uid)
argv.append("--no-overwrite")
# 不覆写已存在歌曲
# argv 传参,调用 __main__ 即可
queuedTasks, failed_ids = __main__()
# 无视拓展名的文件白名单
file_tree_whitelist = [normalize(task.save_as) for task in queuedTasks]
# 只删除这些拓展名的文件
extension_blacklist = {'m4a','mp3','flac','ogg','lrc','ass'}
for file in file_tree:
delete_flag = True
for file_whitelist in file_tree_whitelist:
if file.startswith(file_whitelist):
delete_flag = False
break
if delete_flag:
ext = file.split('.')[-1].lower()
if ext in extension_blacklist:
try:
print('[!] 删除 %s' % file)
remove(file)
except Exception as e:
print('[!!] 删除 %s 失败: %s' % (file,e))
2 changes: 1 addition & 1 deletion pyncm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# 注意事项
- (PR#11) 海外用户可能经历 460 "Cheating" 问题,可通过添加以下 Header 解决: `X-Real-IP = 118.88.88.88`
"""
__version__ = "1.6.9.4.2"
__version__ = "1.6.9.4.3"

from threading import current_thread
from typing import Text, Union
Expand Down
2 changes: 1 addition & 1 deletion pyncm/utils/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
truncate_length = 64
logger = logging.getLogger("pyncm.helper")

def SubstituteWithFullwidth(string,sub=set('\x00\\/:<>|?*"')):
def SubstituteWithFullwidth(string,sub=set('\x00\\/:<>|?*".')):
return "".join([c if not c in sub else chr(ord(c) + 0xFEE0) for c in string])

def Default(default=None):
Expand Down

0 comments on commit 54628d1

Please sign in to comment.