Files
iTunesScraper/scraper.py
2022-03-20 18:57:35 +01:00

46 lines
1.3 KiB
Python
Executable File

#!/usr/bin/python3
# ex: set filetype=python
import argparse
import xml.etree.cElementTree as xml
class Track():
"""
Stores all the information of a track inside an object.
Can be used to easily access the tracks stored in the database.
"""
def __init__(self, xml_doc):
it = iter(xml_doc)
for k, v in zip(it, it):
setattr(self, k.text.replace(' ', ''), v)
def parseXml(xml_file):
"""
Parses an xml file and returns the DOM tree.
"""
tree = xml.parse(xml_file)
root = tree.getroot()
tracks = []
for t in root.findall('.//dict/dict/dict'):
tracks.append(Track(t))
return tracks
def main():
"""
Setup our main program
"""
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--target', type=str, help='The target path to store the scraped \
files into', default='~/itunesscrape')
parser.add_argument('-l', '--logfile', type=str, help='The path to the logfile to use.',
default='~/itunesscrape.log')
parser.add_argument('libraryFile', help='The iTunes library file in XML format.')
args = parser.parse_args()
tracks = parseXml(args.libraryFile)
if __name__ == "__main__":
main()