Created
August 18, 2010 18:45
-
-
Save sitnin/535762 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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