]> kaliko git repositories - mpd-goodies.git/blob - src/wakeup
* add Goodie class (wraps mpdclass and startop together)
[mpd-goodies.git] / src / wakeup
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2009, 2010, 2012 Kaliko Jack <kaliko.jack@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
21 import sys
22
23 from random import choice
24
25 from lib.goodies import Goodie
26
27
28 NAME = 'wakeup'
29 VERSION = '0.1'
30 USAGE = 'USAGE:  %prog --help | AlbumA AlbumB "The Album C" | -f file'
31
32 WAKEUP_OPTS = list([
33     {
34         'sw': ['-f', '--file'],
35         'type': 'str',
36         'dest': 'file',
37         'metavar': 'filename',
38         'help': 'filemame to read album list from. An album name on each line'},
39     ])
40
41
42 class WakeUp(Goodie):
43     """
44     """
45     script_info = dict({
46         'version': VERSION,
47         'prog_name': NAME,
48         'description': 'Queue and play an album chosen at random from a list. Albums list provided from CLI or text file.',
49         'usage': USAGE,
50         })
51
52     def __init__(self):
53         """"""
54         Goodie.__init__(self, self.__class__.script_info,
55                 extra_options=WAKEUP_OPTS)
56         self._get_arg()
57         self._run()
58
59     def _get_arg(self):
60         """"""
61         if self.cli_options.file:
62             # TODO: handle encoding reading file!
63             print 'reading from file, assuming %s encoding' % self.localencoding
64             try:
65                 with open(self.cli_options.file) as f:
66                     self.albums_list = [line.rstrip('\n') for line in f.readlines()]
67                     return True
68             except IOError, err:
69                 print 'ERROR: "%s": %s' % (self.cli_options.file, err[1])
70                 sys.exit(1)
71             if self.cli_args:
72                 print 'WARNING: ignoring "%s"' % self.cli_args
73                 return True
74         if self.cli_args:
75             self.albums_list = self.cli_args
76             return True
77         self.parser.print_usage()
78         print "ERROR: Need at least an argument!"
79         sys.exit(1)
80
81     def _run(self):
82         """"""
83         print 'Connecting %s:%i' % (self.cli_options.host, self.cli_options.port)
84         self.mpdConnect()
85         album = choice(self.albums_list)
86         print 'Queueing "%s"' % album
87         tracks = self.client.find('album', self._u8_convert(album))
88         if not tracks:
89             print 'No tracks found for "%s"' % album
90             self.client.disconnect()
91             sys.exit(1)
92         for track in tracks:
93             self.client.add(track.get('file'))
94         self.client.disconnect()
95         sys.exit(0)
96
97
98 # Script starts here
99 if __name__ == '__main__':
100     try:
101         WakeUp()
102     except KeyboardInterrupt:
103         sys.stdout.write('exit')
104
105 # VIM MODLINE
106 # vim: ai ts=4 sw=4 sts=4 expandtab