Created
June 7, 2009 17:24
-
-
Save jbgutierrez/125401 to your computer and use it in GitHub Desktop.
Así es como tenía consistente el catálogo de iTunes antes de conocer http://dougscripts.com/itunes/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.sun.org.apache.bcel.internal.classfile.JavaClass; | |
import java.util.Arrays; | |
import javax.xml.xpath.*; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import org.xml.sax.InputSource; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
import java.util.HashSet; | |
import java.net.URLDecoder; | |
public class CheckItunesLibrary { | |
static HashSet<String> setItunes = new HashSet<String>(); | |
static HashSet<String> setFiles = new HashSet<String>(); | |
static float sizeOrphanFiles; | |
public static void main(String[] args) { | |
try { | |
String iTunesLibraryPath = null; | |
String iTunesCatalogPath = null; | |
if (args.length==0) usage(); | |
// Parse arguments | |
if (args[0].equals("-usage")) { | |
usage(); | |
} else if (args[0].equals("-help")) { | |
usage(); | |
} else { | |
if (args.length!=2) usage(); | |
iTunesLibraryPath = args[0]; | |
iTunesCatalogPath = args[1]; | |
} | |
XPathFactory factory = XPathFactory.newInstance(); | |
XPath xPath = factory.newXPath(); | |
File xmlDocument = new File(iTunesCatalogPath); | |
InputSource inputSource = new InputSource(new FileInputStream(xmlDocument)); | |
String expression = "/child::plist/child::dict/child::dict/child::dict/child::key[text()='Location']/following-sibling::string/child::text()"; | |
NodeList nodes = (NodeList) xPath.evaluate(expression, inputSource, XPathConstants.NODESET); | |
for (int i = 0; i < nodes.getLength(); i++) { | |
String path = URLDecoder.decode(nodes.item(i).getNodeValue(),"UTF-8"); | |
setItunes.add(parseFilePath(path)); | |
} | |
File f = new File(iTunesLibraryPath); | |
checkFiles(f); | |
System.out.println(setItunes.size() + " missing files"); | |
System.out.println(join(setItunes.toArray(new String[1]),"\n")); | |
System.out.println(setFiles.size() + " orphan files (" + sizeOrphanFiles + " MB)"); | |
System.out.println(join(setFiles.toArray(new String[1]),"\n")); | |
} catch (Exception e) { | |
System.out.println("MESSAGE:\t" + e.getMessage()); | |
System.out.println("STACK:\t" + Arrays.toString(e.getStackTrace())); | |
e.printStackTrace(); | |
} | |
} | |
private static void usage() { | |
System.err.println("Usage: CheckItunesLibrary <libraryPath> <catalogPath>"); | |
System.err.println(" -usage or -help = this message"); | |
System.exit(1); | |
} | |
private static String parseFilePath(String path) { | |
if (File.separatorChar != '/') { | |
path = path.replace(File.separatorChar, '/'); | |
} | |
return path; | |
} | |
private static void checkFiles(File f) { | |
if (f.isDirectory()) { | |
File[] files = f.listFiles(); | |
for (File file: files) { | |
if (file.isDirectory()) | |
checkFiles(file); | |
else | |
checkFile(file); | |
} | |
} | |
else | |
checkFile(f); | |
} | |
private static void checkFile(File f) { | |
if (!setItunes.remove(parseFilePath(f.getAbsolutePath()))) { | |
setFiles.add(parseFilePath(f.getAbsolutePath())); | |
sizeOrphanFiles += f.length() / (1024*1024); | |
} | |
} | |
private static String join(String[] strings, String separator) { | |
StringBuffer sb = new StringBuffer(); | |
for (String s: strings) { | |
sb.append(s); | |
sb.append(separator); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment