|
发表于 2006-11-7 23:06:40
|
显示全部楼层
非常感谢楼主的程序,稍微修改了一下,能在windows上运行了。
所做的修改主要有3处:
1.编码的处理,将汉字都处理成unicode,包括artist, title,这样,不管在什么样的Locale下都能正确显示。
2.调用wget代替axel,因为我没有找到win32 native的axel.只是wget是单线程的,速度慢了点。
3.在下载top100的时候,如果出现某首歌无法下载的时候,不会退出,而是继续下载下一首。
patch如下:
- --- DownloadSong Mon Oct 30 21:54:34 2006
- +++ DownloadSong-zsx.py Tue Nov 07 22:54:54 2006
- @@ -59,6 +59,8 @@
- else:
- title = v
- artist = ''
- + artist = artist.decode('utf-8')
- + title = title.decode('utf-8')
- info = {'artist':artist, 'title':title}
- songs[k] = info
-
- @@ -80,16 +82,17 @@
- """Search results in baidu can't be downloaded directly,
- this function get top 30(or less) urls from the results.
- arguments:
- - artist: artist
- - title: title/song name
- + artist: artist, unicode
- + title: title/song name, unicode
- return values:
- - urls: urls got from search results.
- + urls: urls got from search results. in GBK encoding
- """
- baseurl = 'http://mp3.baidu.com/m?f=ms&tn=baidump3&ct=134217728&lf=&rn=&lm=0&word='
- #multi artist are seperated by '_', replace it with space here
- artist = artist.replace('_', ' ')
- keyword = '%s %s' %(artist, title)
- - keyword = keyword.decode('utf8', 'ignore').encode('gbk')
- + #keyword = keyword.decode('utf8', 'ignore').encode('gbk')
- + keyword = keyword.encode('gbk')
- url = baseurl + urllib.quote(keyword, string.punctuation)
-
- html = urllib2.urlopen(url).read()
- @@ -153,14 +156,18 @@
- return (length, etime)
-
- def DownloadSong(artist, title):
- - filename = '%s-%s.mp3' %(artist, title)
- + ''' artist: unicode
- + title: unicode
- + '''
- + filename = u'%s-%s.mp3' % (artist, title)
- if title == '':
- return
- if artist == '':
- - filename = '%s.mp3' %title
- - print "准备下载《%s - %s》..." %(artist, title)
- + filename = u'%s.mp3' % title
- + filename = filename.encode(sys.getfilesystemencoding())
- + print u"准备下载《%s - %s》..." %(artist, title)
- if os.path.exists(filename) and not os.path.exists(filename + '.st'):
- - print "已经成功下载《%s - %s》"%(artist, title)
- + print u"已经成功下载《%s - %s》"%(artist, title)
- print
- return
- fakeurls = _getFakeURLs(artist, title)
- @@ -175,7 +182,7 @@
- except:
- pass
- if os.path.exists(filename):
- - print "已经成功下载《%s - %s》"%(artist, title)
- + print u"已经成功下载《%s - %s》"%(artist, title)
- print
- return
- try:
- @@ -184,11 +191,12 @@
- continue
- if length<2*1024*1024 or etime>5000:
- continue
- - os.system('axel "%s" -o "%s" -a' %(url, filename))
- + os.system('e:/tools/wget/wget.exe "%s" -O "%s"' %(url, filename))
- + #os.system('axel "%s" -o "%s" -a' %(url, filename))
- continue
- #所有的url都已尝试完毕,仍没有下载到
- if not os.path.exists(filename):
- - print "Sorry, 目前并没有为(%s - %s)找到合适的下载资源,\n您可以手动下载或稍候再试。" %(artist, title)
- + print u"Sorry, 目前并没有为(%s - %s)找到合适的下载资源,\n您可以手动下载或稍候再试。" %(artist, title)
- print
-
-
- @@ -197,8 +205,13 @@
- for rank, info in songs.items():
- artist = info['artist']
- title = info['title']
- - print "正在下载第%d首(共%d首) 歌手:%s 曲名:%s" %(rank, len(songs), artist, title)
- - DownloadSong(artist, title)
- + print u"正在下载第%d首(共%d首) 歌手:%s 曲名:%s" %(rank, len(songs), artist, title)
- + try:
- + DownloadSong(artist, title)
- + except KeyboardInterrupt:
- + break
- + except :
- + print u'出现了未知错误导致下载第%d首(共%d首) 歌手:%s 曲名:%s 失败了。' %(rank, len(songs), artist, title)
-
- def Help():
- helpstr = """Usage: %s [OPTION]
复制代码
好久没弄python了,改得非常仓促,都没仔细看代码,发现可以使用了就停手了。;) |
|