Last active
October 29, 2017 23:40
-
-
Save akmadian/82272f325e03050c3a1b60b47f99863c to your computer and use it in GitHub Desktop.
Makes a list of the music in your iTunes library and exports it to a file in xml or json format.
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
# -*- coding: utf-8 -*- | |
""" | |
File Name: library_export.py | |
Author: Ari Madian | |
Created: October 15, 2017 1:00 PM | |
Python Version: 3.6 | |
Makes a list of the music in your iTunes library | |
and exports it in either xml or JSON | |
- To Use | |
Put the script file in the itunes music directory and run it. | |
To export in JSON, python library_export.py -json | |
To export in XML, python library_export.py -xml | |
- Non standard dependencies | |
- dicttoxml | |
- lxml | |
Edit Oct 29, 2017 - This is already a feature in iTunes so this is pretty much useless. | |
I'll still leave the gist up becuase I'm kinda proud of the code. | |
""" | |
import sys | |
import os | |
import json | |
import dicttoxml | |
from xml.etree import ElementTree as ET | |
library = {} | |
script_path = os.path.os.path.dirname(os.path.realpath(sys.argv[0])) | |
export_format = sys.argv[1:] | |
# Building Dictionary | |
artists = os.listdir(script_path) | |
for artist in artists: | |
library[artist] = None | |
for thing in os.walk(script_path): | |
Path_Artist = thing[0].split('/')[5:] | |
if len(Path_Artist) >= 2: | |
print(thing[0].split('/')[5:], thing[2:]) | |
artist_name = thing[0].split('/')[5:][0] | |
album_name = thing[0].split('/')[5:][1] | |
song_list = [song[3:-4] for song in thing[2:]] | |
if library[artist_name] is not None: | |
l = list(library[artist_name]) | |
l.append({album_name: song_list}) | |
library[artist_name] = tuple(l) | |
elif library[artist_name] is None: | |
library[artist_name] = ({album_name: song_list}) | |
# Exporting | |
if export_format == '-json': | |
with open('file.txt', 'w') as file: | |
file.write(json.dumps(library)) | |
elif export_format == '-xml': | |
xml = dicttoxml.dicttoxml(library, attr_type=False) | |
tree = ET.XML(xml) | |
with open('export.xml', 'w') as f: | |
f.write(str(ET.tostring(tree))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment