以前写的下载yahoo mp3的python脚本

以前写的下载yahoo mp3的代码,不知道现在还能不能用;
yahoomp3类根据命令行输入的歌曲名,从chinamp3获取歌曲列表并打印到屏幕,
需要选择一个,然后会通过wget进行下载(wget在linux系统下默认都有的,windows下需要另外下载),歌曲下载完后,自动修改ID3标签,这个通过Ben Gertzfield写的ID3.py进行修改的。

downmp3.py


#! /usr/bin/python
# create by chenyc version:0.3

import re,urllib,os,socket,locale,sys,getopt,time,urllib2
import ID3
LOCALE_CODE=locale.getdefaultlocale()[1]
WEB_CODE='gbk'

'''DOWN MUSIC FILE FORM MP3.YAHOO.CN'''
class yahoomp3:
    def __init__(self,time_out=0.3,directory='./'):
        self.time_out=time_out
        if directory[-1]<>'/':
           directory=directory+'/'
        self.path=directory

    #analyse html,return result list
    def analyse_html(self,url,expression):
        socket.setdefaulttimeout(10)
        try:
            urldata=urllib.urlopen(url)
            html=urldata.read()
            re_list=re.findall(expression,html)
            return re_list
        except:
            print 'can''t connect to the yahoo website! '
            return 1

    #get the music file download url from the search url
    def get_down_url(self,search_url):
        expression='<a href="(.*)" onclick="return'
        music_url_list=self.analyse_html(search_url,expression)
        i=0
        while len(music_url_list)>0 and i<len(music_url_list) :
            expression='</b><a href="(.*)" target=_blank>'
            down_url_list=self.analyse_html(music_url_list[i],expression)
            if len (down_url_list)>0:
                down_url='http://'+urllib.quote(down_url_list[0][7:])
            print down_url
            try:
                socket.setdefaulttimeout(self.time_out)
                tmp=urllib.urlopen(down_url).readline()
                if  len(tmp) >1 and tmp.upper().find('<')==-1:
                    break
                else:
                    i=i+3
            except:
                i=i+3

        if i<len(music_url_list):
            return down_url

    #generate search url and call method 'down_file'
    def down_music(self,name,author='',file_type='all'):
        if author is  None:
            print 'the author not find in chinamp3,do you want to download anyway?'
            if str(raw_input('input y or n for continue:')).upper() <> 'Y':
                return 1
        else:
            author=author.decode(LOCALE_CODE).encode(WEB_CODE)

        name=name.decode(LOCALE_CODE).encode(WEB_CODE)
        name='_'.join(name.split())
        search_url='http://music.cn.yahoo.com/search?pid=ysearch&source=ysearch_music_result_topsearch&p='
        option='&mimetype='+file_type+'&size=3&source=ysearch_music_result_size_3'
        if author is not None:
            search_url=search_url+name+'+'+author+option
        else:
            search_url=search_url+name+'+'+option
        self.down_file(name,author,search_url)

    #use wget to download music file
    def down_file(self,music_name,author,search_url):
        file_path=self.path
        if author is not None:
            file_name=author+'_'+music_name
        else:
            file_name=music_name
        file_name=file_name.decode(WEB_CODE).encode(LOCALE_CODE)
        try:
            file_list=os.listdir(file_path)
        except:
            print 'list dir error,abort!'
            return 1

        for name in file_list:
            if name.split('.')[0]==file_name:
                print 'file '+file_name+' exit,ignore download!'
                return 1

        down_url=self.get_down_url(search_url)
        if down_url is not None:
            print 'down music '+file_name
            ext=down_url.split('.')[-1]
            file_name=file_name+'.'+ext
            exe_wgetstr='wget --timeout=30 -l 10 -O '+file_path+file_name+' '+down_url
            os.system(exe_wgetstr)
            change_ID3(file_path,file_name)
        else:
            print 'FILE NOT FOUND!'

'''GET MUSIC INFO FROM CHINAMP3.COM'''
class music_info:
    #return to post requst from chinamp3
    def get_html(self,url,postdata):
        urldata=urllib2.urlopen(url, postdata)
        html=urldata.read().decode(WEB_CODE).encode(LOCALE_CODE)
        p=re.compile('</div></td></tr>')
        html=p.sub('\n',html)
        return html

    #get author list from music name
    def get_author_list(self,name):
        name=name.decode(LOCALE_CODE).encode(WEB_CODE)
        value=2
        url='http://search.chinamp3.com/search/searchcontent.php'
        postdata=urllib.urlencode({'type':value,'key':name})
        html=self.get_html(url,postdata)
        exp='&singer_name=(.*)\'\)}">'
        author_list=re.findall(exp,html)
        return author_list

    #get music name list from author
    def get_music_name(self,author):
        author=author.decode(LOCALE_CODE).encode(WEB_CODE)
        value=1
        url='http://search.chinamp3.com/search/searchcontent.php'
        postdata=urllib.urlencode({'type':value,'key':author})
        html=self.get_html(url,postdata)
        exp='php\?song_name=(.*)&singer_name='
        music_list=re.findall(exp,html)
        return music_list

    #get lyrics form music name and author
    def get_lyrics(self,name,author):
        name=name.decode(LOCALE_CODE).encode(WEB_CODE)
        author=author.decode(LOCALE_CODE).encode(WEB_CODE)
        url='http://geci.chinamp3.com/relatedgeci.php?song_name='+name+'&singer_name='+author
        urldata=urllib.urlopen(url)
        html=urldata.read().decode(WEB_CODE).encode(LOCALE_CODE)
        p=re.compile('\s')
        html=p.sub('',html)
        exp='</font><br /><br />(.*)</div></td></tr><tr><td><divalign="center"><fontcolor="#FF0000">'
        lyrics_list=re.findall(exp,html)
        if len(lyrics_list)>0:
            lyrics=lyrics_list[0]
            p=re.compile('<br />|<br/>')
            lyrics=p.sub('\n',lyrics)
            return lyrics

#change the ID3
def change_ID3(file_path,file_name):
    mp3file=file_path+file_name
    try:
        id3info=ID3.ID3(mp3file)
    except:
        print 'open file error!'
        return 1
    mp3info=file_name.split('.')[0].split('_')
    if len(mp3info)==2:
        id3info['ARTIST']=mp3info[0]
        id3info['TITLE']=mp3info[1]
    elif len(mp3info)==1:
        id3info['TITLE']=mp3info[0]

#get author from name
def get_author(name):
    mp3info=music_info();
    author_list=mp3info.get_author_list(name)
    count=len(author_list)
    if count>1:
        print 'please select author from the list:'
        i=0
        for author in author_list:
            print str(i)+'.'+author
            i=i+1
        while True:
            try:
                select=int(raw_input('please select one author:'))
            except:
                continue
            if select<len(author_list):
                author=author_list[select]
                return author
            else:
                print "you select the worng num,please select again!"
                continue
    elif count==1:
        return author_list[0]

# the main method,pass music name and option to program
def main (argv):
    file_type='all'
    author=''
    name=''
    directory='./'
    time_out=0.3
    try:
        opts,args = getopt.getopt (argv, "ha:t:d:o:", ["help","author=","type=","directory=","time out="])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ('-h','--help'):
            usage()
            sys.exit()
        elif opt in ('-d','--directory'):
            directory=arg
        elif opt in ('-a','--author'):
            author = arg
        elif opt in ('-t','--type'):
            file_type = arg
        elif opt in ('-o','--time out'):
            time_out = float(arg)

    if len(args)>0:
        name = args[0]

    if len(args)>1:
        author =args[1]
    yahoo=yahoomp3(time_out,directory)

    if name == '':
        usage()
    elif author == '':
        author=get_author(name)
        yahoo.down_music(name,author,file_type)
    else:
        yahoo.down_music(name,author,file_type)

def usage():
    print 'usage: down_music [option] music_name \noptions:  \n -a --author  author name for search \n -t --type  music type for search \n -d --directory   set the download directory \n '
    pass

if __name__ == "__main__":
    main (sys.argv[1:])

附件:ID3.py downmp3.py

以前写的下载yahoo mp3的python脚本》上有 1 条评论

  1. 博主的代码写的挺好看的,就是现在不能实现功能了,估计是yahoo什么的服务改了,不过博主说的“不知道现在还能不能用”显得有点尴尬吧,您自己试一试不就知道了能不能用。我貌似不能,想改改的,但是水平不够.还希望楼主自己改改,记得改完发我一份啊!我就是靠看这些代码来增加知识的,谢谢。

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>