Created
February 2, 2010 08:53
-
-
Save vvoody/292525 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- getCover.py 2010-01-25 21:44:58.000000000 +0800 | |
+++ getCover/getCover.py 2010-01-25 15:30:40.000000000 +0800 | |
@@ -3,10 +3,14 @@ | |
# @author mingcheng<i.feelinglucky@gmail.com> | |
# @site http://www.gracecode.com/ | |
# @date 2010-01-22 | |
import eyeD3, re, os, sys, time, urllib | |
urlread = lambda url: urllib.urlopen(url).read() | |
+searchkeys = {} | |
+albumcovers = {} | |
+coverdict = {} | |
class getAlbumCover: | |
'''从豆瓣获取专辑封面数据,并写入对应的 mp3 文件''' | |
@@ -14,8 +18,8 @@ | |
_eyeD3 = None | |
# 豆瓣搜索以及专辑封面相关的 API 和格式 | |
- _doubanSearchApi = 'http://www.douban.com/subject_search?search_text={0}&cat=1003' | |
- _doubanCoverPattern = '<img src="http://t.douban.com/spic/s(\d+).jpg"' | |
+ _doubanSearchApi = 'http://api.douban.com/music/subjects?q={0}&max-results=1' | |
+ _doubanCoverPattern = 'http://t.douban.com/spic/s(\d+).jpg' | |
_doubanConverAddr = 'http://www.douban.com/lpic/s{0}.jpg' | |
artist = '' # 演唱者 | |
@@ -55,6 +59,10 @@ | |
''' 从豆瓣获取专辑封面的 URL ''' | |
if not len(keywords): | |
keywords = self.artist + ' ' + (self.album or self.title) | |
+ if keywords in searchkeys: | |
+ return searchkeys[keywords] | |
+ if self.album in albumcovers: | |
+ return albumcovers[self.album] | |
request = self._doubanSearchApi.format(urllib.quote(keywords)) | |
result = urlread(request) | |
@@ -63,7 +71,9 @@ | |
match = re.compile(self._doubanCoverPattern, re.IGNORECASE).search(result) | |
if match: | |
- return self._doubanConverAddr.format(match.groups()[0]) | |
+ cover_addr = self._doubanConverAddr.format(match.groups()[0]) | |
+ albumcovers[self.album] = searchkeys[keywords] = cover_addr | |
+ return cover_addr | |
else: | |
return False | |
@@ -77,15 +87,17 @@ | |
#print '[内容]', handler.artist, handler.title, | |
cover_addr = handler.getCoverAddrFromDouban() | |
if cover_addr: | |
- cover_file = 'cover.jpg' | |
- f = file(cover_file, 'w') | |
- f.write(urlread(cover_addr)) | |
- f.close() | |
+ cover_file = cover_addr.split('/')[-1] | |
+ if not cover_file in coverdict: | |
+ coverdict[cover_file] = None | |
+ f = file(cover_file, 'w') # Use 'wb' under Windows | |
+ f.write(urlread(cover_addr)) | |
+ f.close() | |
if handler.updateCover(cover_file): | |
print '[完成]' | |
else: | |
print '[失败]' | |
- os.remove(cover_file) | |
+# os.remove(cover_file) | |
else: | |
print '[失败]' | |
handler = None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment