Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import locale, sys, os
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
reload(sys)
sys.setdefaultencoding('utf-8')
import plistlib
from pprint import pprint
from urllib import unquote
import shutil
def startup(destination):
print "%s -> %s"%(playlist, destination)
if not os.path.exists(destination):
print "Creating destination directory"
os.mkdir(destination)
os.chdir(destination)
def load_xml_playlist(filename):
res = list()
tracks = plistlib.readPlist(filename)["Tracks"]
for track in tracks:
res.append(unquote(tracks[track]["Location"][16:]))
return tuple(res)
def copy_files(destination, files):
for item in files:
bn = os.path.basename(item)
dn = os.path.join(destination, bn)
if not os.path.exists(dn):
shutil.copyfile(item, dn)
print ("%s — OK"%bn)
else:
print ("%s — SKIPPED"%bn)
def remove_trashed(destination, files):
files_in_dest = os.listdir(destination)
files_in_list = list()
for item in files:
bn = os.path.basename(item)
files_in_list.append(bn)
files_to_delete = list()
for item in files_in_dest:
if item[0] != "." and item not in files_in_list:
files_to_delete.append(item)
for item in files_to_delete:
filename = os.path.join(destination, item)
print("%s — DELETED"%item)
os.remove(filename)
if __name__ == "__main__":
playlist = sys.argv[1]
destination = sys.argv[2]
startup(destination)
files = load_xml_playlist(playlist)
copy_files(destination, files)
remove_trashed(destination, files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment