]> kaliko git repositories - mpd-sima.git/blob - sima/utils/blcli.py
Add blocklist commands, remove simadb_cli
[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, PlayerError, 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         try:
39             getattr(self, cmd.replace('-', '_'))()
40         except PlayerError as err:
41             self.log.error(err)
42             sys.exit(1)
43
44     def bl_view(self):
45         blocklist = self.sdb.view_bl()
46         for entry in ['artist', 'album', 'title']:
47             header = False
48             for bl in blocklist:
49                 art = bl.get(entry, '')
50                 mbid = bl.get(f'musicbrainz_{entry}', '')
51                 if art or mbid:
52                     if not header:
53                         header = True
54                         self.log.info(f'{entry.capitalize()}'
55                                       '(id name musicbranzID):')
56                     self.log.info(f'{bl["id"]} "{art}"\t\t{mbid}')
57
58     def bl_add_artist(self):
59         artist = self.options.get('artist', None)
60         self.connect()
61         if not artist:  # artist not provided
62             self.log.debug('current track: %r', self.current)
63             if not self.current:
64                 self.log.error('No current song, cannot proceed')
65                 return
66             if not self.current.artist:
67                 self.log.error('No artist for the current song: %r',
68                                self.current)
69                 return
70             self.log.info('Using "%s" (from current track)', self.current.artist)
71             artist = self.current.Artist
72         else:  # artist provided
73             self.log.debug('Looking for %r', artist)
74             search = self.search_artist(Artist(name=artist))
75             if not search:
76                 self.log.warning('Artist not found: "%s"', artist)
77                 return
78             self.log.info('Found artist in library: %s', search)
79             artist = search
80         if self.sdb.get_bl_artist(artist, add=False):
81             self.log.info('Already in blocklist')
82             return
83         self.log.info('Add artist to blocklist "%s"', artist.name)
84         self.sdb.get_bl_artist(artist)
85
86     def bl_add_album(self):
87         album = self.options.get('album', None)
88         self.connect()
89         if not album:  # album not provided
90             self.log.debug('current track: %r', self.current)
91             if not self.current:
92                 self.log.error('No current song, cannot proceed')
93                 return
94             if not self.current.album:
95                 self.log.error('No album for the current song: %r',
96                                self.current)
97                 return
98             if not self.current.artist:
99                 self.log.error('No artist for the current song: %r',
100                                self.current)
101                 return
102             self.log.info('Using "%s" (from current track)', self.current.album)
103             album = Album(self.current.album, mbid=self.current.musicbrainz_albumid,
104                           artist=self.current.Artist)
105         else:  # album provided
106             self.log.debug('Looking for %r', album)
107             album = Album(album)
108             tracks = self.find(f'(album == "{album.name_sz}")',
109                                'window', (0, 1))
110             if not tracks:
111                 self.log.warning('Album not found: "%s"', album)
112                 return
113             track = tracks[0]
114             album = Album(name=track.album, mbid=track.musicbrainz_albumid)
115             artist = Artist(name=track.artist, mbid=track.musicbrainz_artistid)
116             self.log.info('Found album in library: %s (by "%s")',
117                           album, artist)
118         if self.sdb.get_bl_album(album, add=False):
119             self.log.info('Already in blocklist')
120             return
121         self.log.info('Add album to blocklist "%s"', album)
122         self.sdb.get_bl_album(album)
123
124     def bl_add_track(self):
125         track = self.options.get('track', None)
126         self.connect()
127         if not track:  # track not provided
128             self.log.debug('current track: %r', self.current)
129             if not self.current:
130                 self.log.error('No current song, cannot proceed')
131                 return
132             if not self.current.title:
133                 self.log.error('No title for the current song: %r',
134                                self.current)
135                 return
136             self.log.info('Using "%s" (from current track)', self.current.title)
137             track = self.current
138         else:  # track provided
139             self.log.debug('Looking for %r', track)
140             track_sz = track.replace("'", r"\'")
141             tracks = self.find(f'(title == "{track_sz}")')
142             if not tracks:
143                 self.log.warning('Track not found: "%s"', track)
144                 return
145             if len(tracks) > 1:
146                 artists = {t.artist for t in tracks}
147                 if len(artists) > 1:
148                     self.log.error('Found various artists for this title: %s',
149                                    artists)
150                     return
151             track = tracks[0]
152         if self.sdb.get_bl_track(track, add=False):
153             self.log.info('Already in blocklist')
154             return
155         self.log.info('Add track to blocklist "%s"', track)
156         self.sdb.get_bl_track(track)
157
158     def bl_delete(self):
159         blid = self.options.get('id', None)
160         blocklist = self.sdb.view_bl()
161         if blid not in [bl['id'] for bl in blocklist]:
162             self.log.error('Blocklist ID not found: %s', blid)
163         self.sdb._remove_blocklist_id(blid)
164
165 # VIM MODLINE
166 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8