1
# -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
3
# Copyright (C) 2007 Austin <austiny@sohu.com>
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2, or (at your option)
10
# The Rhythmbox authors hereby grant permission for non-GPL compatible
11
# GStreamer plugins to be used and distributed together with GStreamer
12
# and Rhythmbox. This permission is above and beyond the permissions granted
13
# by the GPL license by which Rhythmbox is covered. If you modify this code
14
# you may extend this exception to your version of the code, but you are not
15
# obligated to do so. If you do not wish to do so, delete this exception
16
# statement from your version.
18
# This program is distributed in the hope that it will be useful,
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
# GNU General Public License for more details.
23
# You should have received a copy of the GNU General Public License
24
# along with this program; if not, write to the Free Software
25
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
32
from xml.dom import minidom
34
def detect_charset(s):
35
charsets = ('iso-8859-1', 'gbk', 'utf-8')
36
for charset in charsets:
38
return unicode(unicode(s, 'utf-8').encode(charset), 'gbk')
43
class WinampcnParser(object):
44
def __init__(self, artist, title):
48
def search(self, callback, *data):
50
# encode search string
51
title_encode = urllib.quote(detect_charset(self.title).encode('gbk').replace(' ', ''))
52
artist_encode = urllib.quote(detect_charset(self.artist).encode('gbk').replace(' ',''))
53
url = 'http://www.winampcn.com/lyrictransfer/get.aspx?song=%s&artist=%s&lsong=%s&Datetime=20060601' % (title_encode, artist_encode, title_encode)
56
loader.get_url (url, self.got_lyrics, callback, *data)
58
def got_lyrics(self, xmltext, callback, *data):
59
# retrieve xml content
61
callback (None, *data)
65
xmltext = xmltext.decode('gbk').encode('UTF-8')
66
xmltext = xmltext.replace('encoding="gb2312"', 'encoding="UTF-8"')
67
xmldoc = minidom.parseString(xmltext)
68
root = xmldoc.documentElement
70
lrcurl = root.getElementsByTagName('LyricUrl')[0].childNodes[0].data
72
callback (xmltext, *data)
75
# download the lyrics file
76
lrcurl_encode = urllib.quote(detect_charset(lrcurl).encode('gbk'))
77
lrcurl_encode = lrcurl_encode.replace('%3A', ':');
80
loader.get_url (lrcurl_encode, self.parse_lyrics, callback, *data)
82
callback (None, *data)
85
def parse_lyrics(self, lyrics, callback, *data):
88
callback (None, *data)
91
# transform it into plain text
94
lrcplaintext = re.sub('\[.*?\]', '', lrcplaintext)
95
lrcplaintext = lrcplaintext.decode('gbk').encode('UTF-8')
97
callback (lrcplaintext, *data)
101
lrcplaintext += "\n\nLyrics provided by winampcn.com"
102
callback(lrcplaintext, *data)