X-Git-Url: http://git.kaliko.me/?p=mpd-goodies.git;a=blobdiff_plain;f=mtopls;h=6d0c6ee7aff2d7b2267886af0b573ed35b79daaf;hp=78476c60b72b0bc18da9a44d63612b3f97eb143a;hb=fb86eb100710617fea5b6d0e4a7bc452fd016732;hpb=bc382cb2c3140986efbf1d8786e2a58655c26b16 diff --git a/mtopls b/mtopls index 78476c6..6d0c6ee 100755 --- a/mtopls +++ b/mtopls @@ -18,112 +18,89 @@ # # }}} -""" -DOC: -""" import sys -from os import (access, F_OK, W_OK) -from os.path import (dirname, isfile, join, abspath) +from os import(access, W_OK) +from os.path import (dirname, isfile, join, basename, abspath) -from lib.mpdutils import (mconnect, collapse_tags) +from lib.mpdclass import MPDClass +from lib.startop import StartOpt -USAGE = """Usage: +NAME = 'mtopls' +VERSION = '0.1' +USAGE = 'USAGE: %prog [--help] | /path/to/the/playlist/file/' -mtopls [] [--help | -h | help] - - Add the current track to playlist. - - The default playlist, if none specified, is named after the current track's - genre within /var/lib/mpd/playlists/ (creating it if not existing). - - Obviously the script is meant to be executed on hosts where - /var/lib/mpd/playlists/ makes sense or where file entries in the playlist - make sense for you (cf. "man 5 mpd.conf" especially - save_absolute_paths_in_playlists option). - -""" - -class MtoPls(object): +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): """""" - self.pls_path = None - self.cli = mconnect() - self.current = self.cli.currentsong() - self.cli.disconnect() - self._consume_sopt() - self._controls_perm() - self._create_playlist() + StartOpt.__init__(self, self.__class__.script_info, []) + MPDClass.__init__(self) + self.playlist = None self._run() - def _consume_sopt(self): + def _get_opt(self): """""" - if len(sys.argv) >= 2 and \ - sys.argv[1] in ['-h', '--help', 'help']: - sys.stdout.write(USAGE) - sys.exit(1) - if len(sys.argv) == 2: - self.pls_path = sys.argv[1] - if not dirname(self.pls_path): - self.pls_path = abspath(sys.argv[1]) - print >> sys.stdout, ('Playlist set to "%s"' % self.pls_path) - return - if len(sys.argv) == 1: - self._set_playlist() + 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 - sys.stdout.write(USAGE) - sys.exit(1) - - def _set_playlist(self): - """Set playlist is none is given""" + if not access(dirname(self.playlist), W_OK): + self.parser.error('not writable: %s' % dirname(self.playlist)) - mpd_playlists = '/var/lib/mpd/playlists/' - if not access(mpd_playlists, F_OK): - sys.stderr.write('Error: No access to "%s"' % self.pls_path) - sys.exit(1) - genre = collapse_tags(self.current.get('genre', None)) - if not genre: - sys.stderr.write('Error: No genre set in %s\n' % - self.current.get('file')) - print >> sys.stdout, ('Please provide a playlist.') - sys.exit(1) - genre += '.m3u' - self.pls_path = join('/var/lib/mpd/playlists/', genre) + 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 _create_playlist(self): - if not isfile(self.pls_path): + def _write(self): + """""" + if not isfile(self.playlist): # TODO: add M3U header - print >> sys.stdout, ('Create new playlist: %s' % self.pls_path) - open(self.pls_path, 'a').close() - - def _controls_perm(self): - if not access(dirname(self.pls_path), F_OK): - sys.stderr.write('Error: Not existing path: "%s"' % self.pls_path) - sys.exit(1) - if not access(self.pls_path, W_OK): - sys.stderr.write('Error: No write access to path: "%s"' % self.pls_path) - sys.exit(1) - if not isfile(self.pls_path): - sys.stderr.write('Error: Not a regular file: "%s"' % self.pls_path) - sys.exit(1) - return True + 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): """""" - # TODO: controls either file is already in playlist or not - print >> sys.stdout, ('Writing to %s' % self.pls_path) - fd = open(self.pls_path, 'a') - fd.write(self.current.get('file') + '\n') - fd.close() - pass + 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__': - MtoPls() + try: + MtoPls() + except KeyboardInterrupt: + sys.stdout.write('exit') # VIM MODLINE # vim: ai ts=4 sw=4 sts=4 expandtab