1# -*- coding: utf-8 -*-
2
3# Copyright(C) 2013 Julien Veyssier
4#
5# This file is part of weboob.
6#
7# weboob is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Lesser General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# weboob is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Lesser General Public License for more details.
16#
17# You should have received a copy of the GNU Lesser General Public License
18# along with weboob. If not, see <http://www.gnu.org/licenses/>.
19
20from __future__ import print_function
21
22from weboob.capabilities.lyrics import CapLyrics
23from weboob.capabilities.base import empty
24from weboob.tools.application.repl import ReplApplication, defaultcount
25from weboob.tools.application.formatters.iformatter import IFormatter, PrettyFormatter
26
27
28__all__ = ['Booblyrics', 'LyricsGetFormatter', 'LyricsListFormatter']
29
30
31class LyricsGetFormatter(IFormatter):
32    MANDATORY_FIELDS = ('id', 'title', 'artist', 'content')
33
34    def format_obj(self, obj, alias):
35        result = u'%s%s%s\n' % (self.BOLD, obj.title, self.NC)
36        result += 'ID: %s\n' % obj.fullid
37        result += 'Title: %s\n' % obj.title
38        result += 'Artist: %s\n' % obj.artist
39        result += '\n%sContent%s\n' % (self.BOLD, self.NC)
40        result += '%s' % obj.content
41        return result
42
43
44class LyricsListFormatter(PrettyFormatter):
45    MANDATORY_FIELDS = ('id', 'title', 'artist')
46
47    def get_title(self, obj):
48        return obj.title
49
50    def get_description(self, obj):
51        artist = u''
52        if not empty(obj.artist):
53            artist = obj.artist
54        return '%s' % artist
55
56
57class Booblyrics(ReplApplication):
58    APPNAME = 'booblyrics'
59    VERSION = '2.0'
60    COPYRIGHT = 'Copyright(C) 2013-YEAR Julien Veyssier'
61    DESCRIPTION = "Console application allowing to search for song lyrics on various websites."
62    SHORT_DESCRIPTION = "search and display song lyrics"
63    CAPS = CapLyrics
64    EXTRA_FORMATTERS = {'lyrics_list': LyricsListFormatter,
65                        'lyrics_get': LyricsGetFormatter,
66                        }
67    COMMANDS_FORMATTERS = {'search':    'lyrics_list',
68                           'get':      'lyrics_get',
69                           }
70    SEARCH_CRITERIAS = ['artist', 'song']
71
72    def complete_get(self, text, line, *ignored):
73        args = line.split(' ')
74        if len(args) == 2:
75            return self._complete_object()
76
77    def do_get(self, id):
78        """
79        get ID
80
81        Display lyrics of the song.
82        """
83
84        songlyrics = self.get_object(id, 'get_lyrics')
85        if not songlyrics:
86            print('Song lyrics not found: %s' % id, file=self.stderr)
87            return 3
88
89        self.start_format()
90        self.format(songlyrics)
91
92    def complete_search(self, text, line, *ignored):
93        args = line.split(' ')
94        if len(args) == 2:
95            return self.SEARCH_CRITERIAS
96
97    @defaultcount(10)
98    def do_search(self, line):
99        """
100        search [artist | song] [PATTERN]
101
102        Search lyrics by artist name or by song title.
103        """
104        criteria, pattern = self.parse_command_args(line, 2, 2)
105        self.change_path([u'search'])
106        if not pattern:
107            pattern = None
108
109        self.start_format(pattern=pattern)
110        for songlyrics in self.do('iter_lyrics', criteria, pattern):
111            self.cached_format(songlyrics)
112