package main import ( "fmt" "strings" "strconv" "os" "bufio" "github.com/beevik/etree" ) func main() { // setup itunes_media := "file://localhost/Z:/Music/usb/" web_media := "https://dtam.pw/music/tracks/" playlist_length := 0 name_next := false artist_next := false location_next := false id_next := false name_temp := "" artist_temp := "" location_temp := "" id_temp := "" // song information type Song_Information struct { name string artist string location string id string } //var songlist = []*Song_Information {} var playlist_tracks []string var songlist []Song_Information var playlist []Song_Information fmt.Printf("%s\n%s\n", itunes_media, web_media) // import the playlist doc := etree.NewDocument() if err := doc.ReadFromFile("main.xml"); err != nil { panic(err) } // find the playlist elements for _, e := range doc.FindElements("./plist/dict/array/dict/array/dict/integer") { // add track to playlist playlist_tracks = append(playlist_tracks, e.Text()) } // get the length of the playlist for _, e := range doc.FindElements("./plist/dict/dict/*") { _ = e playlist_length += 1 } // cycle through the playlist and grab the song information for i := 0; i < playlist_length; i++ { // based on how the xml is formmated from itunes, it has 2 separate elements for each // key, so we need to mark when we find the key we need and say the next item is the item for _, e := range doc.FindElements("./plist/dict/dict/dict[" + strconv.Itoa(i) + "]/*") { if(e.Text() == "Name") { name_next = true continue } if(e.Text() == "Artist") { artist_next = true continue } if(e.Text() == "Location") { location_next = true continue } if(e.Text() == "Track ID") { id_next = true continue } if(name_next) { name_temp = e.Text() name_temp = strings.Replace(name_temp, "&", "&", 5) name_next = false } if(artist_next) { artist_temp = e.Text() artist_temp = strings.Replace(artist_temp, "&", "&", 5) artist_next = false } if(location_next) { location_temp = e.Text() // replace the file name link with the web link location_temp = strings.Replace(location_temp, itunes_media, web_media, 1) location_temp = strings.Replace(location_temp, "&", "&", 5) location_next = false } if(id_next) { id_temp = e.Text() id_next = false } // once we have found all the information we need, put it into a struct and append it // to our master struct if(name_temp != "" && artist_temp != "" && location_temp != "") { songlist = append(songlist, Song_Information{name_temp, artist_temp, location_temp, id_temp}) name_temp = "" artist_temp = "" location_temp = "" } } } // put our tracks in the appropriate order based on the playlist for _, e := range playlist_tracks { for _, f := range songlist { if(f.id == e) { playlist = append(playlist, Song_Information{f.name, f.artist, f.location, f.id}) break } } } // create the xml file file, err := os.OpenFile("main_parsed.xml", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { panic(err) } w := bufio.NewWriter(file) fmt.Fprintln(w, ``) fmt.Fprintln(w, ``) fmt.Fprintln(w, "") for _, e := range playlist { fmt.Fprintln(w, "") fmt.Fprintln(w, "" + e.artist + "") fmt.Fprintln(w, "" + e.name + "") fmt.Fprintln(w, "" + e.location+ "") fmt.Fprintln(w, "") } fmt.Fprintln(w, "") fmt.Fprintln(w, "") w.Flush() }