diff options
author | luxagraf <sng@luxagraf.net> | 2023-06-15 15:58:59 -0500 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2023-06-15 15:58:59 -0500 |
commit | ab987e10f154f5536bb8fd936ae0966e909fa969 (patch) | |
tree | 9de5076f38b71ececb1bc94f8d9d19170898d603 /mpd-playlist-export.py |
added all my scriptssynced/master
Diffstat (limited to 'mpd-playlist-export.py')
-rwxr-xr-x | mpd-playlist-export.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/mpd-playlist-export.py b/mpd-playlist-export.py new file mode 100755 index 0000000..0e1e393 --- /dev/null +++ b/mpd-playlist-export.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +""" +Simple tool that copies current mpd playlist's files to an external directory. +It also creates a m3u playlist file there (eg. for Android devices). +""" + +__author__ = 'Marcin Rataj (http://lidel.org)' +__license__ = 'CC0 (public domain)' + + +# requires: python-mpd >=0.3.0 (http://jatreuman.indefero.net/p/python-mpd/) +from mpd import (MPDClient, CommandError) +from random import choice +from socket import error as SocketError +from shutil import copy +import argparse +import os +import sys + + +## SETTINGS +HOST = 'localhost' +PORT = '6600' +PASSWORD = False +LIBRARY_DIR = '/home/lxf/music/library' + + +def get_paths(root): + client = MPDClient() + client.connect(host=HOST, port=PORT) + + if PASSWORD: + client.password(PASSWORD) + + playlist = client.playlist() + client.disconnect() + + return [entry.replace('file: ', root) for entry in playlist if entry.startswith('file: ')] + + +def copy_files(files, args): + dest = args.output + if not os.path.exists(dest): + os.makedirs(dest) + for file in files: + if not args.quiet: + print("copying '{0}' to '{1}'".format(os.path.basename(file), dest)) + copy(file, dest) + + +def create_playlist(files, args): + dest = args.output + name = os.path.basename(os.path.normpath(args.output)) + playlist_file = open("{0}/{1}.m3u".format(dest, name), 'w') + for song in files: + playlist_file.write(os.path.basename(song) + '\n') + playlist_file.close() + + +def main(parser): + try: + args = parser.parse_args() + playlist_files = get_paths(args.library+"/") + copy_files(playlist_files, args) + create_playlist(playlist_files, args) + except Exception as e: + print("Error occured:\n{0}\nAborted.".format(e)) + return 1 + else: + return 0 + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('-quiet', default=False, action='store_true', help='Be quiet.') + parser.add_argument('-output', required=True, help='Output directory. (required)') + parser.add_argument('-library', default=LIBRARY_DIR, help="MPD library path. Default: {0}".format(LIBRARY_DIR)) + sys.exit(main(parser)) + +# vim: ai ts=4 sw=4 sts=4 expandtab |