源码:批量修改喜马拉雅下载节目的文件名

长久未登录CSDN了,发现之前的博文,批量修改喜马拉雅下载节目的文件名下有很多朋友留言,需要代码的人很多,看来有困难的人还不少。

在此,贴出代码,有需要的朋友可以下载使用。

用法:

         cd 下载目录 (喜马拉雅App里可以看到这个下载目录设置)

         python Himalaya-TitleName.py

之后目录名和文件名就自动修改好了,重复执行好像存在一点问题,其它问题请遇到的朋友告诉我。

Python源码下载链接: https://pan.baidu.com/s/10UruUhPptxqPAxBvCHUvqw 提取码: hcc8 

#!/usr/bin/python
# -*- coding: UTF-8 -*-

#Author: xDoom (李洪伟)

import os, json, string
import re

#Windows文件名中的非法字符. 注意这里的\\和\"是用了转义符
illegal_Char = re.compile(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 os.path.exists(subdir):
		return 
	
	os.chdir(subdir)	
	print(os.getcwd())
	
	
	#####修改文件#####
	#获取上级目录下的list文件,是个多维JSON,包括了目录下所有节目信息
	infofilepath = updir + "\\" + dir + "list.json"
	
		
	infofile = open(infofilepath,'r',encoding = "utf-8")
	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 (os.path.exists(new)):
			new = new = playlist[i]["title"] + str(playlist[i]["trackId"]) + ".m4a"
			new = re.sub(illegal_Char,"x",new)
		
		#print总是要按控制台的默认编码打印,有些节目名包含“0xc2a0”这个字符,是特殊的“-”符号,
		#GBK无法编码这个字符,所以我们这里先主动编码一下,对无法解释的字符选择ignore,然后解码,
		print(old,"==>", new.encode("gbk","ignore").decode("gbk"))
		
		try:
			os.rename(old, new);		
		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 (os.path.exists(new)):
		new = new + "-1"
		
	print("dir: ",old,"==>", new)
	
	try:
		os.rename(old, new);
	except Exception as e:
			print(e)
		
	infofile.close()
		
	#####修改目录名结束#####
	
	return



if __name__ == '__main__':

	dirlist =  getfileList(os.getcwd())

	#喜马拉雅的节目名都是字符编码
	pattern = re.compile(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!")
				

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值