Created
April 17, 2016 18:25
-
-
Save libcrack/af52d22b3142c0e7eded3fb54892560d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 python2 | |
| # coding: utf-8 | |
| # Sun Apr 17 19:34:37 CEST 2016 | |
| # (mecagon el puto itunes y susmuertos a caballo) | |
| """ Rebuilds iTunes database plist which is usually | |
| located in ~/Music/iTunes/iTunes Music Library.xml | |
| iTunes track object keys: | |
| Minor Version | |
| Playlists | |
| Features | |
| Major Version | |
| Library Persistent ID | |
| Music Folder | |
| Application Version | |
| Tracks | |
| Show Content Ratings | |
| Date | |
| """ | |
| import os | |
| import sys | |
| import shutil | |
| import urllib | |
| import plistlib | |
| DEBUG = False | |
| xmldb_path = "/Music/iTunes/iTunes Music Library.xml" | |
| itunesdb_path = os.path.expanduser("~") + xmldb_path | |
| itunesdb_path_backup = itunesdb_path + ".backup" | |
| print("[*] Creating iTunes database backup \"{0}\"".format(itunesdb_path_backup)) | |
| try: | |
| shutil.copyfile(itunesdb_path,itunesdb_path_backup) | |
| except IOError as e: | |
| print("[!] Error: Cannot create backup \"{0}\"".format(itunesdb_path_backup)) | |
| raise Exception(e) | |
| print("[*] Reading iTunes database \"{0}\"".format(itunesdb_path)) | |
| try: | |
| itunesdb = plistlib.readPlist(itunesdb_path) | |
| except IOError as e: | |
| print("[!] Error: Cannot read iTunes database \"{0}\"".format(itunesdb_path)) | |
| raise Exception(e) | |
| for track_id,track in itunesdb["Tracks"].items(): | |
| if not track["Location"].startswith("file://"): | |
| if DEBUG: print("[i] Invalid location: skipping {0}".format(track["Location"])) | |
| continue | |
| try: | |
| if "audio" not in track["Kind"]: | |
| if DEBUG: print("[i] Not an audio file: skipping {0}".format(track["Location"])) | |
| continue | |
| except: | |
| pass | |
| track_path = urllib.unquote(track["Location"])[7:] | |
| if not os.path.isfile(track_path): | |
| print("[*] Deleting file entry \"{0}\"".format(track_path)) | |
| del(itunesdb["Tracks"][track_id]) | |
| else: | |
| if DEBUG: print("[*] Valid entry ({0}) \"{1}\"".format(track_id,track_path)) | |
| print("[*] Writting iTunes database \"{0}\"".format(itunesdb_path)) | |
| try: | |
| plistlib.writePlist(itunesdb, itunesdb_path) | |
| except TypeError: | |
| print "[!] Error: Could not write iTunes database \"{0}\"".format(itunesdb_path) | |
| print "[*] Done" | |
| sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment