added additional comments, slight cleanup, and slight optimizations
This commit is contained in:
57
convert.go
57
convert.go
@@ -11,20 +11,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// setup
|
|
||||||
itunes_media := "file://localhost/Z:/Music/usb/"
|
// media locations to be replaced
|
||||||
web_media := "https://dtam.pw/music/tracks/"
|
itunes_media := "file://localhost/Z:/Music/usb/" // original location
|
||||||
|
web_media := "https://dtam.pw/music/tracks/" // final web location
|
||||||
|
|
||||||
|
// playlist length counter
|
||||||
playlist_length := 0
|
playlist_length := 0
|
||||||
|
|
||||||
|
// temporary variables to set up and gather information
|
||||||
|
// next: will toggle so we know on the next loop to save information
|
||||||
name_next := false
|
name_next := false
|
||||||
artist_next := false
|
artist_next := false
|
||||||
location_next := false
|
location_next := false
|
||||||
id_next := false
|
id_next := false
|
||||||
|
// temp: temporarily store the song information before we insert it into our array
|
||||||
name_temp := ""
|
name_temp := ""
|
||||||
artist_temp := ""
|
artist_temp := ""
|
||||||
location_temp := ""
|
location_temp := ""
|
||||||
id_temp := ""
|
id_temp := ""
|
||||||
|
|
||||||
// song information
|
// struct containing a single song and it's information
|
||||||
type Song_Information struct {
|
type Song_Information struct {
|
||||||
name string
|
name string
|
||||||
artist string
|
artist string
|
||||||
@@ -32,12 +39,15 @@ func main() {
|
|||||||
id string
|
id string
|
||||||
}
|
}
|
||||||
|
|
||||||
//var songlist = []*Song_Information {}
|
// array of the playlist, but only it's ID. when we initially parse the xml,
|
||||||
|
// it only gives us the playlist in ID form
|
||||||
var playlist_tracks []string
|
var playlist_tracks []string
|
||||||
var songlist []Song_Information
|
|
||||||
var playlist []Song_Information
|
|
||||||
|
|
||||||
fmt.Printf("%s\n%s\n", itunes_media, web_media)
|
// array of the songs, but not in the correct order
|
||||||
|
var songlist []Song_Information
|
||||||
|
|
||||||
|
// array of the songs, in the correct order
|
||||||
|
var playlist []Song_Information
|
||||||
|
|
||||||
// import the playlist
|
// import the playlist
|
||||||
doc := etree.NewDocument()
|
doc := etree.NewDocument()
|
||||||
@@ -45,7 +55,7 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// find the playlist elements
|
// find the playlist in it's ID form
|
||||||
for _, e := range doc.FindElements("./plist/dict/array/dict/array/dict/integer") {
|
for _, e := range doc.FindElements("./plist/dict/array/dict/array/dict/integer") {
|
||||||
// add track to playlist
|
// add track to playlist
|
||||||
playlist_tracks = append(playlist_tracks, e.Text())
|
playlist_tracks = append(playlist_tracks, e.Text())
|
||||||
@@ -59,9 +69,12 @@ func main() {
|
|||||||
|
|
||||||
// cycle through the playlist and grab the song information
|
// cycle through the playlist and grab the song information
|
||||||
for i := 0; i < playlist_length; i++ {
|
for i := 0; i < playlist_length; i++ {
|
||||||
// based on how the xml is formmated from itunes, it has 2 separate elements for each
|
// based on how the xml is formatted 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
|
// 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) + "]/*") {
|
for _, e := range doc.FindElements("./plist/dict/dict/dict[" + strconv.Itoa(i) + "]/*") {
|
||||||
|
|
||||||
|
// if we find an element with the name "Name", we know the next element is the actual
|
||||||
|
// name, so mark it so that we know next loop to store it
|
||||||
if(e.Text() == "Name") {
|
if(e.Text() == "Name") {
|
||||||
name_next = true
|
name_next = true
|
||||||
continue
|
continue
|
||||||
@@ -82,16 +95,20 @@ func main() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// once we have determined that this should be the information, save it
|
||||||
|
// to a temporary variable
|
||||||
if(name_next) {
|
if(name_next) {
|
||||||
name_temp = e.Text()
|
name_temp = e.Text()
|
||||||
name_temp = strings.Replace(name_temp, "&", "&", 5)
|
name_temp = strings.Replace(name_temp, "&", "&", 5) // make the ampersand xml safe
|
||||||
name_next = false
|
name_next = false
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if(artist_next) {
|
if(artist_next) {
|
||||||
artist_temp = e.Text()
|
artist_temp = e.Text()
|
||||||
artist_temp = strings.Replace(artist_temp, "&", "&", 5)
|
artist_temp = strings.Replace(artist_temp, "&", "&", 5)
|
||||||
artist_next = false
|
artist_next = false
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if(location_next) {
|
if(location_next) {
|
||||||
@@ -100,46 +117,56 @@ func main() {
|
|||||||
location_temp = strings.Replace(location_temp, itunes_media, web_media, 1)
|
location_temp = strings.Replace(location_temp, itunes_media, web_media, 1)
|
||||||
location_temp = strings.Replace(location_temp, "&", "&", 5)
|
location_temp = strings.Replace(location_temp, "&", "&", 5)
|
||||||
location_next = false
|
location_next = false
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if(id_next) {
|
if(id_next) {
|
||||||
id_temp = e.Text()
|
id_temp = e.Text()
|
||||||
id_next = false
|
id_next = false
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// once we have found all the information we need, put it into a struct and append it
|
// once we have found all the information we need, put it into a struct and append it
|
||||||
// to our master struct
|
// to our unordered array of songs
|
||||||
if(name_temp != "" && artist_temp != "" && location_temp != "") {
|
if(name_temp != "" && artist_temp != "" && location_temp != "" && id_temp != "") {
|
||||||
|
|
||||||
songlist = append(songlist, Song_Information{name_temp, artist_temp, location_temp, id_temp})
|
songlist = append(songlist, Song_Information{name_temp, artist_temp, location_temp, id_temp})
|
||||||
name_temp = ""
|
name_temp = "" // clear our variables again
|
||||||
artist_temp = ""
|
artist_temp = ""
|
||||||
location_temp = ""
|
location_temp = ""
|
||||||
|
id_temp = ""
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// put our tracks in the appropriate order based on the playlist
|
// put our tracks in the appropriate order based on the playlist
|
||||||
for _, e := range playlist_tracks {
|
for _, e := range playlist_tracks {
|
||||||
|
// loop through our unordered array and find the correct song
|
||||||
for _, f := range songlist {
|
for _, f := range songlist {
|
||||||
if(f.id == e) {
|
if(f.id == e) {
|
||||||
|
// add the correct song to the final playlist
|
||||||
playlist = append(playlist, Song_Information{f.name, f.artist, f.location, f.id})
|
playlist = append(playlist, Song_Information{f.name, f.artist, f.location, f.id})
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// at this point, our playlist should be sorted
|
||||||
|
|
||||||
// create the xml file
|
// create the xml file
|
||||||
file, err := os.OpenFile("main_parsed.xml", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
file, err := os.OpenFile("main_parsed.xml", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// write the initial information for the xml
|
||||||
w := bufio.NewWriter(file)
|
w := bufio.NewWriter(file)
|
||||||
fmt.Fprintln(w, `<?xml version="1.0" encoding="UTF-8"?>`)
|
fmt.Fprintln(w, `<?xml version="1.0" encoding="UTF-8"?>`)
|
||||||
fmt.Fprintln(w, `<playlist xmlns="https://xspf.org/ns/0/" version="1">`)
|
fmt.Fprintln(w, `<playlist xmlns="https://xspf.org/ns/0/" version="1">`)
|
||||||
fmt.Fprintln(w, "<tracklist>")
|
fmt.Fprintln(w, "<tracklist>")
|
||||||
|
|
||||||
|
// cycle through our ordered playlist and write it to the file
|
||||||
for _, e := range playlist {
|
for _, e := range playlist {
|
||||||
fmt.Fprintln(w, "<track>")
|
fmt.Fprintln(w, "<track>")
|
||||||
fmt.Fprintln(w, "<creator>" + e.artist + "</creator>")
|
fmt.Fprintln(w, "<creator>" + e.artist + "</creator>")
|
||||||
@@ -148,9 +175,11 @@ func main() {
|
|||||||
fmt.Fprintln(w, "</track>")
|
fmt.Fprintln(w, "</track>")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// closing information
|
||||||
fmt.Fprintln(w, "</tracklist>")
|
fmt.Fprintln(w, "</tracklist>")
|
||||||
fmt.Fprintln(w, "</playlist>")
|
fmt.Fprintln(w, "</playlist>")
|
||||||
|
|
||||||
|
// write it out
|
||||||
w.Flush()
|
w.Flush()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user