Skip to content

Instantly share code, notes, and snippets.

@joelgallant
Last active December 19, 2015 03:09
Show Gist options
  • Save joelgallant/5888497 to your computer and use it in GitHub Desktop.
Save joelgallant/5888497 to your computer and use it in GitHub Desktop.
Using [Java Music Tag](http://javamusictag.sourceforge.net/), finds albums from MusicBrainz based on title and artist. Isn't fantastic at doing things automatically (musicbrainz search is really bad at sorting). Look at source code to understand how to use.
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Scanner;
import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;
import org.farng.mp3.id3.ID3v1;
public class AlbumFinder {
static final String f = "YOUR FILE HERE";
public static void main(String[] args) throws IOException, TagException {
file(new File(f));
}
public static void file(File f) throws IOException, TagException {
if (f.isFile() && f.getName().contains(".mp3")) {
doFile(new MP3File(f));
} else if (f.isDirectory()) {
Iterator<File> i = Arrays.asList(f.listFiles()).iterator();
while (i.hasNext()) {
file(i.next());
}
}
}
public static void doFile(MP3File file) throws IOException, TagException {
ID3v1 id = file.getID3v1Tag();
if (id.getAlbum().equals("")) {
System.out.println(id.getSongTitle() + " does not have album");
} else {
System.out.println(id.getSongTitle() + " has album (" + id.getAlbum() + ")");
return;
}
System.out.println("Searching for " + id.getLeadArtist() + " " + id.getSongTitle());
System.out.println(("http://musicbrainz.org/taglookup?tag-lookup.artist=" + id.getLeadArtist() + "&tag-lookup.filename=" + id.getSongTitle()).replaceAll(" ", "%20"));
InputStream stream = new URL(("http://musicbrainz.org/taglookup?tag-lookup.artist=" + id.getLeadArtist() + "&tag-lookup.filename=" + id.getSongTitle()).replaceAll(" ", "%20")).openStream();
BufferedReader bis = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String s;
while ((s = bis.readLine()) != null) {
builder.append(s).append('\n');
}
String web = builder.toString();
boolean song = false, artist = false, changed = false;
while (web.contains("<bdi>")) {
String x = web.substring(web.indexOf("<bdi>") + 5, web.indexOf("</bdi>"));
web = web.substring(web.indexOf("</bdi>") + 5);
if (song && artist) {
System.out.println("Found album " + x);
System.out.println("Correct?");
String i = new Scanner(System.in).nextLine();
if (i.equalsIgnoreCase("y")) {
id.setAlbum(x);
changed = true;
break;
} else if (i.equalsIgnoreCase("n")) {
song = false;
artist = false;
} else if (i.equalsIgnoreCase("e")) {
id.setAlbum("Single");
changed = true;
break;
} else if (!i.trim().isEmpty()) {
System.out.println(i + " as album?");
if (new Scanner(System.in).nextLine().equalsIgnoreCase("y")) {
id.setAlbum(i);
changed = true;
break;
}
}
} else if (x.equalsIgnoreCase(id.getSongTitle())) {
if (!song) {
song = true;
artist = false;
}
} else if (x.equalsIgnoreCase(id.getLeadArtist())) {
if (song) {
artist = true;
} else {
artist = false;
}
}
}
if (changed) {
file.setID3v1Tag(id);
file.save();
System.out.println("Saved");
} else {
System.out.println("Could not find result");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment