]> kaliko git repositories - mpd-sima.git/blob - sima/utils/blcli.py
PlayerError inherit from MPDError instead of plain Exception
[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, MPDError, 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 MPDError 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                 val = bl.get(entry, '')
50                 mbid = bl.get(f'musicbrainz_{entry}', '')
51                 if val 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"]} "{val}"\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             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')
119             return
120         self.log.info('Add album to blocklist "%s"', album)
121         self.sdb.get_bl_album(album)
122
123     def bl_add_track(self):
124         track = self.options.get('track', None)
125         self.connect()
126         if not track:  # track not provided
127             self.log.debug('current track: %r', self.current)
128             if not self.current:
129                 self.log.error('No current song, cannot proceed')
130                 return
131             if not self.current.title:
132                 self.log.error('No title for the current song: %r',
133                                self.current)
134                 return
135             self.log.info('Using "%s" (from current track)', self.current.title)
136             track = self.current
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}")')
141             if not tracks:
142                 self.log.warning('Track not found: "%s"', track)
143                 return
144             if len(tracks) > 1:
145                 artists = {t.artist for t in tracks}
146                 if len(artists) > 1:
147                     self.log.error('Found various artists for this title: %s',
148                                    artists)
149                     return
150             track = tracks[0]
151         if self.sdb.get_bl_track(track, add=False):
152             self.log.info('Already in blocklist')
153             return
154         self.log.info('Add track to blocklist "%s"', track)
155         self.sdb.get_bl_track(track)
156
157     def bl_delete(self):
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)
163
164 # VIM MODLINE
165 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8