]> kaliko git repositories - mpd-sima.git/blob - sima/utils/blcli.py
Set max_art default value to 20
[mpd-sima.git] / sima / utils / blcli.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2021 kaliko <kaliko@azylum.org>
3 #
4 #  This file is part of sima
5 #
6 #  sima is free software: you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation, either version 3 of the License, or
9 #  (at your option) any later version.
10 #
11 #  sima is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with sima.  If not, see <http://www.gnu.org/licenses/>.
18
19 # standard library import
20 import atexit
21 import sys
22
23 # local import
24 from ..mpdclient import MPD, Artist, Album
25 from ..lib.simadb import SimaDB
26
27
28 class BLCli(MPD):
29
30     def __init__(self, conf, options):
31         super().__init__(conf)
32         self.options = options
33         self.sdb = SimaDB(db_path=conf.get('sima', 'db_file'))
34         atexit.register(self.disconnect)
35         cmd = options.get('command', None)
36         if not cmd or not cmd.startswith('bl-'):
37             return
38         getattr(self, cmd.replace('-', '_'))()
39
40     def bl_view(self):
41         blocklist = self.sdb.view_bl()
42         for entry in ['artist', 'album', 'title']:
43             header = False
44             for blitem in blocklist:
45                 val = blitem.get(entry, '')
46                 mbid = blitem.get(f'musicbrainz_{entry}', '')
47                 if val or mbid:
48                     if not header:
49                         header = True
50                         self.log.info(f'{entry.capitalize()}'
51                                       '(id name musicbranzID):')
52                     self.log.info(f'{blitem["id"]} "{val}"\t\t{mbid}')
53
54     def bl_add_artist(self):
55         artist = self.options.get('artist', None)
56         self.connect()
57         if not artist:  # artist not provided
58             self.log.debug('current track: %r', self.current)
59             if not self.current:
60                 self.log.error('No current song, cannot proceed')
61                 return
62             if not self.current.artist:
63                 self.log.error('No artist for the current song: %r',
64                                self.current)
65                 return
66             self.log.info('Using "%s" (from current track)', self.current.artist)
67             artist = self.current.Artist
68         else:  # artist provided
69             self.log.debug('Looking for %r', artist)
70             search = self.search_artist(Artist(name=artist))
71             if not search:
72                 self.log.warning('Artist not found: "%s"', artist)
73                 return
74             self.log.info('Found artist in library: %s', search)
75             artist = search
76         if self.sdb.get_bl_artist(artist, add=False):
77             self.log.info('Already in blocklist')
78             return
79         self.log.info('Add artist to blocklist "%s"', artist.name)
80         self.sdb.get_bl_artist(artist)
81
82     def bl_add_album(self):
83         album = self.options.get('album', None)
84         self.connect()
85         if not album:  # album not provided
86             self.log.debug('current track: %r', self.current)
87             if not self.current:
88                 self.log.error('No current song, cannot proceed')
89                 return
90             if not self.current.album:
91                 self.log.error('No album for the current song: %r',
92                                self.current)
93                 return
94             if not self.current.artist:
95                 self.log.error('No artist for the current song: %r',
96                                self.current)
97                 return
98             self.log.info('Using "%s" (from current track)', self.current.album)
99             album = Album(self.current.album, mbid=self.current.musicbrainz_albumid,
100                           artist=self.current.Artist)
101         else:  # album provided
102             self.log.debug('Looking for %r', album)
103             album = Album(album)
104             tracks = self.find(f'(album == "{album.name_sz}")',
105                                'window', (0, 1))
106             if not tracks:
107                 self.log.warning('Album not found: "%s"', album)
108                 return
109             track = tracks[0]
110             album = Album(name=track.album, mbid=track.musicbrainz_albumid)
111             self.log.info('Found album in library: %s (by "%s")',
112                           album, track.Artist.albumartist)
113         if self.sdb.get_bl_album(album, add=False):
114             self.log.info('Already in blocklist')
115             return
116         self.log.info('Add album to blocklist "%s"', album)
117         self.sdb.get_bl_album(album)
118
119     def bl_add_track(self):
120         track = self.options.get('track', None)
121         self.connect()
122         if not track:  # track not provided
123             self.log.debug('current track: %r', self.current)
124             if not self.current:
125                 self.log.error('No current song, cannot proceed')
126                 return
127             if not self.current.title:
128                 self.log.error('No title for the current song: %r',
129                                self.current)
130                 return
131             self.log.info('Using "%s" (from current track)', self.current.title)
132             track = self.current
133         else:  # track provided
134             self.log.debug('Looking for %r', track)
135             track_sz = track.replace("'", r"\'")
136             tracks = self.find(f'(title == "{track_sz}")')
137             if not tracks:
138                 self.log.warning('Track not found: "%s"', track)
139                 return
140             if len(tracks) > 1:
141                 artists = {t.artist for t in tracks}
142                 if len(artists) > 1:
143                     self.log.error('Found various artists for this title: %s',
144                                    artists)
145                     return
146             track = tracks[0]
147         if self.sdb.get_bl_track(track, add=False):
148             self.log.info('Already in blocklist')
149             return
150         self.log.info('Add track to blocklist "%s"', track)
151         self.sdb.get_bl_track(track)
152
153     def bl_delete(self):
154         blid = self.options.get('id', None)
155         blocklist = self.sdb.view_bl()
156         if blid not in [bl['id'] for bl in blocklist]:
157             self.log.error('Blocklist ID not found: %s', blid)
158         self.sdb._remove_blocklist_id(blid)
159
160 # VIM MODLINE
161 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8