返回顶部

收藏

从songtaste.com上下载歌曲..

更多

[Python]代码

import sys,urllib,re,os,time,string,math
from time import strftime,localtime,mktime,clock

def main():
    args = sys.argv[1:]
    if len(args) == 0 :
        print 'not correct arguments..'
        sys.exit(0)
    if len(args) == 1 :
        a = args[0].strip().lower()
        # if txt file
        if args[0].endswith('.txt'):
            f = open(args[0], 'r')
            for line in f :
                id = line.replace("\n", "").strip()
                if id == '':
                    continue
                else:
                    run(int(id))

def run(mp3id):
    start = time.clock()  
    response = urllib.urlopen("http://songtaste.com/song/" + str(mp3id) + "/") 
    Result = response.read()
    list = re.findall(r"'player', '([^']+)'.*?(http://\d+\.)'",Result, re.S)

    if len(list[0]) != 2 :
        return

    urllib.urlretrieve(list[0][1] + list[0][0] + '.mp3', './download/'+ str(mp3id) +'.mp3', downloading)
    print 'end..', 'cost time : ' + str(math.floor(time.clock() - start)) + 'seconds!'

def downloading(a, b, c):
    if a == 0 :
        print "download begin ..."
    prec = 100.0*a*b/c
    if 100 < prec:
        prec = 100
    print "%.2f%%"%(prec,)

if __name__ == "__main__":
    main()

标签:下载,歌曲

收藏

0人收藏

支持

0

反对

0

发表评论