]> kaliko git repositories - mpd-goodies.git/blob - bin/mcrop
Massive refactoring
[mpd-goodies.git] / bin / mcrop
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2009,2010,2012,2019 kaliko <kaliko@azylum.org>
5 #
6 #   This program 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 #   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 import argparse
21 import sys
22
23 from os.path import basename
24
25 import musicpd
26
27 VERSION = '0.2'
28
29
30 class Crop(musicpd.MPDClient):
31     script_info = dict({
32         'prog': basename(__file__),
33         'description': 'Keep <n> tracks before currently played, removed others. Default is to keep 6 tracks.',
34         'epilog': 'Set MPD host/port in env. var'
35     })
36
37     def __init__(self):
38         """"""
39         musicpd.MPDClient.__init__(self)
40         self.nb_tracks = 6
41         self._get_args()
42         self._run()
43
44     def _get_args(self):
45         """"""
46         parser = argparse.ArgumentParser(**self.__class__.script_info)
47         parser.add_argument('--version', action='version',
48                             version='v%s' % VERSION)
49         parser.add_argument('n', type=int, nargs='?',
50                             help='how many titles to crop')
51         args = parser.parse_args()
52         if args.n:
53             self.nb_tracks = args.n
54
55     def _run(self):
56         """"""
57         self.connect()
58         current_pos = int(self.currentsong().get('pos', 0))
59         if current_pos <= self.nb_tracks:
60             self.disconnect()
61             sys.exit(0)
62         print('Keeping %i tracks' % self.nb_tracks)
63         while current_pos > self.nb_tracks:
64             self.delete(0)
65             current_pos = int(self.currentsong().get('pos', 0))
66         self.disconnect()
67         sys.exit(0)
68
69
70 # Script starts here
71 if __name__ == '__main__':
72     try:
73         Crop()
74     except KeyboardInterrupt:
75         sys.stdout.write('exit')
76
77 # VIM MODLINE
78 # vim: ai ts=4 sw=4 sts=4 expandtab