Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New lyrics plugins #17

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions sonata/info.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

from __future__ import with_statement

import sys
Expand All @@ -7,12 +8,12 @@

import gtk
import pango
import gobject

import ui
import misc
import mpdhelper as mpdh
import consts
import threading
from pluginsystem import pluginsystem


Expand Down Expand Up @@ -386,18 +387,31 @@ def get_lyrics_start(self, search_artist, search_title, filename_artist,
lyrics = lyrics[len(header):]
self._show_lyrics(filename_artist, filename_title, lyrics=lyrics)
else:
# Fetch lyrics from lyricwiki.org etc.
lyrics_fetchers = pluginsystem.get('lyrics_fetching')
callback = lambda * args: self.get_lyrics_response(
filename_artist, filename_title, song_dir, *args)
if lyrics_fetchers:
msg = _("Fetching lyrics...")
for _plugin, cb in lyrics_fetchers:
cb(callback, search_artist, search_title)
else:
msg = _("No lyrics plug-in enabled.")
self._show_lyrics(filename_artist, filename_title,
lyrics=msg)
# Fetch lyrics from plugins.
thread = threading.Thread(target=self.fetch_lyrics_from_plugins,
args=(search_artist, search_title,
song_dir))
thread.start()

def fetch_lyrics_from_plugins(self, search_artist, search_title, song_dir):
lyrics_fetchers = pluginsystem.get('lyrics_fetching')
if lyrics_fetchers:
self._show_lyrics(search_artist, search_title,
lyrics=_("Fetching lyrics..."))
for plugin, get_lyrics in lyrics_fetchers:
lyrics = get_lyrics(search_artist, search_title)
if lyrics:
self.logger.info(_("Lyrics for '") + search_artist + " - " +
search_title + _("' fetched by ") +
plugin.name + _(" plugin."))
self.get_lyrics_response(search_artist, search_title,
song_dir, lyrics=lyrics)
return
msg = _("Lyrics not found.")
else:
msg = _("No lyrics plug-in enabled.")

self._show_lyrics(search_artist, search_title, lyrics=msg)

def get_lyrics_response(self, artist_then, title_then, song_dir,
lyrics=None, error=None):
Expand Down
1 change: 0 additions & 1 deletion sonata/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
import streams
import playlists
import current
import lyricwiki # plug-ins
import rhapsodycovers
import dbus_plugin as dbus

Expand Down
141 changes: 141 additions & 0 deletions sonata/plugins/alltexts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# -*- coding: utf-8 -*-

### BEGIN PLUGIN INFO
# [plugin]
# plugin_format: 0, 0
# name: AllTexts
# version: 0, 1, 0
# description: Fetch lyrics from alltexts.ru
# author: Anton Lashkov
# author_email: Lenton_91@mail.ru
# url:
# license: GPL v3 or later
# [capabilities]
# enablables: on_enable
# lyrics_fetching: get_lyrics
### END PLUGIN INFO

import re
import urllib

alltexts = None

class AllTexts():
def __init__(self):
pass

def format(self, string):
#It should make "when_you_don't_control_your_government_people_want_to_kill_you"
#from "When You Don't Control Your Government, People Want to Kill You"
#and translite cyrilic letters.

string = self.cyr2lat(string)
string = re.sub(r"[\,\?\!\:\;]",'',string)
string = string.replace(' ','_')
string = str(string).lower()

return string

def cyr2lat(self, s):
conversion = {
'а' : 'a',
'б' : 'b',
'в' : 'v',
'г' : 'g',
'д' : 'd',
'е' : 'e',
'ё' : 'jo',
'ж' : 'zh',
'з' : 'z',
'и' : 'i',
'й' : 'j',
'к' : 'k',
'л' : 'l',
'м' : 'm',
'н' : 'n',
'о' : 'o',
'п' : 'p',
'р' : 'r',
'с' : 's',
'т' : 't',
'у' : 'u',
'ф' : 'f',
'х' : 'h',
'ц' : 'c',
'ч' : 'ch',
'ш' : 'sh',
'щ' : 'sch',
'ь' : "",
'ы' : 'y',
'ъ' : "",
'э' : 'e',
'ю' : 'ju',
'я' : 'ja',
'А' : 'A',
'Б' : 'B',
'В' : 'V',
'Г' : 'G',
'Д' : 'D',
'Е' : 'E',
'Ё' : 'J',
'Ж' : 'ZH',
'З' : 'Z',
'И' : 'I',
'Й' : 'JO',
'К' : 'K',
'Л' : 'L',
'М' : 'M',
'Н' : 'N',
'О' : 'O',
'П' : 'P',
'Р' : 'R',
'С' : 'S',
'Т' : 'T',
'У' : 'U',
'Ф' : 'F',
'Х' : 'H',
'Ц' : 'C',
'Ч' : 'CH',
'Ш' : 'SH',
'Щ' : 'SCH',
'Ъ' : "",
'Ы' : 'Y',
'Ь' : "",
'Э' : 'E',
'Ю' : 'JU',
'Я' : 'JA',
}
for c in conversion:
try:
s = s.replace(c,conversion[c])
except KeyError:
pass
return s

def get_lyrics(self, search_artist, search_title):

addr = "http://alltexts.ru/text/%s/%s.php" % (self.format(search_artist), self.format(search_title))
try:
page = urllib.urlopen(addr).read()
lyrics = page.split('<pre class="text">')[1].split("</pre>")[0].strip()
lyrics = lyrics.decode('cp1251').encode('utf8')
lyrics = lyrics.replace("\n\n","\n")
if lyrics == "":
return None
except Exception:
return None

return lyrics

def on_enable(state):

global alltexts

if state:
if alltexts is None:
alltexts = AllTexts()

def get_lyrics(artist, title):

return alltexts.get_lyrics(artist, title)

47 changes: 47 additions & 0 deletions sonata/plugins/azlyrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

### BEGIN PLUGIN INFO
# [plugin]
# plugin_format: 0, 0
# name: AZLyrics
# version: 0, 2, 0
# description: Fetch lyrics from AZLyrics.com.
# author: Anton Lashkov
# author_email: Lenton_91@mail.ru
# url:
# license: GPL v3 or later
# [capabilities]
# lyrics_fetching: get_lyrics
### END PLUGIN INFO

import re
import urllib

def clear(string):
#It should make "whenyoudontcontrolyourgovernmentpeoplewanttokillyou"
#from "When You Don't Control Your Government, People Want to Kill You"

string = str(string).lower()
string = re.sub(r"[\'\.\,\-\?\!\:\;\$\(\)\ ]",'',string)

return string

def get_lyrics(search_artist, search_title):
addr = "http://www.azlyrics.com/lyrics/%s/%s.html" % (clear(search_artist),
clear(search_title))

try:
page = urllib.urlopen(addr).read()
lyrics = page.split("<!-- start of lyrics -->")[1].\
split("<!-- end of lyrics -->")[0].strip()
lyrics = lyrics.replace("<br />","")

if lyrics == "":
return None
else:
return lyrics

except:
return None

if __name__ == "__main__":
print get_lyrics("anti-flag", "Death Of A Nation")
48 changes: 48 additions & 0 deletions sonata/plugins/lyricstime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

### BEGIN PLUGIN INFO
# [plugin]
# plugin_format: 0, 0
# name: Lyrics.time
# version: 0, 2, 0
# description: Fetch lyrics from lyricstime.com.
# author: Anton Lashkov
# author_email: Lenton_91@mail.ru
# url:
# license: GPL v3 or later
# [capabilities]
# lyrics_fetching: get_lyrics
### END PLUGIN INFO

import re
import urllib

def clear(string):
#It should make "when-you-don-t-control-your-government-people-want-to-kill-you"
#from "When You Don't Control Your Government, People Want to Kill You"

string = str(string).lower()
string = re.sub(r"[\'\ ]",'-',string)
string = re.sub(r"[\.\,\?\!\:\;\$\(\)]",'',string)

return string

def get_lyrics(search_artist, search_title):
addr = "http://www.lyricstime.com/%s-%s-lyrics.html" % (
clear(search_artist), clear(search_title))

try:
page = urllib.urlopen(addr).read()
lyrics = page.split('<div id="songlyrics" >')[1].\
split('</div>')[0].strip()
lyrics = lyrics.replace("<br />","").replace("<p>","").replace("</p>","")

if lyrics == "":
return None
else:
return lyrics

except:
return None

if __name__ == "__main__":
print get_lyrics("anti-flag", "Death Of A Nation")
40 changes: 40 additions & 0 deletions sonata/plugins/lyricwiki.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

### BEGIN PLUGIN INFO
# [plugin]
# plugin_format: 0, 0
# name: LyricWiki
# version: 0, 2, 0
# description: Fetch lyrics from lyrics.wikia.com.
# author: Anton Lashkov
# author_email: Lenton_91@mail.ru
# url:
# license: GPL v3 or later
# [capabilities]
# lyrics_fetching: get_lyrics
### END PLUGIN INFO

import urllib

def lyricwiki_format(text):
return urllib.quote(str(unicode(text).title()))

def get_lyrics(search_artist, search_title):
addr = 'http://lyrics.wikia.com/index.php?title=%s:%s&action=edit' % (
lyricwiki_format(search_artist), lyricwiki_format(search_title))

try:
content = urllib.urlopen(addr).read()
lyrics = content.split("&lt;lyrics>")[1].split("&lt;/lyrics>")[0].strip()

if lyrics != \
("&lt;!-- PUT LYRICS HERE (and delete this entire line) -->") \
and lyrics != "":
return lyrics.decode("utf-8")
else:
return None

except Exception:
return None

if __name__ == "__main__":
print get_lyrics("anti-flag", "Death Of A Nation")
Loading