File: blcli.py

package info (click to toggle)
mpd-sima 0.18.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 716 kB
  • sloc: python: 4,364; sh: 222; makefile: 166
file content (161 lines) | stat: -rw-r--r-- 6,536 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# -*- coding: utf-8 -*-
# Copyright (c) 2021 kaliko <kaliko@azylum.org>
#
#  This file is part of sima
#
#  sima is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  sima is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with sima.  If not, see <http://www.gnu.org/licenses/>.

# standard library import
import atexit
import sys

# local import
from ..mpdclient import MPD, Artist, Album
from ..lib.simadb import SimaDB


class BLCli(MPD):

    def __init__(self, conf, options):
        super().__init__(conf)
        self.options = options
        self.sdb = SimaDB(db_path=conf.get('sima', 'db_file'))
        atexit.register(self.disconnect)
        cmd = options.get('command', None)
        if not cmd or not cmd.startswith('bl-'):
            return
        getattr(self, cmd.replace('-', '_'))()

    def bl_view(self):
        blocklist = self.sdb.view_bl()
        for entry in ['artist', 'album', 'title']:
            header = False
            for blitem in blocklist:
                val = blitem.get(entry, '')
                mbid = blitem.get(f'musicbrainz_{entry}', '')
                if val or mbid:
                    if not header:
                        header = True
                        self.log.info(f'{entry.capitalize()}'
                                      '(id name musicbranzID):')
                    self.log.info(f'{blitem["id"]} "{val}"\t\t{mbid}')

    def bl_add_artist(self):
        artist = self.options.get('artist', None)
        self.connect()
        if not artist:  # artist not provided
            self.log.debug('current track: %r', self.current)
            if not self.current:
                self.log.error('No current song, cannot proceed')
                return
            if not self.current.artist:
                self.log.error('No artist for the current song: %r',
                               self.current)
                return
            self.log.info('Using "%s" (from current track)', self.current.artist)
            artist = self.current.Artist
        else:  # artist provided
            self.log.debug('Looking for %r', artist)
            search = self.search_artist(Artist(name=artist))
            if not search:
                self.log.warning('Artist not found: "%s"', artist)
                return
            self.log.info('Found artist in library: %s', search)
            artist = search
        if self.sdb.get_bl_artist(artist, add=False):
            self.log.info('Already in blocklist')
            return
        self.log.info('Add artist to blocklist "%s"', artist.name)
        self.sdb.get_bl_artist(artist)

    def bl_add_album(self):
        album = self.options.get('album', None)
        self.connect()
        if not album:  # album not provided
            self.log.debug('current track: %r', self.current)
            if not self.current:
                self.log.error('No current song, cannot proceed')
                return
            if not self.current.album:
                self.log.error('No album for the current song: %r',
                               self.current)
                return
            if not self.current.artist:
                self.log.error('No artist for the current song: %r',
                               self.current)
                return
            self.log.info('Using "%s" (from current track)', self.current.album)
            album = Album(self.current.album, mbid=self.current.musicbrainz_albumid,
                          artist=self.current.Artist)
        else:  # album provided
            self.log.debug('Looking for %r', album)
            album = Album(album)
            tracks = self.find(f'(album == "{album.name_sz}")',
                               'window', (0, 1))
            if not tracks:
                self.log.warning('Album not found: "%s"', album)
                return
            track = tracks[0]
            album = Album(name=track.album, mbid=track.musicbrainz_albumid)
            self.log.info('Found album in library: %s (by "%s")',
                          album, track.Artist.albumartist)
        if self.sdb.get_bl_album(album, add=False):
            self.log.info('Already in blocklist')
            return
        self.log.info('Add album to blocklist "%s"', album)
        self.sdb.get_bl_album(album)

    def bl_add_track(self):
        track = self.options.get('track', None)
        self.connect()
        if not track:  # track not provided
            self.log.debug('current track: %r', self.current)
            if not self.current:
                self.log.error('No current song, cannot proceed')
                return
            if not self.current.title:
                self.log.error('No title for the current song: %r',
                               self.current)
                return
            self.log.info('Using "%s" (from current track)', self.current.title)
            track = self.current
        else:  # track provided
            self.log.debug('Looking for %r', track)
            track_sz = track.replace("'", r"\'")
            tracks = self.find(f'(title == "{track_sz}")')
            if not tracks:
                self.log.warning('Track not found: "%s"', track)
                return
            if len(tracks) > 1:
                artists = {t.artist for t in tracks}
                if len(artists) > 1:
                    self.log.error('Found various artists for this title: %s',
                                   artists)
                    return
            track = tracks[0]
        if self.sdb.get_bl_track(track, add=False):
            self.log.info('Already in blocklist')
            return
        self.log.info('Add track to blocklist "%s"', track)
        self.sdb.get_bl_track(track)

    def bl_delete(self):
        blid = self.options.get('id', None)
        blocklist = self.sdb.view_bl()
        if blid not in [bl['id'] for bl in blocklist]:
            self.log.error('Blocklist ID not found: %s', blid)
        self.sdb._remove_blocklist_id(blid)

# VIM MODLINE
# vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8