Skip to content

Instantly share code, notes, and snippets.

@TheBlackParrot
Created August 17, 2016 09:21
Show Gist options
  • Save TheBlackParrot/2eda53ca386781b3f6559e29e28a3ea3 to your computer and use it in GitHub Desktop.
Save TheBlackParrot/2eda53ca386781b3f6559e29e28a3ea3 to your computer and use it in GitHub Desktop.
(MPD/Mopidy) Adds tracks to the playlist newly added to the library
#!/usr/bin/python
import os;
import pickle;
from subprocess import check_output;
from subprocess import Popen;
from mpd import MPDClient;
run_dir = os.environ.get("XDG_RUNTIME_DIR");
client = MPDClient();
client.timeout = 10;
client.idletimeout = None;
client.connect("localhost", 6600);
try:
cache_file = open(run_dir + "/mpd_cache.pickle", 'rb');
old = pickle.load(cache_file);
except:
cache_file = None;
old = [];
new = check_output(["mpc", "ls", "Local media/Tracks"]).decode("utf-8").strip().split("\n");
if not cache_file:
final = set(new);
else:
final = set(new) - set(old);
try:
if cache_file:
cache_file.close();
cache_file = open(run_dir + "/mpd_cache.pickle", 'wb');
pickle.dump(new, cache_file);
except:
print("Couldn't write cache pickle");
pass;
client.command_list_ok_begin();
for item in final:
print("Adding {} to the current playlist...".format(item));
client.add(item);
results = client.command_list_end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment