1try: 2 from bs4 import BeautifulSoup 3except ImportError: 4 BeautifulSoup = None 5import re 6import urllib.parse 7 8from xl.lyrics import LyricSearchMethod, LyricsNotFoundException 9from xl import common, providers 10 11 12def enable(exaile): 13 """ 14 Enables the lyric wiki plugin that fetches track lyrics 15 from lyrics.wikia.com 16 """ 17 if BeautifulSoup: 18 providers.register('lyrics', LyricWiki(exaile)) 19 else: 20 raise NotImplementedError('BeautifulSoup is not available.') 21 return False 22 23 24def disable(exaile): 25 providers.unregister('lyrics', providers.get_provider('lyrics', 'lyricwiki')) 26 27 28class LyricWiki(LyricSearchMethod): 29 30 name = "lyricwiki" 31 display_name = "Lyric Wiki" 32 33 def __init__(self, exaile): 34 self.user_agent = exaile.get_user_agent_string('lyricwiki') 35 36 def find_lyrics(self, track): 37 try: 38 (artist, title) = ( 39 track.get_tag_raw('artist')[0], 40 track.get_tag_raw('title')[0], 41 ) 42 except TypeError: 43 raise LyricsNotFoundException 44 45 if not artist or not title: 46 raise LyricsNotFoundException 47 48 artist = urllib.parse.quote(artist.replace(' ', '_')) 49 title = urllib.parse.quote(title.replace(' ', '_')) 50 51 url = 'https://lyrics.fandom.com/wiki/%s:%s' % (artist, title) 52 53 try: 54 source = common.get_url_contents(url, self.user_agent) 55 except Exception: 56 raise LyricsNotFoundException 57 58 soup = BeautifulSoup(source, "lxml") 59 lyrics = soup.findAll(attrs={"class": "lyricbox"}) 60 if lyrics: 61 with_div = ( 62 lyrics[0].renderContents().decode('utf-8').replace('<br />', '\n') 63 ) 64 string = '\n'.join( 65 self.remove_div(with_div).replace('\n\n\n', '').split('\n') 66 ) 67 lyrics = re.sub(r' Send.*?Ringtone to your Cell ', '', string) 68 else: 69 raise LyricsNotFoundException 70 71 lyrics = self.remove_script(lyrics) 72 lyrics = self.remove_html_tags(str(BeautifulSoup(lyrics, "lxml"))) 73 74 return (lyrics, self.name, url) 75