Created
December 1, 2017 11:06
-
-
Save iwouldnot/45ec7af75be26150c08a40fad6866912 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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