1# -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
2#
3# Copyright (C) 2012 He Jian <hejian.he@gmail.com>
4#
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)
8# any later version.
9#
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.
17#
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.
22#
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.
26
27import rb
28import urllib.parse
29import re
30
31class JlyricParser (object):
32	def __init__ (self, artist, title):
33		self.artist = artist
34		self.title = title
35
36	def search (self, callback, *data):
37		artist = urllib.parse.quote_plus(self.artist)
38		title = urllib.parse.quote_plus(self.title)
39		url = 'http://j-lyric.net/index.php?kt=%s&ka=%s' % (title, artist)
40		loader = rb.Loader()
41		loader.get_url (url, self.got_results, callback, *data)
42
43	def got_results (self, result, callback, *data):
44		if result is None:
45			callback (None, *data)
46			return
47
48		result = result.decode('utf-8')
49		m = re.search('<div class=\'title\'><a href=\'(/artist/[^\.]*\.html)\'>', result)
50		if m is None:
51			callback (None, *data)
52			return
53
54		loader = rb.Loader()
55		loader.get_url ('http://j-lyric.net' + m.group(1), self.parse_lyrics, callback, *data)
56
57	def parse_lyrics (self, result, callback, *data):
58		if result is None:
59			callback (None, *data)
60			return
61
62		result = result.decode('utf-8')
63		lyrics = re.split ('<p id=\'lyricBody\'>', result)[1]
64		lyrics = re.split ('</p>', lyrics)[0]
65
66		lyrics = re.sub('<br />', '', lyrics)
67		lyrics = self.title + "\n\n" + lyrics
68		lyrics += "\n\nLyrics provided by j-lyric.net"
69
70		callback (lyrics, *data)
71