源码:批量修改喜马拉雅下载节⽬的⽂件名长久未登录CSDN了,发现之前的博⽂,下有很多朋友留⾔,需要代码的⼈很多,看来有困难的⼈还不少。
在此,贴出代码,有需要的朋友可以下载使⽤。
⽤法:
cd 下载⽬录 (喜马拉雅App⾥可以看到这个下载⽬录设置)
python Himalaya-TitleName.py南京大屠杀是哪一天
之后⽬录名和⽂件名就⾃动修改好了,重复执⾏好像存在⼀点问题,其它问题请遇到的朋友告诉我。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#Author: xDoom (李洪伟)
邓亚萍的个人资料import os, json, string
import re
#Windows⽂件名中的⾮法字符. 注意这⾥的\\和\"是⽤了转义符
illegal_Char = repile(r"[/\\:*?\"<>|]")
#
#读取⽬录中的所有⽂件,返回列表
#
def getfileList( p ):
p = str( p )
if p=="":
return [ ]
p = p.replace( "/","\\")
if p[ -1] != "\\":
p = p+"\\"
a = os.listdir( p )
b = [ x  for x in a if os.path.isfile( p + x ) ]
return b
#
#修改⽬录下的所有⽂件名
海涛打人事件#
怎么拍拍别人
def ren_files(dir):
updir = os.getcwd()
subdir =  ".\\"+dir
if not ists(subdir):
return
os.chdir(subdir)
wd())
>修改⽂件>
#获取上级⽬录下的list⽂件,是个多维JSON,包括了⽬录下所有节⽬信息
infofilepath = updir + "\\" + dir + "list.json"
infofile = open(infofilepath,'r',encoding = "utf-8")
playlist = json.load(infofile)
playlist = json.load(infofile)
#print(playlist)
#print(len(playlist))
for i in range(len(playlist)):
print(playlist[i]["uid"])
#some item not real file
if (playlist[i]["uid"] == 0):
continue
old =  str(playlist[i]["id"]) + ".m4a"
new = playlist[i]["title"] + ".m4a"
new = re.sub(illegal_Char,"x",new)
#同⼀⽬录下会有同名⽂件,奇葩的喜马拉雅
if (ists(new)):
new = new = playlist[i]["title"] + str(playlist[i]["trackId"]) + ".m4a"
new = re.sub(illegal_Char,"x",new)
十大高端门窗品牌#print总是要按控制台的默认编码打印,有些节⽬名包含“0xc2a0”这个字符,是特殊的“-”符号,
#GBK⽆法编码这个字符,所以我们这⾥先主动编码⼀下,对⽆法解释的字符选择ignore,然后解码,  print(old,"==>", de("gbk","ignore").decode("gbk"))
try:
except Exception as e:
print(e)
infofile.close()
>修改⽂件结束>
os.chdir(updir)
>修改⽬录名>
infofilepath = updir + "\\" + dir + "info.json"
print(infofilepath)
infofile = open(infofilepath,'r',encoding = "utf-8")
playlist = json.load(infofile)
征服 电视剧
old =  dir
new = playlist["title"]
new = re.sub(illegal_Char,"x",new)
#改变⽬录名,如果重复执⾏
if (ists(new)):
new = new + "-1"
print("dir: ",old,"==>", new)
try:
except Exception as e:
print(e)
infofile.close()
>修改⽬录名结束>
return
if __name__ == '__main__':
dirlist =  wd())
#喜马拉雅的节⽬名都是字符编码
pattern = repile(r"\d*\d")
# 每个节⽬有xxxinfo.json和xxxlist.json两个⽂件,只进⼀个即可 visitdir = []
for album in dirlist:
dirindex = pattern.findall(album)
#print(dirindex)
if  not ( dirindex in visitdir ) and (dirindex != []):
try:
visitdir.append(dirindex)
ren_files(dirindex[0])
except IOError as msg:
print("IOError. Msg=", msg)
print("Done!")