1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2021 kaliko <kaliko@azylum.org>
4 # This file is part of sima
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.
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.
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/>.
19 # standard library import
24 from ..mpdclient import MPD, PlayerError, Artist, Album
25 from ..lib.simadb import SimaDB
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-'):
39 getattr(self, cmd.replace('-', '_'))()
40 except PlayerError as err:
45 blocklist = self.sdb.view_bl()
46 for entry in ['artist', 'album', 'title']:
49 art = bl.get(entry, '')
50 mbid = bl.get(f'musicbrainz_{entry}', '')
54 self.log.info(f'{entry.capitalize()}'
55 '(id name musicbranzID):')
56 self.log.info(f'{bl["id"]} "{art}"\t\t{mbid}')
58 def bl_add_artist(self):
59 artist = self.options.get('artist', None)
61 if not artist: # artist not provided
62 self.log.debug('current track: %r', self.current)
64 self.log.error('No current song, cannot proceed')
66 if not self.current.artist:
67 self.log.error('No artist for the current song: %r',
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))
76 self.log.warning('Artist not found: "%s"', artist)
78 self.log.info('Found artist in library: %s', search)
80 if self.sdb.get_bl_artist(artist, add=False):
81 self.log.info('Already in blocklist')
83 self.log.info('Add artist to blocklist "%s"', artist.name)
84 self.sdb.get_bl_artist(artist)
86 def bl_add_album(self):
87 album = self.options.get('album', None)
89 if not album: # album not provided
90 self.log.debug('current track: %r', self.current)
92 self.log.error('No current song, cannot proceed')
94 if not self.current.album:
95 self.log.error('No album for the current song: %r',
98 if not self.current.artist:
99 self.log.error('No artist for the current song: %r',
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)
108 tracks = self.find(f'(album == "{album.name_sz}")',
111 self.log.warning('Album not found: "%s"', album)
114 album = Album(name=track.album, mbid=track.musicbrainz_albumid)
115 self.log.info('Found album in library: %s (by "%s")',
116 album, track.Artist.albumartist)
117 if self.sdb.get_bl_album(album, add=False):
118 self.log.info('Already in blocklist')
120 self.log.info('Add album to blocklist "%s"', album)
121 self.sdb.get_bl_album(album)
123 def bl_add_track(self):
124 track = self.options.get('track', None)
126 if not track: # track not provided
127 self.log.debug('current track: %r', self.current)
129 self.log.error('No current song, cannot proceed')
131 if not self.current.title:
132 self.log.error('No title for the current song: %r',
135 self.log.info('Using "%s" (from current track)', self.current.title)
137 else: # track provided
138 self.log.debug('Looking for %r', track)
139 track_sz = track.replace("'", r"\'")
140 tracks = self.find(f'(title == "{track_sz}")')
142 self.log.warning('Track not found: "%s"', track)
145 artists = {t.artist for t in tracks}
147 self.log.error('Found various artists for this title: %s',
151 if self.sdb.get_bl_track(track, add=False):
152 self.log.info('Already in blocklist')
154 self.log.info('Add track to blocklist "%s"', track)
155 self.sdb.get_bl_track(track)
158 blid = self.options.get('id', None)
159 blocklist = self.sdb.view_bl()
160 if blid not in [bl['id'] for bl in blocklist]:
161 self.log.error('Blocklist ID not found: %s', blid)
162 self.sdb._remove_blocklist_id(blid)
165 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8