Skip to content

Instantly share code, notes, and snippets.

@stleamist
Created November 28, 2019 05:07
Show Gist options
  • Save stleamist/f5ccb832109dbbd1a3d1cfba3c32dceb to your computer and use it in GitHub Desktop.
Save stleamist/f5ccb832109dbbd1a3d1cfba3c32dceb to your computer and use it in GitHub Desktop.
import sys, plistlib, json, re, subprocess
from datetime import datetime as dt
from datetime import timedelta
import urllib.parse as up
def dequote(path):
if (path[0] == path[-1]) and path.startswith(("'", '"')):
return path[1:-1]
else:
return path
def xmlToDict(path):
xml = plistlib.readPlist(path)
jsRaw = json.dumps(xml)
jsDict = json.loads(jsRaw)
tracks = jsDict['Tracks']
return tracks
def getLocation(musicID):
location = tracks[musicID]['Location']
location = up.urlparse(location).path
location = up.unquote(location)
return re.escape(location)
def getDateAdded(musicID):
dateAdded = dt.strptime( tracks[musicID]['Date Added'] , "%Y-%m-%dT%H:%M:%SZ") + timedelta(hours=9)
return dateAdded.strftime("%m/%d/%Y %H:%M:%S")
def changeDate(tracks):
count = 0
errors = []
f = open("./iTL_result-%s.txt" % dt.now().strftime("%y%m%d%H%M%S"), 'w')
for musicID in tracks.keys():
location = ""
try:
location = getLocation(musicID)
except KeyError as e:
errormessage = "error: [%s] %s - %s (message: %s)" % (musicID, tracks[musicID]['Name'], tracks[musicID]['Artist'], e)
print(errormessage)
f.write(errormessage)
errors.append(errormessage)
continue
if (location[-3:] == "mp3" or location[-3:] == "m4a"):
dateAdded = getDateAdded(musicID)
command = "SetFile -d \'%s' %s" % (dateAdded, location)
print(location)
try:
subprocess.check_output(command, shell=True)
count += 1
except subprocess.CalledProcessError as e:
errormessage = "error: %s (message: %s)" % (location, e.output)
print(errormessage)
f.write(errormessage)
errors.append(errormessage)
f.close()
print("Done: %d" % count)
print("%d errors" % len(errors))
print(errors)
path = dequote(sys.argv[1])
tracks = xmlToDict(path)
changeDate(tracks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment