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