]> kaliko git repositories - mpd-goodies.git/blob - src/mtopls
* add Goodie class (wraps mpdclass and startop together)
[mpd-goodies.git] / src / mtopls
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 os import(access, W_OK)
24 from os.path import (dirname, isfile, basename)
25
26 from lib.goodies import Goodie
27
28
29 NAME = 'mtopls'
30 VERSION = '0.1'
31 USAGE = 'USAGE:  %prog [--help] | /path/to/the/playlist/file/'
32
33
34 class MtoPls(Goodie):
35     """
36     """
37     script_info = dict({
38         'version': VERSION,
39         'prog_name': NAME,
40         'description': 'Save currently played track into your playlists',
41         'usage': USAGE,
42         })
43
44     def __init__(self):
45         """"""
46         Goodie.__init__(self, self.__class__.script_info)
47         self.playlist = None
48         self._run()
49
50     def _get_opt(self):
51         """"""
52         if len(self.cli_args) != 1:
53             self.parser.error('need at least a playlist file name!')
54         self.playlist = self.cli_args[0]
55         if (isfile(self.playlist) and
56                 access(self.playlist, W_OK)):
57             return
58         if not access(dirname(self.playlist), W_OK):
59             self.parser.error('not writable: %s' % dirname(self.playlist))
60
61     def _check_dupes(self):
62         """Controls the file is not already in the playlist"""
63         fd = open(self.playlist)
64         lines = fd.readlines()
65         fd.close()
66         if self.current_song.get('file') + '\n' in lines:
67             print 'File already in the playlist.'
68             sys.exit(0)
69
70     def _write(self):
71         """"""
72         if not isfile(self.playlist):
73             # TODO: add M3U header
74             print >> sys.stdout, ('Create new playlist: %s' % self.playlist)
75             open(self.playlist, 'a').close()
76         else:
77             self._check_dupes()
78         fd = open(self.playlist, 'a')
79         fd.write(self.current_song.get('file') + '\n')
80         fd.close()
81
82     def _run(self):
83         """"""
84         print 'Connecting %s:%i' % (self.cli_options.host,
85                 self.cli_options.port)
86         self.mpdConnect()
87         self.current_song = self.client.currentsong()
88         self.client.disconnect()
89         self._get_opt()
90         print 'Will try to add "%s" to "%s"' % (
91                 basename(self.current_song.get('file')),
92                 self.playlist)
93         self._write()
94         print 'Done...'
95         sys.exit(0)
96
97
98 # Script starts here
99 if __name__ == '__main__':
100     try:
101         MtoPls()
102     except KeyboardInterrupt:
103         sys.stdout.write('exit')
104
105 # VIM MODLINE
106 # vim: ai ts=4 sw=4 sts=4 expandtab