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

Fixed the parsers that weren't working #81

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ It is intended as a replacement of the built-in lyrics plugin of Rhythmbox with
Lyrics sources
---------------

- Lyricwiki.org
- Letras.terra.com.br
- Letras.mus.br
- Vagalume.com.br
- Metrolyrics.com
- AZLyrics.com
- Lyricsnmusic.com
- Lyricsmania.com
- Genius.com
- Darklyrics.com
Expand Down
12 changes: 9 additions & 3 deletions lLyrics/AZLyricsParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ def parse(self):
clean_title = clean_title.lower()

# create lyrics Url
url = "http://www.azlyrics.com/lyrics/" + clean_artist + "/" + clean_title + ".html"
url = (
"https://www.azlyrics.com/lyrics/"
+ clean_artist
+ "/"
+ clean_title
+ ".html"
)
print("azlyrics Url " + url)
try:
resp = urllib.request.urlopen(url, None, 3).read()
Expand All @@ -60,12 +66,12 @@ def get_lyrics(self, resp):
if start == -1:
print("lyrics start not found")
return ""
resp = resp[(start + 9):]
resp = resp[(start + 9) :]
end = resp.find("</div>")
if end == -1:
print("lyrics end not found ")
return ""
resp = resp[:(end - 1)]
resp = resp[: (end - 1)]

# replace unwanted parts
resp = resp.replace("<br>", "")
Expand Down
37 changes: 28 additions & 9 deletions lLyrics/ChartlyricsParser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Parser for Chartlyrics.com API
#
# Chartlyrics API seems to have problems with multiple consecutive requests
# (it apparently requires a 20-30sec interval between two API-calls),
# (it apparently requires a 20-30sec interval between two API-calls),
# so just use SearchLyricDirect since it only needs one API request.

# This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -34,15 +34,15 @@ def __init__(self, artist, title):
self.correct = True
self.lyrics = ""

# define handler for parsing
# define handler for parsing
def handle_starttag(self, tag, attrs):
self.tag = tag

# definde handler for parsing
# definde handler for parsing
def handle_endtag(self, tag):
self.tag = None

# definde handler for parsing
# definde handler for parsing
def handle_data(self, data):
if self.tag == "lyricsong":
if data.lower() != self.title:
Expand All @@ -57,17 +57,36 @@ def handle_data(self, data):

def parse(self):
# API searchLyric request
url = "http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=" + urllib.parse.quote(
self.artist) + "&song=" + urllib.parse.quote(self.title)
print("call chartlyrics API: " + url)
url = (
"http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist="
+ urllib.parse.quote(self.artist)
+ "&song="
+ urllib.parse.quote(self.title)
)
print("chartlyrics Url " + url)
try:
resp = urllib.request.urlopen(url, None, 3).read()
except:
print("could not connect to chartlyric.com API")
return ""

resp = Util.bytes_to_string(resp)
self.feed(resp)

self.lyrics = self.get_lyrics(resp)
self.lyrics = string.capwords(self.lyrics, "\n").strip()

return self.lyrics

def get_lyrics(self, resp):
# cut HTML source to relevant part
start = resp.find("<Lyric>")
if start == -1:
print("lyrics start not found")
return ""
resp = resp[(start + 7) :]
end = resp.find("</Lyric>")
if end == -1:
print("lyrics end not found ")
return ""
resp = resp[:end]

return resp
77 changes: 63 additions & 14 deletions lLyrics/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import lLyrics
import lLyrics_rb3compat as Compat

DCONF_DIR = 'org.gnome.rhythmbox.plugins.llyrics'
DCONF_DIR = "org.gnome.rhythmbox.plugins.llyrics"


class Config(object):
Expand Down Expand Up @@ -121,9 +121,12 @@ def get_left_sidebar(self):
def get_hide_label(self):
return self.settings["hide-label"]

def get_line_spacing(self):
return self.settings["line-spacing"]


class ConfigDialog(GObject.Object, PeasGtk.Configurable):
__gtype_name__ = 'lLyricsConfigDialog'
__gtype_name__ = "lLyricsConfigDialog"
object = GObject.property(type=GObject.Object)

def __init__(self):
Expand All @@ -132,7 +135,7 @@ def __init__(self):

def do_create_configure_widget(self):
# init locales
gettext.install('lLyrics', os.path.dirname(__file__) + "/locale/")
gettext.install("lLyrics", os.path.dirname(__file__) + "/locale/")

# page 1 for general settings
page1 = Gtk.VBox()
Expand Down Expand Up @@ -160,7 +163,12 @@ def do_create_configure_widget(self):
label.set_use_markup(True)

descr = Gtk.Label(
"<i>" + _("Whether to automatically save retrieved lyrics in the folder specified below") + "</i>")
"<i>"
+ _(
"Whether to automatically save retrieved lyrics in the folder specified below"
)
+ "</i>"
)
descr.set_alignment(0, 0)
descr.set_margin_left(15)
descr.set_line_wrap(True)
Expand Down Expand Up @@ -200,8 +208,14 @@ def do_create_configure_widget(self):
label = Gtk.Label("<b>" + _("Always ignore parentheses in song title") + "</b>")
label.set_use_markup(True)

descr = Gtk.Label("<i>" + _("When turned off, only parentheses containing specific strings "
"(e.g. 'remix', 'live', 'edit', etc) are filtered") + "</i>")
descr = Gtk.Label(
"<i>"
+ _(
"When turned off, only parentheses containing specific strings "
"(e.g. 'remix', 'live', 'edit', etc) are filtered"
)
+ "</i>"
)
descr.set_alignment(0, 0)
descr.set_margin_left(15)
descr.set_line_wrap(True)
Expand All @@ -215,6 +229,28 @@ def do_create_configure_widget(self):

page1.pack_start(vbox, False, False, 10)

# line spacing
hbox = Gtk.HBox()
switch = Gtk.Switch()
switch.set_active(self.settings["line-spacing"])
switch.connect("notify::active", self.switch_toggled, "line-spacing")

label = Gtk.Label("<b>" + _("Line spacing ") + "</b>")
label.set_use_markup(True)

descr = Gtk.Label("<i>" + _("Spacing between lines in lyrics") + "</i>")
descr.set_alignment(0, 0)
descr.set_margin_left(15)
descr.set_line_wrap(True)
descr.set_use_markup(True)

hbox.pack_start(label, False, False, 5)
hbox.pack_start(switch, False, False, 5)
vbox = Gtk.VBox()
vbox.pack_start(hbox, False, False, 0)
vbox.pack_start(descr, False, False, 0)
page1.pack_start(vbox, False, False, 10)

# page 2 for sources settings
page2 = Gtk.VBox()

Expand All @@ -235,16 +271,21 @@ def do_create_configure_widget(self):
check.connect("toggled", self.source_toggled, source)
hbox.pack_start(check, True, True, 3)

button_up = Gtk.Button('\u2191')
button_up = Gtk.Button("\u2191")
button_up.connect("clicked", self.reorder_sources, source, hbox, vbox, "up")
hbox.pack_start(button_up, False, False, 3)
if self.settings["scanning-order"].index(source) == 0:
button_up.set_sensitive(False)

button_down = Gtk.Button('\u2193')
button_down.connect("clicked", self.reorder_sources, source, hbox, vbox, "down")
button_down = Gtk.Button("\u2193")
button_down.connect(
"clicked", self.reorder_sources, source, hbox, vbox, "down"
)
hbox.pack_start(button_down, False, False, 3)
if self.settings["scanning-order"].index(source) == len(self.settings["scanning-order"]) - 1:
if (
self.settings["scanning-order"].index(source)
== len(self.settings["scanning-order"]) - 1
):
button_down.set_sensitive(False)

vbox.pack_start(hbox, False, False, 0)
Expand Down Expand Up @@ -278,11 +319,19 @@ def do_create_configure_widget(self):
switch.set_active(self.settings["left-sidebar"])
switch.connect("notify::active", self.switch_toggled, "left-sidebar")

label = Gtk.Label("<b>" + _("Show lyrics in left sidebar instead of right one") + "</b>")
label = Gtk.Label(
"<b>" + _("Show lyrics in left sidebar instead of right one") + "</b>"
)
label.set_use_markup(True)

descr = Gtk.Label("<i>" + _("You have to disable and re-enable this plugin or restart Rhythmbox "
"to apply changes here") + "</i>")
descr = Gtk.Label(
"<i>"
+ _(
"You have to disable and re-enable this plugin or restart Rhythmbox "
"to apply changes here"
)
+ "</i>"
)
descr.set_alignment(0, 0)
descr.set_margin_left(15)
descr.set_line_wrap(True)
Expand All @@ -296,7 +345,7 @@ def do_create_configure_widget(self):

page3.pack_start(vbox, False, False, 10)

# create a notebook as top level container
# create a notebook as top level container
nb = Gtk.Notebook()
nb.append_page(page1, Gtk.Label(_("General")))
nb.append_page(page2, Gtk.Label(_("Sources")))
Expand Down
25 changes: 21 additions & 4 deletions lLyrics/DarklyricsParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ def parse(self):
clean_artist = clean_artist.replace(" ", "")

# create artist Url
url = "http://www.darklyrics.com/" + clean_artist[:1] + "/" + clean_artist + ".html"
url = (
"http://www.darklyrics.com/"
+ clean_artist[:1]
+ "/"
+ clean_artist
+ ".html"
)
print("darklyrics artist Url " + url)
try:
resp = urllib.request.urlopen(url, None, 3).read()
Expand All @@ -44,7 +50,9 @@ def parse(self):
resp = Util.bytes_to_string(resp)

# find title with lyrics url
match = re.search("<a href=\"\.\.(.*?)\">" + self.title + "</a><br />", resp, re.I)
match = re.search(
'<a href="\.\.(.*?)">' + self.title + "</a><br />", resp, re.I
)
if match is None:
print("could not find title")
return ""
Expand All @@ -67,8 +75,17 @@ def parse(self):

def get_lyrics(self, resp):
# search for the relevant lyrics
match = re.search("<h3><a name=\"" + self.track_no + "\">" + self.track_no + "\. " + self.title + "</a></h3>",
resp, re.I)
match = re.search(
'<h3><a name="'
+ self.track_no
+ '">'
+ self.track_no
+ "\. "
+ self.title
+ "</a></h3>",
resp,
re.I,
)
if match is None:
print("lyrics start not found")
return ""
Expand Down
Loading