Skip to content

Instantly share code, notes, and snippets.

@hendriks73
Created May 16, 2019 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hendriks73/736be182cb221b75896898da7127295d to your computer and use it in GitHub Desktop.
Save hendriks73/736be182cb221b75896898da7127295d to your computer and use it in GitHub Desktop.
highly commented traktor/beaTunes import code
def boolean importInfo(StartElement element, String location) {
AudioSong song = null
try {
// read some attributes from the <INFO> element
// first read the attribute named PLAYCOUNT, i.e. <INFO PLAYCOUNT="someValue" ...>
Attribute playcount = element.getAttributeByName(new QName("PLAYCOUNT"))
// read the attribute named LAST_PLAYED, i.e. <INFO LAST_PLAYED="someValue" ...>
Attribute lastPlayed = element.getAttributeByName(new QName("LAST_PLAYED"))
// probably for historic reasons, Traktor calls comment 2 "rating"
// in its .nml file.
// we map its contents to the field custom1.
// read the attribute named RATING, i.e. <INFO RATING="someValue" ...>
Attribute rating = element.getAttributeByName(new QName("RATING"))
// *** if you want to read additional attributes, do so here *** //
// e.g. for genre something like:
// Attribute genre = element.getAttributeByName(new QName("GENRE"))
// the attributeValues for PLAYCOUNT, LAST_PLAYED, and RATING are now
// stored in the variables named playcount, lastPlayed, and rating
// sometimes <INFO ...> does not contain the desired attributes, because they are optional
// if <INFO ...> does not contain PLAYCOUNT, the variable play count will be "null"
// in case none of the desired attributes is present, skip further processing for efficiency.
// *** if you want to add some new attribute, add it to the following list in the "if" statement *** //
// i.e. add something like "|| genre != null"
// note that the || means "or" (logical operator)
if (playcount != null || lastPlayed != null || rating != null) {
// at this point we are certain that at least ONE of the desired attributes is present
// because we definitely are going to import something, we now need to look up
// the song object in beaTunes. we can do that via its location (file path).
song = getSong(location)
// check, if we really found the song. if we can't find it, it's "null"
if (song != null) {
// we have found the song object in beaTunes and can now proceed to manipulate it
// did we read a lastPlayed value? if so call the function setPlayDateUTC() on the song object
// calling song.setPlayDateUTC(...) effectively writes the value to beaTunes and if possible iTunes.
if (lastPlayed != null) {
log.debug("Setting playdate for " + song + ": " + lastPlayed.getValue())
song.setPlayDateUTC(new SimpleDateFormat("yyyy/M/d").parse(lastPlayed.getValue()))
}
// now check whether we have found a play count value
if (playcount != null) {
// apparently we found one. let's write it via song.setPlayCount()
log.debug("Setting playcount for " + song + ": " + playcount.getValue())
song.setPlayCount(Integer.valueOf(playcount.getValue()))
}
// now check whether we have found a rating/comment2 value (weird Traktor naming)
if (rating != null) {
// apparently we found one. let's write it via song.setCustom1()
log.debug("Setting rating/comment2/custom1 for " + song + ": " + rating.getValue())
song.setCustom1(rating.getValue())
}
// to add the genre import, do something like this:
// if (genre != null) {
// // apparently we found one. let's write it via song.setGenre()
// log.debug("Setting genre for " + song + ": " + genre.getValue())
// song.setGenre(genre.getValue())
// }
} else {
log.info("Failed to find song for location " + location)
}
}
} catch(Exception e) {
log.error("Failed to import info for " + song, e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment