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, 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-'):
38 getattr(self, cmd.replace('-', '_'))()
41 blocklist = self.sdb.view_bl()
42 for entry in ['artist', 'album', 'title']:
44 for blitem in blocklist:
45 val = blitem.get(entry, '')
46 mbid = blitem.get(f'musicbrainz_{entry}', '')
50 self.log.info(f'{entry.capitalize()}'
51 '(id name musicbranzID):')
52 self.log.info(f'{blitem["id"]} "{val}"\t\t{mbid}')
54 def bl_add_artist(self):
55 artist = self.options.get('artist', None)
57 if not artist: # artist not provided
58 self.log.debug('current track: %r', self.current)
60 self.log.error('No current song, cannot proceed')
62 if not self.current.artist:
63 self.log.error('No artist for the current song: %r',
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))
72 self.log.warning('Artist not found: "%s"', artist)
74 self.log.info('Found artist in library: %s', search)
76 if self.sdb.get_bl_artist(artist, add=False):
77 self.log.info('Already in blocklist')
79 self.log.info('Add artist to blocklist "%s"', artist.name)
80 self.sdb.get_bl_artist(artist)
82 def bl_add_album(self):
83 album = self.options.get('album', None)
85 if not album: # album not provided
86 self.log.debug('current track: %r', self.current)
88 self.log.error('No current song, cannot proceed')
90 if not self.current.album:
91 self.log.error('No album for the current song: %r',
94 if not self.current.artist:
95 self.log.error('No artist for the current song: %r',
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)
104 tracks = self.find(f'(album == "{album.name_sz}")',
107 self.log.warning('Album not found: "%s"', album)
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')
116 self.log.info('Add album to blocklist "%s"', album)
117 self.sdb.get_bl_album(album)
119 def bl_add_track(self):
120 track = self.options.get('track', None)
122 if not track: # track not provided
123 self.log.debug('current track: %r', self.current)
125 self.log.error('No current song, cannot proceed')
127 if not self.current.title:
128 self.log.error('No title for the current song: %r',
131 self.log.info('Using "%s" (from current track)', self.current.title)
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}")')
138 self.log.warning('Track not found: "%s"', track)
141 artists = {t.artist for t in tracks}
143 self.log.error('Found various artists for this title: %s',
147 if self.sdb.get_bl_track(track, add=False):
148 self.log.info('Already in blocklist')
150 self.log.info('Add track to blocklist "%s"', track)
151 self.sdb.get_bl_track(track)
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)
161 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8