1# Copyright (C) 2014 Rocco Aliberti
2# This program is free software; you can redistribute it and/or modify
3# it under the terms of the GNU General Public License as published by
4# the Free Software Foundation; either version 2, or (at your option)
5# any later version.
6#
7# This program is distributed in the hope that it will be useful,
8# but WITHOUT ANY WARRANTY; without even the implied warranty of
9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10# GNU General Public License for more details.
11#
12# You should have received a copy of the GNU General Public License
13# along with this program; if not, write to the Free Software
14# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15
16try:
17    import lxml.html
18except ImportError:
19    lxml = None
20
21import re
22
23from xl.lyrics import LyricSearchMethod, LyricsNotFoundException
24from xl import common, providers
25
26
27def enable(exaile):
28    """
29    Enables the lyrics mania plugin that fetches track lyrics
30    from lyricsmania.com
31    """
32    if lxml:
33        providers.register('lyrics', LyricsMania(exaile))
34    else:
35        raise NotImplementedError('LXML is not available.')
36        return False
37
38
39def disable(exaile):
40    providers.unregister('lyrics', providers.get_provider('lyrics', 'lyricsmania'))
41
42
43class LyricsMania(LyricSearchMethod):
44
45    name = "lyricsmania"
46    display_name = "Lyrics Mania"
47
48    def __init__(self, exaile):
49        self.user_agent = exaile.get_user_agent_string('lyricsmania')
50
51    def find_lyrics(self, track):
52        try:
53            (artist, title) = (
54                track.get_tag_raw('artist')[0],
55                track.get_tag_raw('title')[0],
56            )
57        except TypeError:
58            raise LyricsNotFoundException
59
60        if not artist or not title:
61            raise LyricsNotFoundException
62
63        artist = artist.replace(' ', '_').replace('\'', '').lower()
64        title = title.replace(' ', '_').replace('\'', '').lower()
65
66        url = 'https://www.lyricsmania.com/%s_lyrics_%s.html' % (title, artist)
67
68        try:
69            html = common.get_url_contents(url, self.user_agent)
70        except Exception:
71            raise LyricsNotFoundException
72
73        try:
74            lyrics_html = lxml.html.fromstring(html)
75        except lxml.etree.XMLSyntaxError:
76            raise LyricsNotFoundException
77
78        try:
79            lyrics_body = lyrics_html.find_class('lyrics-body')[0]
80            lyrics_body.remove(lyrics_body.get_element_by_id('video-musictory'))
81            lyrics = re.sub(r'^\s+Lyrics to .+', '', lyrics_body.text_content())
82            lyrics = lyrics.replace('\t', '')
83            lyrics = self.remove_script(lyrics)
84            lyrics = self.remove_html_tags(lyrics)
85        except Exception:
86            raise LyricsNotFoundException
87
88        # We end up with unicode in some systems, str (bytes) in others;
89        # no idea why and which one is correct.
90        if isinstance(lyrics, bytes):
91            lyrics = lyrics.decode('utf-8', errors='replace')
92        return (lyrics, self.name, url)
93