* improved music data mappings for acoustid response

This commit is contained in:
Reinhard Pointner 2015-01-11 23:32:37 +00:00
parent 42d5175b54
commit b39cc04cee
1 changed files with 37 additions and 11 deletions

View File

@ -6,10 +6,12 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.ProcessBuilder.Redirect; import java.lang.ProcessBuilder.Redirect;
import java.lang.reflect.Field;
import java.net.URL; import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
@ -101,6 +103,9 @@ public class AcoustIDClient implements MusicIdentificationService {
// submit // submit
response = Charset.forName("UTF-8").decode(post(url, postParam, requestParam)).toString(); response = Charset.forName("UTF-8").decode(post(url, postParam, requestParam)).toString();
// DEBUG
// System.out.println(response);
cache.put(cacheKey, response); cache.put(cacheKey, response);
return response; return response;
} }
@ -158,8 +163,8 @@ public class AcoustIDClient implements MusicIdentificationService {
Object[] secondaryTypes = array(releaseGroup, "secondarytypes"); Object[] secondaryTypes = array(releaseGroup, "secondarytypes");
Object[] releases = array(releaseGroup, "releases"); Object[] releases = array(releaseGroup, "releases");
if (releases == null || secondaryTypes != null || (secondaryTypes != null && secondaryTypes.length > 0) || (type == null || !type.equals("Album"))) { if (releases == null || secondaryTypes != null || (!"Album".equals(type))) {
return null; // ignore music that doesn't belong to a proper album return audioTrack; // default to simple music info if album data is undesirable
} }
for (Object it : releases) { for (Object it : releases) {
@ -201,11 +206,14 @@ public class AcoustIDClient implements MusicIdentificationService {
return thisRelease; return thisRelease;
} }
} }
// default to simple music info if extended info is not available
return audioTrack;
} catch (Exception e) { } catch (Exception e) {
Logger.getLogger(AcoustIDClient.class.getName()).log(Level.WARNING, e.toString(), e); Logger.getLogger(AcoustIDClient.class.getName()).log(Level.WARNING, e.toString(), e);
}
return null; return null;
}).filter(o -> o != null).findFirst().get(); }
}).filter(o -> o != null).sorted(new MostFieldsNotNull()).findFirst().get();
} catch (Exception e) { } catch (Exception e) {
// ignore // ignore
} }
@ -230,13 +238,7 @@ public class AcoustIDClient implements MusicIdentificationService {
Process process = null; Process process = null;
try { try {
ProcessBuilder processBuilder = new ProcessBuilder(command); process = new ProcessBuilder(command).redirectError(Redirect.INHERIT).start();
try {
processBuilder.redirectError(Redirect.INHERIT);
} catch (Throwable e) {
Logger.getLogger(AcoustIDClient.class.getName()).log(Level.WARNING, "Unable to inherit IO: " + e.getMessage());
}
process = processBuilder.start();
} catch (Exception e) { } catch (Exception e) {
throw new IOException("Failed to exec fpcalc: " + e.getMessage()); throw new IOException("Failed to exec fpcalc: " + e.getMessage());
} }
@ -247,6 +249,7 @@ public class AcoustIDClient implements MusicIdentificationService {
try { try {
while (scanner.hasNextLine()) { while (scanner.hasNextLine()) {
String[] value = scanner.nextLine().split("=", 2); String[] value = scanner.nextLine().split("=", 2);
if (value.length != 2) if (value.length != 2)
continue; continue;
@ -265,4 +268,27 @@ public class AcoustIDClient implements MusicIdentificationService {
return results; return results;
} }
private static class MostFieldsNotNull implements Comparator<Object> {
@Override
public int compare(Object o1, Object o2) {
return Integer.compare(count(o2), count(o1));
}
public int count(Object o) {
int n = 0;
try {
for (Field field : o.getClass().getDeclaredFields()) {
if (field.get(o) != null) {
n++;
}
}
} catch (Exception e) {
Logger.getLogger(AcoustIDClient.class.getName()).log(Level.WARNING, e.toString(), e);
}
return n;
}
}
} }