summaryrefslogtreecommitdiff
path: root/plugins/Lyrics.py
blob: f0efb55397fcfc69244b3cbccb23462e3198a828 (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
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):
    " contains the lyrics"
    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()))
        if lyrics:
            self.txtView.insertPlainText(lyrics)

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

    def __init__(self, winMain):
        Plugin.__init__(self, winMain, 'Lyrics')
        self.addListener('onSongChange', self.refresh)
        self.addListener('onReady', self.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 _getDockWidget(self):
        return self._createDock(self.o)

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

    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 result.Return.Lyrics