summaryrefslogtreecommitdiff
path: root/nephilim/plugins/Lyrics.py
blob: 2bfbb110d917028e9333cd4b65aba72eb5ccae35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from PyQt4 import QtGui,QtCore
from PyQt4.QtCore import QVariant

from thread import start_new_thread
import logging

from ..clPlugin import Plugin
import LyricWiki_client

class wgLyrics(QtGui.QWidget):
    txtView     = None  # text-object
    p           = None  # plugin
    def __init__(self, p, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.p = p
        self.curLyrics = ''

        self.txtView = QtGui.QTextEdit(self)
        self.txtView.setReadOnly(True)

        self.setLayout(QtGui.QVBoxLayout())
        self.layout().setSpacing(0)
        self.layout().setMargin(0)
        self.layout().addWidget(self.txtView)

    def set_lyrics(self, song, lyrics):
        self.txtView.clear()

        if song:
            self.txtView.insertHtml('<b>%s</b>\n<br /><u>%s</u><br />'\
                    '<br />\n\n'%(song.getTitle(), song.getArtist()))
        else:
            return

        if lyrics:
            self.txtView.insertPlainText(lyrics)
        else:
            self.txtView.insertPlainText('Lyrics not found.')

class Lyrics(Plugin):
    o        = None
    DEFAULTS  = {'sites' : ['lyricwiki'], 'lyricdir' : '$musicdir/$songdir',
                 'lyricname' : '.lyric_mpclient_$artist_$album_$song'}
    LISTENERS = {'onSongChange' : 'refresh', 'onReady' : 'refresh'}

    def _load(self):
        self.o = wgLyrics(self)
    def _unload(self):
        self.o = None
    def getInfo(self):
        return "Show (and fetch) the lyrics of the currently playing song."

    def _get_dock_widget(self):
        return self._create_dock(self.o)

    def refresh(self, params = None):
        lyrics = None
        song   = self.mpclient().current_song()
        if not song:
            self.o.set_lyrics(None, None)
            return
        for site in self.settings().value(self.name() + '/sites').toStringList():
            lyrics = eval('self.fetch_%s(song)'%site)
            if lyrics:
                self.o.set_lyrics(song, lyrics)
                return

        self.o.set_lyrics(song, None)

    def fetch_lyricwiki(self, song):
        soap = LyricWiki_client.LyricWikiBindingSOAP("http://lyricwiki.org/server.php")
        req  = LyricWiki_client.getSongRequest()
        req.Artist = song.getArtist()
        req.Song   = song.getTitle()
        result     = soap.getSong(req).Return.Lyrics.decode('utf-8').encode('iso8859').decode('utf-8')

        return result if result != 'Not found' else None