从百度下载MP3歌词并把歌词设置在MP3文件中,以便在iphone音乐播放器显示歌词,iphone音乐播放器,[Python]代码#c
从百度下载MP3歌词并把歌词设置在MP3文件中,以便在iphone音乐播放器显示歌词,iphone音乐播放器,[Python]代码#c
[Python]代码
#coding=UTF-8'''Created on 2012-4-5@author: jackliu'''import sysimport osimport eyeD3import reimport urllibclass FileErrorException(Exception): passclass RedApple: def __init__(self): passbaiduurl = 'http://mp3.baidu.com'mp3url = "http://mp3.baidu.com/m?f=ms&rf=idx&tn=baidump3lyric&ct=150994944&lf=&rn=10&lm=-1&word=%s"resultlyrics_r = '<div class="item-lyric">.*?</div>.*?</div>.*?</div>.*?</div>'lyrics_info = '<span class="title_c">(.*?)</span>.*?<span class="title_c">.*?<a.*?>(.*?)</a>.*?<div class="text-lyric-abstract.*?">(.*?)</div>'lyrics_info_r = re.compile(lyrics_info,re.I)next = '<a class=".*?next.*?".*?href="(.*?)">'next_r = re.compile(next,re.I)filetypelist = ['mp3','mP3','Mp3','MP3']class LyricsParser: """ get lyrics from internet """ __mp3Name = "" #音乐名称 __artist = "" #演唱者 __lyrics = "" #歌词 def __init__(self,mp3Name,artist): self.__mp3Name = mp3Name self.__artist = artist self.__lyrics = "" def parse(self): next_url = mp3url % urllib.quote(self.__mp3Name.encode('gbk','ignore')) while next_url is not None: #print next_url try: mp3 = urllib.urlopen(next_url) except: next_url = None continue content = mp3.read(); content = content.decode('gbk','ignore'); content = re.sub('\n+',' ',content) #获取歌词列表 result_lyrics= re.findall(resultlyrics_r,content) match = next_r.search(content) if match: next_url = baiduurl+match.group(1) else: next_url = None for lyric_info in result_lyrics: lyric_info = re.sub("</?em>","",lyric_info) match = lyrics_info_r.search(lyric_info) # print lyric_info if self.__checkLyric(match): return #print 'no lyrics' def __checkLyric(self,match): if match: #print match.group(1),match.group(2) if match.group(1).strip() == self.__mp3Name: #如果歌名匹配 lyrics = None if match.group(3): #存在歌词 lyrics = re.sub("<br.*?>",'\n',match.group(3)) if match.group(2).strip() == self.__artist: #如果作者匹配 if lyrics != None: self.__lyrics = lyrics return True else: #不匹配作者 if self.__lyrics == '': #如果当前歌词为空 self.__lyrics = lyrics return False def getLyric(self): return self.__lyricsdef main(): if len(sys.argv) == 1: print("error: the command like redapple folder|file [folder|file]...") print(" Pelease try again") return; totalMp3File = 0 #总的文件数 successDownload = 0 #成功下载歌词的文件数 for path in sys.argv[1:]: if os.path.isdir(path) or os.path.isfile(path): (total,success) = download(path) totalMp3File += total successDownload += success else: #不是文件目录也不是文件 print("error: %s is not a folder or file" % path) print "Total: {0},success:{1}".format(totalMp3File,successDownload) def download(path): filelist = [] success = 0 maxlen = 0 if os.path.isdir(path): for file in os.listdir(path): fullpathname = os.path.join(path,file) if fullpathname[-3:] in filetypelist: if maxlen < len(fullpathname): maxlen = len(fullpathname) filelist.append(fullpathname) else: fullpathname = path if fullpathname[-3:] in filetypelist: filelist.append(fullpathname) maxlen += 6 for file in filelist: print u"正在处理:",file,(maxlen - len(file))*'.', if downloadLyricsByID3V3(file) or downloadLyricsByFilename(file): success += 1 print u"下载成功" else: print u"下载失败" return len(filelist),success def downloadLyricsByID3V3(file): tag = eyeD3.Tag() tag.link(file) tag.encoding = '\x01' if tag.getLyrics() and len(tag.getLyrics()[0].lyrics): #已经存在歌词 #print tag.getLyrics()[0].lyrics return 1 else: #不存在歌词,需要下载 artist = tag.getArtist() title = re.sub("\(.*?\)",'',tag.getTitle()) lyricsParser = LyricsParser(title,artist) lyricsParser.parse() lyrics = unicode(lyricsParser.getLyric()) if len(lyrics) > 0 : #下载成功 tag.addLyrics(lyrics) tag.update() return 1 return 0def downloadLyricsByFilename(file): tag = eyeD3.Tag() tag.link(file) tag.encoding = '\x01' if tag.getLyrics() and len(tag.getLyrics()[0].lyrics): #已经存在歌词 #print tag.getLyrics()[0].lyrics return 1 else: #不存在歌词,需要下载 filename = file[len(os.path.dirname(file))+1:] artist,title =filename.rsplit('-',1) artist = artist.strip() title = title.strip()[:-4] title = re.sub("\(.*?\)",'',title) #print artist,title lyricsParser = LyricsParser(title.decode('gbk'),artist.decode('gbk')) lyricsParser.parse() lyrics = unicode(lyricsParser.getLyric()) if len(lyrics) > 0 : #下载成功 tag.addLyrics(lyrics) tag.update() return 1 return 0if __name__ == '__main__': main()
相关内容
- 最新python3 的金山快盘登录签到多功能 学习好帮手,
- 使用Python画三维函数图象,python画函数图象,[Python]代码
- 太极匀手,马步必备,太极匀马步必备,[Python]代码#!
- python调用win32应用程序,python调用win32,[Python]代码fr
- 斐波那契数,,[Python]代码#-
- 将List转成XML,List转成XML,[Python]代码#
- 在pymongo中使用sql的like语句,pymongolike,db.houses.fi
- python 解决0-1背包问题算法,python0-1背包算法,#!/usr/bin
- python执行外部程序的四种方法汇总,python四种方法,1、
- python 冒泡,python,[Python]代码#-
评论关闭