From a902c00e46d8910767534752142ae167d8d73beb Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 14 Mar 2013 15:35:57 +0000 Subject: [PATCH] * make the internal AcoustID api more flexible --- .../net/sourceforge/filebot/web/AcoustID.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/source/net/sourceforge/filebot/web/AcoustID.java b/source/net/sourceforge/filebot/web/AcoustID.java index ff3b2297..771aba6f 100644 --- a/source/net/sourceforge/filebot/web/AcoustID.java +++ b/source/net/sourceforge/filebot/web/AcoustID.java @@ -2,6 +2,7 @@ package net.sourceforge.filebot.web; +import static java.util.Collections.*; import static net.sourceforge.filebot.web.WebRequest.*; import static net.sourceforge.tuned.FileUtilities.*; @@ -58,26 +59,39 @@ public class AcoustID implements MusicIdentificationService { Map results = new LinkedHashMap(); for (Map fp : fpcalc(files)) { - results.put(new File(fp.get("FILE")), lookup(fp.get("DURATION"), fp.get("FINGERPRINT"))); + results.put(new File(fp.get("FILE")), parse(lookup(fp.get("DURATION"), fp.get("FINGERPRINT")))); } return results; } - public AudioTrack lookup(String duration, String fingerprint) throws IOException, InterruptedException, ParseException { + public String lookup(File file) throws Exception { + Map fp = fpcalc(singleton(file)).get(0); + return lookup(fp.get("DURATION"), fp.get("FINGERPRINT")); + } + + + public String lookup(String duration, String fingerprint) throws IOException, InterruptedException, ParseException { // http://api.acoustid.org/v2/lookup?client=8XaBELgH&meta=recordings+releasegroups+compress&duration=641&fingerprint=AQABz0qUkZK4oOfhL-CPc4e5C_wW2H2QH9uDL4cvoT8UNQ-eHtsE8cceeFJx-LiiHT-aPzhxoc-Opj_eI5d2hOFyMJRzfDk-QSsu7fBxqZDMHcfxPfDIoPWxv9C1o3yg44d_3Df2GJaUQeeR-cb2HfaPNsdxHj2PJnpwPMN3aPcEMzd-_MeB_Ej4D_CLP8ghHjkJv_jh_UDuQ8xnILwunPg6hF2R8HgzvLhxHVYP_ziJX0eKPnIE1UePMByDJyg7wz_6yELs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BCFOECQFAIgAABR2QAgFjCDMA0AUMIoAIMChQghChASGEGeYEAIAIhgBSErnJPPEGWYAMgw05AhiiGHiBBBGGSCQcQgwRYJwhDDhgCSCSSEIQYwILoyAjAIigBFEUQK8gAYAQ5BCAAjkjCCAEEMZAUQAZQCjCCkpCgFMCCiIcVIAZZgilAQAiSHQECOcQAQIc4QClAHAjDDGkAGAMUoBgyhihgEChFCAAWEIEYwIJYwViAAlHCBIGEIEAEIQAoBwwgwiEBAEEEOoEwBY4wRwxAhBgAcKAESIQAwwIowRFhoBhAE URL url = new URL("http://api.acoustid.org/v2/lookup?client=" + apikey + "&meta=recordings+releasegroups+releases+tracks+compress&duration=" + duration + "&fingerprint=" + fingerprint); Cache cache = Cache.getCache("web-datasource"); - AudioTrack audioTrack = cache.get(url, AudioTrack.class); - if (audioTrack != null) - return audioTrack; + String response = cache.get(url, String.class); + if (response != null) { + return response; + } // respect rate limit REQUEST_LIMIT.acquirePermit(); + response = readAll(getReader(url.openConnection())); + cache.put(url, response); - String response = readAll(getReader(url.openConnection())); - Map data = (Map) new JSONParser().parse(response); + return response; + } + + + public AudioTrack parse(String json) throws IOException, InterruptedException, ParseException { + Map data = (Map) new JSONParser().parse(json); if (!data.get("status").equals("ok")) { throw new IOException("acoustid responded with error: " + data.get("status")); @@ -90,7 +104,7 @@ public class AcoustID implements MusicIdentificationService { String artist = (String) ((Map) ((List) recording.get("artists")).get(0)).get("name"); String title = (String) recording.get("title"); - audioTrack = new AudioTrack(artist, title, null); + AudioTrack audioTrack = new AudioTrack(artist, title, null); try { Map releaseGroup = (Map) ((List) recording.get("releasegroups")).get(0); List releases = (List) releaseGroup.get("releases"); @@ -141,8 +155,6 @@ public class AcoustID implements MusicIdentificationService { e.printStackTrace(); // allow album to be null } - - cache.put(url, audioTrack); return audioTrack; } catch (Exception e) { }