VPlayer / lyrics.py

# -*- coding: utf-8 -*-
import re
import urllib

from PyQt4 import QtCore

class Lyrics(QtCore.QThread):
    def __init__(self, track):
        QtCore.QThread.__init__(self)
        self.track = track

    def run(self):
        lyrics = self.get_lyrics()
        self.emit(QtCore.SIGNAL('results'), lyrics)

    def get_lyrics(self, track = None):
        if track is None:
            track = self.track
        url = 'http://lyricwiki.org/api.php'
        vars = {}
        vars['func'] = 'getSong'
        vars['artist'] =  unicode(track.artist).encode('utf-8')
        vars['song'] =  unicode(track.title).encode('utf-8')
        vars['fmt'] = 'text'
        url = url + '?' + urllib.urlencode(vars)
        try:
            self.lyrics = unicode(urllib.urlopen(url).read(), 'utf-8')
        except:
            return self.tr("Could not receive track lyrics :(")

        url = self.lyrics
        print url
        page = urllib.urlopen(url).read()
        res = re.findall('lyricbox\' >(.*)<!--', page)
        if len(res) == 0: return self.tr("Could not receive track lyrics :(")
        else:
            self.lyrics = unicode(res[0].replace('<br />', '\n'), 'utf-8', 'replace')

        return self.lyrics
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.