8thCivic.com

Go Back   8th Generation Honda Civic Forum > Civic Style > I.C.E., Electrical, Security, & Navigation

Reply
 
LinkBack Thread Tools Display Modes
Old 11-14-2006, 05:33 AM   #1 (permalink)
Senior Member
 
chylld's Avatar
 
Join Date: May 2006
Location: Sydney, Australia
Age: 25
Posts: 239
iTrader: 0 / 0%
For MP3 HU owners: Java program for parsing iTunes library

Hi all

This is a little program i wrote that parses the iTunes library and copies all 4-star and 5-star songs out into another folder, sorted into further folders for each artist. It also renames the copied files so that they don't include characters which error (underscore) on our HU displays, like ?, *, & etc.

I wrote this because the default itunes way to burn mp3 cd's is either flat or double-nested by artist then album, which i felt didn't work well with my HU which only has prev/next folder and prev/next track.

If you burn the resulting folder structure to cdr (or better, cdrw) you can (if ur HU operates similar to mine) then use the "folder" knob to scroll through artists, and then the prev/next track buttons on the HU or on the steering wheel to scroll through songs for that artist. just seems to make better sense to me than going flat or scrolling to the "unknown album" folder all the time :)

feel free to use and/or modify the code at your own will; i trust if you know how to compile and run it, then you'll know how to change it! i've highlighted the 2 bits you'll probably need to change though.

Quote:
package crumpet;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Crumpet {
public static void main(String[] args) throws Exception {
new Crumpet();
}

public Crumpet() throws Exception {
parseXML("C:\\Documents and Settings\\username\\My Documents\\"+
"My Music\\iTunes\\iTunes Music Library.xml");
}

private void parseXML(String xmlFile) throws Exception {
parseTracks(getElementNodeByKey(
DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.parse(new File(xmlFile))
.getDocumentElement()
.getElementsByTagName("dict")
.item(0),"Tracks"));
}

private void parseTracks(Node tracks) throws Exception {
NodeList children = tracks.getChildNodes();

for(int i=0;i<children.getLength();i++) {
Node n = children.item(i);
if(n.getNodeName().equals("dict")) {
parseTrack(n);
}
}
}

private void parseTrack(Node track) throws Exception {
String artist = getTextValue(getElementNodeByKey(track,"Artist"));
String name = getTextValue(getElementNodeByKey(track,"Name"));
String location = getTextValue(getElementNodeByKey(track,"Location") );
String rating = getTextValue(getElementNodeByKey(track,"Rating"));

if(rating.equals("80") || rating.equals("100")) {
String dest = "Y:\\songs\\"+artist+"\\"+name+".mp3";
fileCopy(URLDecoder.decode(location.substring(17), "UTF-8"),fileSafe(dest));
}
}

private Node getElementNodeByKey(Node node, String key) {
Node ret = null;
NodeList children = node.getChildNodes();

for(int i=0;i<children.getLength();i++) {
Node n = children.item(i);
if(n.getNodeName().equals("key") && getTextValue(n).equals(key)) {
ret = n.getNextSibling();
while(ret.getNodeType()!=Node.ELEMENT_NODE) {
ret = ret.getNextSibling();
}
}
}

return ret;
}

private String getTextValue(Node n) {
String ret = "";

if(n!=null) {
NodeList children = n.getChildNodes();
for(int i=0;i<children.getLength();i++) {
if(children.item(i).getNodeType()==Node.TEXT_NODE) {
ret += children.item(i).getNodeValue().trim();
}
}
}

return ret;
}

private void fileCopy(String src, String dest) throws Exception {
File fSrc = new File(src);
File fDest = new File(dest);

System.out.println(src+" => "+dest);

new File(dest.substring(0,dest.lastIndexOf("\\"))).mkd irs();

FileChannel in = null, out = null;

in = new FileInputStream(fSrc).getChannel();
out = new FileOutputStream(fDest).getChannel();

long size = in.size();
MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);

out.write(buf);

in.close();
out.close();
}

private String fileSafe(String name) {
return name.replace("?","")
.replace("*","-")
.replace("(","")
.replace(")","")
.replace("'","")
.replace(",","")
.replace("&","-");
}
}
Digg this Post!Add Post to del.icio.us
chylld is offline  
Reply With Quote
Old 11-14-2006, 07:02 AM   #2 (permalink)
Senior Member
 
skiz's Avatar
 
Join Date: Sep 2006
Location: Ali ma quippa
Age: 39
Posts: 774
Nee-kon Squad! #5
iTrader: 4 / 100%
dude! that is sweet...

thanks
Digg this Post!Add Post to del.icio.us
skiz is offline  
Reply With Quote
Old 11-14-2006, 03:01 PM   #3 (permalink)
Senior Member
 
Garrison's Avatar
 
Join Date: Sep 2006
Location: Georgia
Age: 33
Posts: 1,071
Garrison
iTrader: 3 / 100%
My Nav unit also adds a number to the song even though my burned mp3 cd already contains the songs numbered. So I have a list of songs with the songs number displayed twice.

(e.g. 01 01 Promiscous-Nelly Furtado)

Will this correct that?
Digg this Post!Add Post to del.icio.us
Garrison is offline  
Reply With Quote
Old 11-14-2006, 03:16 PM   #4 (permalink)
Senior Member
 
chylld's Avatar
 
Join Date: May 2006
Location: Sydney, Australia
Age: 25
Posts: 239
iTrader: 0 / 0%
Quote:
Originally Posted by Garrison
My Nav unit also adds a number to the song even though my burned mp3 cd already contains the songs numbered. So I have a list of songs with the songs number displayed twice.

(e.g. 01 01 Promiscous-Nelly Furtado)

Will this correct that?
most probably yes... the script above extracts the artist and name from the itunes playlist xml (which in turn comes from the mp3's id3 tag) so the name of the copied file shouldn't have that number at the start.
Digg this Post!Add Post to del.icio.us
chylld is offline  
Reply With Quote
Old 11-14-2006, 06:49 PM   #5 (permalink)
Senior Member
 
Garrison's Avatar
 
Join Date: Sep 2006
Location: Georgia
Age: 33
Posts: 1,071
Garrison
iTrader: 3 / 100%
+1 rep. Now, please forgive my ignorance. When do I put this code to make this happen? In the Itunes folder?
Digg this Post!Add Post to del.icio.us
Garrison is offline  
Reply With Quote
Old 11-14-2006, 07:01 PM   #6 (permalink)
Senior Member
 
chylld's Avatar
 
Join Date: May 2006
Location: Sydney, Australia
Age: 25
Posts: 239
iTrader: 0 / 0%
currently the above script exists only as a java program, which means you need a java compiler and java runtime to run it. if you don't know what that means (and i wouldn't blame you) then i suppose you can either learn a bit about java (at http://java.sun.com) or wait until someone writes an executable version that you can just download and run. the reason i haven't done that is simply due to the security risk of trusting an arbitrary program someone wants you to download and run...

apparently you can also write itunes plugins, although i have no experience in that area (yet)... that would be the easiest way as you'd potentially be able to run the program from within itunes.
Digg this Post!Add Post to del.icio.us
chylld is offline  
Reply With Quote
Old 11-14-2006, 07:07 PM   #7 (permalink)
Senior Member
 
Garrison's Avatar
 
Join Date: Sep 2006
Location: Georgia
Age: 33
Posts: 1,071
Garrison
iTrader: 3 / 100%
I guess it is always good to learn something new. Thank you!!
Digg this Post!Add Post to del.icio.us
Garrison is offline  
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -4. The time now is 01:42 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0
copyright 8thcivic.com - all rights reserved