Skip to content

Instantly share code, notes, and snippets.

@iwouldnot
Created December 1, 2017 11:06
Show Gist options
  • Save iwouldnot/45ec7af75be26150c08a40fad6866912 to your computer and use it in GitHub Desktop.
Save iwouldnot/45ec7af75be26150c08a40fad6866912 to your computer and use it in GitHub Desktop.
class Song(object):
def __init__(self, artist, title):
self.artist = self.__format_str(artist)
self.title = self.__format_str(title)
self.url = None
self.lyric = None
def __format_str(self, s):
# убираем пробельчики в начале
s = s.strip()
try:
# нормализуем к юникодовским символам в строку (декомпозированная форма)
s = ''.join(c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn')
except:
pass
s = s.title()
return s
def __quote(self, s):
return urllib.parse.quote(s.replace(' ', '_'))
def __make_url(self):
artist = self.__quote(self.artist)
title = self.__quote(self.title)
artist_title = '{}:{}'.format(artist, title)
url = 'http://lyrics.wikia.com/' + artist_title
self.url = url
def update(self, artist=None, title=None):
if artist:
self.artist = self.__format_str(artist)
if title:
self.title = self.__format_str(title)
def lyricwikia(self):
self.__make_url()
try:
doc = lxml.html.parse(self.url)
lyricbox = doc.getroot().cssselect('.lyricbox')[0]
except (IOError, IndexError) as e:
self.lyric = ''
return self.lyric
lyrics = []
for node in lyricbox:
if node.tag == 'br':
lyrics.append('\n')
if node.tail is not None:
lyrics.append(node.tail)
self.lyric = "".join(lyrics).strip()
return self.lyric
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment