Skip to content

Commit

Permalink
Formatting changeFormatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
timoloewe committed Feb 4, 2016
1 parent 9e35cc0 commit 085f39e
Show file tree
Hide file tree
Showing 15 changed files with 584 additions and 663 deletions.
23 changes: 11 additions & 12 deletions lLyrics/AZLyricsParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

import Util


class Parser(object):

def __init__(self, artist, title):
self.artist = artist
self.title = title
self.lyrics = ""

def parse(self):
# remove unwanted characters from artist and title strings
clean_artist = self.artist
Expand All @@ -33,11 +33,11 @@ def parse(self):
clean_artist = clean_artist.replace(" and ", "")
clean_artist = clean_artist.replace(" ", "")
clean_artist = Util.remove_punctuation(clean_artist)

clean_title = self.title
clean_title = clean_title.replace(" ", "")
clean_title = Util.remove_punctuation(clean_title)

# create lyrics Url
url = "http://www.azlyrics.com/lyrics/" + clean_artist + "/" + clean_title + ".html"
print("azlyrics Url " + url)
Expand All @@ -46,30 +46,29 @@ def parse(self):
except:
print("could not connect to azlyrics.com")
return ""

resp = Util.bytes_to_string(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("that. -->")
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>", "")
resp = resp.replace("<i>", "")
resp = resp.replace("</i>", "")

return resp

20 changes: 10 additions & 10 deletions lLyrics/ChartlyricsParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@

import Util


class Parser(HTMLParser):

def __init__(self, artist, title):
HTMLParser.__init__(self)
self.artist = artist
self.title = title
self.tag = None
self.correct = True
self.lyrics = ""

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

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

# definde handler for parsing
def handle_data(self, data):
if self.tag == "lyricsong":
Expand All @@ -54,20 +54,20 @@ def handle_data(self, data):
return
if self.correct and self.tag == "lyric":
self.lyrics = 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)
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)
try:
resp = urllib.request.urlopen(url, None, 3).read()
except:
print("could not connect to chartlyric.com API")

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

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

return self.lyrics

Loading

0 comments on commit 085f39e

Please sign in to comment.