一个音乐管理的脚本
reiase
|
1#
reiase 发表于 2006-07-29 12:28
一个音乐管理的脚本
刚刚开始用python,写写玩的
发现还算好用,以后想做得正规点 Usage:mp3repos [源路径] [目标路径][格式串] 在[源路径] 搜索mp3文件,复制到[目标路径],拷贝时根据[格式串]和ID3标签组织目录结构 比如: %a/%l/%n-%t.%f表示 artist/album/tracknumber-title.filetype 在我的系统上能支持utf8格式的ID3 需要id3-py库,http://id3-py.sourceforge.net/
[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/env python
#m3m -- mp3manager by Reiase import os import sys import string import re import popen2 from ID3 import * from output import blue, bold, colorize, darkblue, darkgreen, darkred, green, \ havecolor, nc_len, nocolor, red, teal, turquoise, white, xtermTitle, \ xtermTitleReset, yellow def msgn(msg): """Print normal message""" return blue(msg) def msgo(msg): """Print object message""" return yellow(msg) def msgu(msg): """Print User input message""" return darkgreen(msg) def msgs(msg): """Print important message""" return red(msg) class Repository: """Repository is another name for the directory you want to manage A Repository is something like database,It know want to do and will try to do it right. Not like filesystem you need command from operate system. """ FormatMap = { '%a':'ARTIST', '%l':'ALBUM', '%g':'GENRE', '%t':'TITLE', '%n':'TRACKNUMBER', # '%c':'COMMENT' } def __init__(self,RootPath,FormatString = '%a/%l/%n %t.mp3',FileType = 'mp3'): print msgn('Initializing ')+msgo('Repository')+msgu(RootPath) if os.path.isdir(RootPath): self.RootPath = RootPath else: print msgu(RootPath)+msgn("is Not a derectory.") sys.exit(1) self.setFileType(FileType) self.Format = FormatString def getFileList(self): self.FileList = [] ListFromLs = popen2.popen2('ls -Rl "'+self.RootPath+'"')[0] line = ListFromLs.readline() while line: if re.search('^.*:$',line): CurrentPath = re.findall('^(.*):$',line)[0] for file in os.listdir(CurrentPath): file = os.path.join(CurrentPath,file) if os.path.isfile(file) and self.matchType(file): self.FileList.append(file) line = ListFromLs.readline() def importFile(self,Source,Target): workingDir=self.RootPath (Target,FileName) = os.path.split(Target) workingDir = os.path.join(workingDir,Target) if not os.path.isdir(workingDir): os.system('mkdir -p "'+workingDir+'"') cpCommand='cp "'+Source+'" "'+os.path.join(workingDir,FileName)+'"' if not os.system(cpCommand): print msgs('\t...[OK]') def importMp3(self,FileName): Music = MusicObject(FileName) Music.getInfomation() print msgn('----Importing Music ')+msgu(FileName) self.importFile(FileName,self.targetPath(Music)) def setFileType(self,Filetype): self.FileType = Filetype print msgn('--Set ')+msgo('Repository:'+self.RootPath)+msgn(' to mode ')+msgu(self.FileType) def matchType(self,Filename): pattern = ".*\.("+self.FileType+")$" if re.search(pattern,string.lower(Filename)): return True return False def targetPath(self,Music): Target = self.Format for key in self.FormatMap: target = Music.info[self.FormatMap[key]] Target = re.sub(key,target,Target) return Target class MusicObject (ID3): "This is a music file" def __init__(self,FullName): file=open(FullName,'rb') ID3.__init__(self,file,FullName) self.FullName = FullName self.Path,self.FileName = os.path.split(FullName) def getInfomation(self): self.index = { 'ALBUM' :'Unknow Album', 'GENRE' :'Unknow Genre', 'ARTIST' :'Unknow Artist', 'TRACKNUMBER' :'??', 'TITLE' :'Unknow Title' } self.info = self.as_dict() self.clearInfomation() def clearInfomation(self): for keys in self.info: self.info[keys] = re.sub('_',' ',self.info[keys]) self.info[keys] = re.sub('~',' ',self.info[keys]) for keys in self.index: if not(self.has_key(keys)): self.info[keys]=self.index[keys] def main(): print 'Import Music from '+sys.argv[1]+' To '+sys.argv[2] fromRepos = Repository(sys.argv[1]) toRepos = Repository(sys.argv[2]) fromRepos.getFileList() for file in fromRepos.FileList: toRepos.importMp3(file) main() |