]> kaliko git repositories - mpd-goodies.git/blob - bin/wakeup
Massive refactoring
[mpd-goodies.git] / bin / wakeup
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 random import choice
24 from os.path import basename
25
26 import musicpd
27
28
29 VERSION = '0.2'
30
31
32 class WakeUp(musicpd.MPDClient):
33     script_info = dict({
34         'prog': basename(__file__),
35         'description': 'Queues and plays an album chosen at random from a list. Albums list provided from CLI or text file.',
36         'epilog': 'Set MPD host/port in env. var'
37     })
38
39     def __init__(self):
40         musicpd.MPDClient.__init__(self)
41         self._run()
42
43     def _get_args(self):
44         parser = argparse.ArgumentParser(**self.__class__.script_info)
45         parser.add_argument('--version', action='version',
46                             version='v%s' % VERSION)
47         parser.add_argument('-f', type=argparse.FileType('r'), dest='albumf',
48                             help='text file, one album per line', nargs='?')
49         parser.add_argument('album', type=str,
50                             nargs='*', help='album name')
51         args = parser.parse_args()
52         if args.albumf:
53             args.album.extend([line.rstrip(' \n')
54                                for line in args.albumf.readlines()])
55         if not args.album:
56             # parser.print_help()
57             print(
58                 'No album names found on CLI, run "{} -h" for usage'.format(sys.argv[0]))
59             sys.exit(1)
60         return args.album
61
62     def _run(self):
63         albums_list = self._get_args()
64         album = choice(albums_list)
65         print('Queueing "%s"' % album)
66         self.connect()
67         tracks = self.search('album', album)
68         if not tracks:
69             print('No tracks found for "%s"' % album)
70             self.disconnect()
71             sys.exit(1)
72         self.searchadd('album', album)
73         self.disconnect()
74         sys.exit(0)
75
76
77 # Script starts here
78 if __name__ == '__main__':
79     try:
80         WakeUp()
81     except KeyboardInterrupt:
82         print('exit')
83
84 # VIM MODLINE
85 # vim: ai ts=4 sw=4 sts=4 expandtab