2 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009, 2010 Efrim <efrim@azylum.org> {{{
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.
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.
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/>.
24 from os import(access, W_OK)
25 from os.path import (dirname, isfile, join, basename, abspath)
27 from lib.mpdclass import MPDClass
28 from lib.startop import StartOpt
32 USAGE = 'USAGE: %prog [--help] | /path/to/the/playlist/file/'
34 class MtoPls(StartOpt, MPDClass):
40 'description': 'Save currently played track into your playlists',
46 StartOpt.__init__(self, self.__class__.script_info, [])
47 MPDClass.__init__(self)
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)):
59 if not access(dirname(self.playlist), W_OK):
60 self.parser.error('not writable: %s' % dirname(self.playlist))
62 def _check_dupes(self):
63 """Controls the file is not already in the playlist"""
64 fd = open(self.playlist)
65 lines = fd.readlines()
67 if self.current_song.get('file') + '\n' in lines:
68 print 'File already in the playlist.'
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()
79 fd = open(self.playlist, 'a')
80 fd.write(self.current_song.get('file') + '\n')
85 print 'Connecting %s:%i' % (self.cli_options.host, self.cli_options.port)
87 self.current_song = self.client.currentsong()
88 self.client.disconnect()
90 print 'Will try to add "%s" to "%s"' % (
91 basename(self.current_song.get('file')),
99 if __name__ == '__main__':
102 except KeyboardInterrupt:
103 sys.stdout.write('exit')
106 # vim: ai ts=4 sw=4 sts=4 expandtab