{"id":1111,"date":"2013-03-13T19:49:16","date_gmt":"2013-03-13T18:49:16","guid":{"rendered":"http:\/\/denbeke.be\/blog\/?page_id=1111"},"modified":"2013-03-13T19:49:16","modified_gmt":"2013-03-13T18:49:16","slug":"itunes-library-parser-itunesparser","status":"publish","type":"page","link":"https:\/\/denbeke.be\/blog\/itunes-library-parser\/itunes-library-parser-itunesparser\/","title":{"rendered":"iTunes Library Parser &#8211; iTunesParser"},"content":{"rendered":"<pre><code data-language=\"python\">class iTunesParser:\r\n'''Class for parsing an iTunes Library'''\r\n\r\ndef __init__(self, input_file):\r\n    '''Constructor'''\r\n    self.source = input_file #Source file\r\n    self.xml = ElementTree.parse(input_file) # Parsed XML File in a tree\r\n    self.library = list() #List containing all the tracks\r\n\r\n\r\ndef parse(self):\r\n    '''Parse the file'''\r\n    # Documentation about Element Tree ->  http:\/\/docs.python.org\/2\/library\/xml.etree.elementtree.html\r\n    root = self.xml.getroot()\r\n    \r\n    #Find index of element containing tracks\r\n    i = 0\r\n    \r\n    for child in root[0]:\r\n        if child.text == \"Tracks\":\r\n            tracksIndex = i+1\r\n            break\r\n        i += 1\r\n    \r\n            \r\n    #Loop trough all songs\r\n    i = 0\r\n    for song in root[0][tracksIndex]:\r\n        #Every second element is a track\r\n        if i % 2 == 1:\r\n            \r\n            #Loop through metadata of the track and extract info\r\n            j = 0\r\n            \r\n            albumArtist = None\r\n            \r\n            for tag in song:\r\n\r\n                if tag.text == \"Name\":\r\n                    trackName = song[j+1].text\r\n                \r\n                elif tag.text == \"Artist\":\r\n                    artist = song[j+1].text\r\n                    \r\n                elif tag.text == \"Album Artist\":\r\n                    albumArtist = song[j+1].text\r\n                    \r\n                elif tag.text == \"Album\":\r\n                    album = song[j+1].text\r\n                    \r\n                elif tag.text == \"Genre\":\r\n                    genre = song[j+1].text\r\n                    \r\n                elif tag.text == \"Year\":\r\n                    year = song[j+1].text\r\n                \r\n                j += 1\r\n            \r\n            \r\n            #Sometime Album Artist is not defined, then we take the artist as album artist\r\n            if albumArtist == None:\r\n                albumArtist = artist\r\n            \r\n            #Make new track\r\n            try:\r\n                track = Track(trackName, artist, albumArtist, album, year, genre)\r\n            except:\r\n                #Sometime Album Artist is not defined, then we take the artist as album artist\r\n                track = Track(trackName, artist, artist, album, year, genre)\r\n            \r\n            #Add track to 'library'\r\n            self.library.append(track)\r\n        \r\n        i += 1\t\t\t\r\n        \r\n    \r\n    return\r\n\r\n\r\ndef getTrackList(self):\r\n    '''Returns list with all tracks'''\r\n    return self.library\r\n    \r\n\r\ndef __str__(self):\r\n    '''Returns string of iTunes Library with all albums'''\r\n    \r\n    string = \"\"\r\n    \r\n    for i in range ( len(self.library) ):\r\n        \r\n        string += str(i+1)+\". \"\r\n        \r\n        string += str(self.library[i])\r\n        \r\n        if i != len(self.library) - 1:\r\n            string += \"\\n\"\r\n    \r\n    return string<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>class iTunesParser: &#8221;&#8217;Class for parsing an iTunes Library&#8221;&#8217; def __init__(self, input_file): &#8221;&#8217;Constructor&#8221;&#8217; self.source = input_file #Source file self.xml = ElementTree.parse(input_file) # Parsed XML File in a tree self.library = list() #List containing all the tracks def parse(self): &#8221;&#8217;Parse the file&#8221;&#8217; # Documentation about Element Tree -> http:\/\/docs.python.org\/2\/library\/xml.etree.elementtree.html root = self.xml.getroot() #Find index of element containing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1102,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-1111","page","type-page","status-publish","hentry","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>iTunes Library Parser - iTunesParser &#8211; DenBeke<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/denbeke.be\/blog\/itunes-library-parser\/itunes-library-parser-itunesparser\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"iTunes Library Parser - iTunesParser &#8211; DenBeke\" \/>\n<meta property=\"og:description\" content=\"class iTunesParser: &#039;&#039;&#039;Class for parsing an iTunes Library&#039;&#039;&#039; def __init__(self, input_file): &#039;&#039;&#039;Constructor&#039;&#039;&#039; self.source = input_file #Source file self.xml = ElementTree.parse(input_file) # Parsed XML File in a tree self.library = list() #List containing all the tracks def parse(self): &#039;&#039;&#039;Parse the file&#039;&#039;&#039; # Documentation about Element Tree -&gt; http:\/\/docs.python.org\/2\/library\/xml.etree.elementtree.html root = self.xml.getroot() #Find index of element containing [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/denbeke.be\/blog\/itunes-library-parser\/itunes-library-parser-itunesparser\/\" \/>\n<meta property=\"og:site_name\" content=\"DenBeke\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/itunes-library-parser\\\/itunes-library-parser-itunesparser\\\/\",\"url\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/itunes-library-parser\\\/itunes-library-parser-itunesparser\\\/\",\"name\":\"iTunes Library Parser - iTunesParser &#8211; DenBeke\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#website\"},\"datePublished\":\"2013-03-13T18:49:16+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/itunes-library-parser\\\/itunes-library-parser-itunesparser\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/denbeke.be\\\/blog\\\/itunes-library-parser\\\/itunes-library-parser-itunesparser\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/itunes-library-parser\\\/itunes-library-parser-itunesparser\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"iTunes Library Parser\",\"item\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/itunes-library-parser\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"iTunes Library Parser &#8211; iTunesParser\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/\",\"name\":\"DenBeke\",\"description\":\"Mathias Beke\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"iTunes Library Parser - iTunesParser &#8211; DenBeke","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/denbeke.be\/blog\/itunes-library-parser\/itunes-library-parser-itunesparser\/","og_locale":"en_US","og_type":"article","og_title":"iTunes Library Parser - iTunesParser &#8211; DenBeke","og_description":"class iTunesParser: '''Class for parsing an iTunes Library''' def __init__(self, input_file): '''Constructor''' self.source = input_file #Source file self.xml = ElementTree.parse(input_file) # Parsed XML File in a tree self.library = list() #List containing all the tracks def parse(self): '''Parse the file''' # Documentation about Element Tree -> http:\/\/docs.python.org\/2\/library\/xml.etree.elementtree.html root = self.xml.getroot() #Find index of element containing [&hellip;]","og_url":"https:\/\/denbeke.be\/blog\/itunes-library-parser\/itunes-library-parser-itunesparser\/","og_site_name":"DenBeke","twitter_card":"summary_large_image","twitter_site":"@MthsBk","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/denbeke.be\/blog\/itunes-library-parser\/itunes-library-parser-itunesparser\/","url":"https:\/\/denbeke.be\/blog\/itunes-library-parser\/itunes-library-parser-itunesparser\/","name":"iTunes Library Parser - iTunesParser &#8211; DenBeke","isPartOf":{"@id":"https:\/\/denbeke.be\/blog\/#website"},"datePublished":"2013-03-13T18:49:16+00:00","breadcrumb":{"@id":"https:\/\/denbeke.be\/blog\/itunes-library-parser\/itunes-library-parser-itunesparser\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/denbeke.be\/blog\/itunes-library-parser\/itunes-library-parser-itunesparser\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/denbeke.be\/blog\/itunes-library-parser\/itunes-library-parser-itunesparser\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/denbeke.be\/blog\/"},{"@type":"ListItem","position":2,"name":"iTunes Library Parser","item":"https:\/\/denbeke.be\/blog\/itunes-library-parser\/"},{"@type":"ListItem","position":3,"name":"iTunes Library Parser &#8211; iTunesParser"}]},{"@type":"WebSite","@id":"https:\/\/denbeke.be\/blog\/#website","url":"https:\/\/denbeke.be\/blog\/","name":"DenBeke","description":"Mathias Beke","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/denbeke.be\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/pages\/1111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/comments?post=1111"}],"version-history":[{"count":1,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/pages\/1111\/revisions"}],"predecessor-version":[{"id":1112,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/pages\/1111\/revisions\/1112"}],"up":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/pages\/1102"}],"wp:attachment":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/media?parent=1111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}