免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2924 | 回复: 0
打印 上一主题 下一主题

linux自动下载歌词的python代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-21 21:27 |只看该作者 |倒序浏览


       
        文件:TTDownLoadLyric.py.tar.gz
        大小:2KB
        下载:
下载
       
                                闲得无聊,想做一个linux下播放器,命令行的模式的,可以像千千那样自动下载歌词,想法挺好,就是没实现出来,只做了歌词部分功能,等毕业后再说吧,校园网访问资源费劲。
代码访问千千静听服务器下载歌词,参考一下文章
http://www.cnblogs.com/5yplan/archive/2009/01/15/1376265.html
上面有个c#代码的
使用了一个人用python写好的库,用了adapter模式,给自己定制了功能,做了一个小测试,在linux调用mplayer放mp3,呵呵,相当白痴的功能,我等着会用gstreamer或者其他什么的再说
下面是读mp3的id3v1标签代码
import sys
import os
import codecs
import locale
class ID3V1Node:
    def __init__(self, title, artist,album):
        self.title = title
        self.artist = artist
        self.album = album
    def print_me(self):
        print self.title
        print self.artist
        print self.album
   
    def get_title(self):
        return self.title
    def get_artist(self):
        return self.artist
class ID3V1:
    def __init__(self, filename):
        self.filename = filename
    def get_tag(self):
        try:
            f = file(self.filename)
        except IOError:
            print 'not find',self.filename
            return None
        f.seek(-128,os.SEEK_END)
        s = f.read(128)
        if s[0:3] != 'TAG':
            print 'not a legal id3v1 tag'
            return None
        f.close()
        
        s = s[3:len(s)]
        title = s[0:30]
        s = s[30:len(s)]
        artist = s[0:30]
        s = s[30:len(s)]
        album = s[0:30]
        node = ID3V1Node(title,artist,album)
        #node.print_me()
        return node
if __name__ == '__main__':
    sys_code = locale.getdefaultlocale()[1]
    tag = ID3V1('shuimunianhua.mp3')
    node = tag.get_tag()
下面是从千千服务器下载歌词的代码
import TTDownLoadLyric
import locale
import codecs
import sys
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement
from xml.etree.ElementTree import dump
from xml.etree.ElementTree import Comment
from xml.etree.ElementTree import tostring
class LyricNode:
    def __init__(self, id, artist, title):
        self.id =id
        self.artist = artist
        self.title = title
    def printit(self):
        print 'id:',self.id,'artist:',self.artist,'title:',self.title
class TTLyric:
    def __init__(self,artist,title, sys_code):
        self.artist = artist
        self.title = title
        self.lrc_code ='utf-8'
        self.sys_code = sys_code
        self.lrc_proxy = TTDownLoadLyric
        self.lyric_list = []
    def search_lyric(self):
        xml = self.lrc_proxy.SearchLyric(self.artist, self.title)
        #print xml.decode(self.lyr_code)
        f = file('tmp.xml','w')
        f.write(xml)
        f.close()
        guide_xml = ElementTree(file = 'tmp.xml').getroot()
        lrc_list = guide_xml.findall('lrc')
        for item in lrc_list:
            title = item.get('title')
            artist = item.get('artist')
            id = item.get('id')
            node = LyricNode(id, artist, title)
            self.lyric_list.append(node)
        return len(self.lyric_list)
    def get_lyric_list(self):
        return self.lyric_list
    def download_lyric(self,index):
        if index >= len(self.lyric_list):
            print 'out of range of lyric set'
            return -1
        item = self.lyric_list[index]
        lrctxt = self.lrc_proxy.DownLoadLyric(item.id, item.artist, item.title)
        print lrctxt.decode(self.lrc_code)
        lrc_file = item.artist+'-'+item.title+'.'+'lrc'
        f = file(lrc_file, 'w')
        f.write(unicode(lrctxt,self.lrc_code).encode(self.sys_code))
        f.close()
    def print_lyric_list(self):
        for i in range(0,len(self.lyric_list)):
            print i,
            self.lyric_list.printit()
if __name__ == '__main__':
    default_code = locale.getdefaultlocale()[1]
    artist = sys.argv[1].decode(default_code)
    title = sys.argv[2].decode(default_code)
    lrc = TTLyric(artist,title,default_code)
    lrc.search_lyric()
    lrc.print_lyric_list()
    lrc.download_lyric(0)
最后是测试代码
#!/usr/bin/python
import sys
import lyric
import idv
import os
import threading
import time
class MPlayer(threading.Thread):
    def __init__(self, mp3_name):
        threading.Thread.__init__(self, name = mp3_name)
        self.mp3_name = mp3_name
    def run(self):
        os.system('mplayer '+self.mp3_name)
mp3_file =sys.argv[1]
tag = idv.ID3V1(mp3_file)
node = tag.get_tag()
if node == None:
    print 'error'
    exit()
title = node.get_title().decode('gbk')
artist = node.get_artist().decode('gbk')
lrc = lyric.TTLyric(artist, title,'gbk')
lrc.search_lyric()
mplayer = MPlayer(mp3_file)
#mplayer.setDaemon(1)
mplayer.start()
time.sleep(2)
lrc.download_lyric(0)
#while True:
#   time.sleep(1)
附件是一个访问千千服务的代码,不是我写的呵呵
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/43391/showart_1871102.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP