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