--- /dev/null
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2009, 2010 Efrim <efrim@azylum.org>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+
+import sys
+
+from os import(access, W_OK)
+from os.path import (dirname, isfile, join, basename, abspath)
+
+from lib.mpdclass import MPDClass
+from lib.startop import StartOpt
+
+NAME = 'mtopls'
+VERSION = '0.1'
+USAGE = 'USAGE: %prog [--help] | /path/to/the/playlist/file/'
+
+
+class MtoPls(StartOpt, MPDClass):
+ """
+ """
+ script_info = dict({
+ 'version': VERSION,
+ 'prog_name': NAME,
+ 'description': 'Save currently played track into your playlists',
+ 'usage': USAGE,
+ })
+
+ def __init__(self):
+ """"""
+ StartOpt.__init__(self, self.__class__.script_info, [])
+ MPDClass.__init__(self)
+ self.playlist = None
+ self._run()
+
+ def _get_opt(self):
+ """"""
+ if len(self.cli_args) != 1:
+ self.parser.error('need at least a playlist file name!')
+ self.playlist = self.cli_args[0]
+ if (isfile(self.playlist) and
+ access(self.playlist, W_OK)):
+ return
+ if not access(dirname(self.playlist), W_OK):
+ self.parser.error('not writable: %s' % dirname(self.playlist))
+
+ def _check_dupes(self):
+ """Controls the file is not already in the playlist"""
+ fd = open(self.playlist)
+ lines = fd.readlines()
+ fd.close()
+ if self.current_song.get('file') + '\n' in lines:
+ print 'File already in the playlist.'
+ sys.exit(0)
+
+ def _write(self):
+ """"""
+ if not isfile(self.playlist):
+ # TODO: add M3U header
+ print >> sys.stdout, ('Create new playlist: %s' % self.playlist)
+ open(self.playlist, 'a').close()
+ else:
+ self._check_dupes()
+ fd = open(self.playlist, 'a')
+ fd.write(self.current_song.get('file') + '\n')
+ fd.close()
+
+ def _run(self):
+ """"""
+ print 'Connecting %s:%i' % (self.cli_options.host,
+ self.cli_options.port)
+ self.mpdConnect()
+ self.current_song = self.client.currentsong()
+ self.client.disconnect()
+ self._get_opt()
+ print 'Will try to add "%s" to "%s"' % (
+ basename(self.current_song.get('file')),
+ self.playlist)
+ self._write()
+ print 'Done...'
+ sys.exit(0)
+
+
+# Script starts here
+if __name__ == '__main__':
+ try:
+ MtoPls()
+ except KeyboardInterrupt:
+ sys.stdout.write('exit')
+
+# VIM MODLINE
+# vim: ai ts=4 sw=4 sts=4 expandtab