Пример #1
0
def search(songs) :
	process = 5								#最大进程数
	total = len(songs.slist)
	notfoundlist = []						#未找到歌曲列表
	i,j = 0,0
	PS = {}									#进程pid与歌曲名字的字典
	path = raw_input('Path to download : ')
	if path :
		if not os.path.exists(path) : os.mkdir(path)
		os.chdir(path)

	current_path = os.getcwd()
	print 'Now download songs to :',current_path
	songlist = filterlist(songs.namelist,songs.slist,current_path) 
	print 'There are %d in list and %d need to download' % (len(songs.slist),len(songlist))
#	for (i,(song,album,singer)) in enumerate(songlist) :
#		print song+'\t'+album+'\t'+singer

	try :
		for (i,(song,album,singer)) in enumerate(songlist) :
			if process <= 0:
				pid,failed = os.wait()
				process += 1
				if failed :
					print 'Download Failed in',song
					notfoundlist.append(PS[pid])
				del PS[pid]

			process -= 1
			pid = os.fork()
			if not pid :
				print '%3d now trying to download %s' % (i,song)
				linklist = Sougou.select(song,album,singer)+Yahoo.select(song,album,singer)#+Top100.select(song,album,singer)
				if not linklist : exit(1)
				linklist.sort()
				for distinction,downlink in linklist[:3] :
					exname = downlink.split('.')[-1].rstrip().lower().split('?')[0]
					filename = song+'.'+exname
					if Download.begin_download(downlink,filename,5) :
						print 'Download complete : ',song
						exit()
					else :
						print 'Download crashed : ',song,'\tTry to download from another source'
				exit(1)
			else :
				PS[pid]=(song,album,singer)


		for key in PS.keys() :
			pid,failed = os.wait()
			process += 1
			if failed :
				print 'Download Failed in',song
				notfoundlist.append(PS[pid])
				del PS[pid]

	except KeyboardInterrupt :
		for key in PS.keys() :
			print key
		for key in PS.keys() :
			os.kill(key,9)
	
	
	if notfoundlist :											#输出未找到的歌曲
		print 'Failed to find following songs'
		for song,album,singer in notfoundlist :
			print song+'\t'+album+'\t'+singer
Пример #2
0
def search(songs):
    process = 5  #最大进程数
    total = len(songs.slist)
    notfoundlist = []  #未找到歌曲列表
    i, j = 0, 0
    PS = {}  #进程pid与歌曲名字的字典
    path = raw_input('Path to download : ')
    if path:
        if not os.path.exists(path): os.mkdir(path)
        os.chdir(path)

    current_path = os.getcwd()
    print 'Now download songs to :', current_path
    songlist = filterlist(songs.namelist, songs.slist, current_path)
    print 'There are %d in list and %d need to download' % (len(
        songs.slist), len(songlist))
    #	for (i,(song,album,singer)) in enumerate(songlist) :
    #		print song+'\t'+album+'\t'+singer

    try:
        for (i, (song, album, singer)) in enumerate(songlist):
            if process <= 0:
                pid, failed = os.wait()
                process += 1
                if failed:
                    print 'Download Failed in', song
                    notfoundlist.append(PS[pid])
                del PS[pid]

            process -= 1
            pid = os.fork()
            if not pid:
                print '%3d now trying to download %s' % (i, song)
                linklist = Sougou.select(song, album, singer) + Yahoo.select(
                    song, album, singer)  #+Top100.select(song,album,singer)
                if not linklist: exit(1)
                linklist.sort()
                for distinction, downlink in linklist[:3]:
                    exname = downlink.split('.')[-1].rstrip().lower().split(
                        '?')[0]
                    filename = song + '.' + exname
                    if Download.begin_download(downlink, filename, 5):
                        print 'Download complete : ', song
                        exit()
                    else:
                        print 'Download crashed : ', song, '\tTry to download from another source'
                exit(1)
            else:
                PS[pid] = (song, album, singer)

        for key in PS.keys():
            pid, failed = os.wait()
            process += 1
            if failed:
                print 'Download Failed in', song
                notfoundlist.append(PS[pid])
                del PS[pid]

    except KeyboardInterrupt:
        for key in PS.keys():
            print key
        for key in PS.keys():
            os.kill(key, 9)

    if notfoundlist:  #输出未找到的歌曲
        print 'Failed to find following songs'
        for song, album, singer in notfoundlist:
            print song + '\t' + album + '\t' + singer