127 lines
4.8 KiB
Java
127 lines
4.8 KiB
Java
package me.neurodock.genius;
|
|
|
|
import io.github.classgraph.ClassGraph;
|
|
import io.github.classgraph.ClassInfo;
|
|
import io.github.classgraph.ClassInfoList;
|
|
import io.github.classgraph.ScanResult;
|
|
import me.neurodock.core.Core;
|
|
import me.neurodock.ollama.OllamaFunctionTool;
|
|
import me.neurodock.ollama.OllamaFunctionTools;
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.Map;
|
|
|
|
public class GeniusTools {
|
|
|
|
public final String Client_ID;
|
|
public final String Client_Secret;
|
|
public final String Access_Token;
|
|
public final String BaseURL = "https://api.genius.com";
|
|
public final OllamaFunctionTools GeniusTools;
|
|
public final File CacheFile = new File(Core.DATA_DIR + "/genius_cache.json");
|
|
public JSONObject CacheData;
|
|
|
|
public GeniusTools() {
|
|
super();
|
|
try {
|
|
JSONObject obj = new JSONObject(Files.readString(Path.of(Core.DATA_DIR + "/geniusapi.json")));
|
|
this.Client_ID = obj.getString("client_id");
|
|
this.Client_Secret = obj.getString("client_secret");
|
|
this.Access_Token = obj.getString("access_token");
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
|
|
if(CacheFile.exists()) {
|
|
try{
|
|
CacheData = new JSONObject(Files.readString(CacheFile.toPath()));
|
|
}catch (IOException ex)
|
|
{
|
|
ex.printStackTrace();
|
|
if(CacheData == null)
|
|
CacheData = new JSONObject();
|
|
CacheFile.delete();
|
|
}
|
|
}
|
|
else {
|
|
CacheData = new JSONObject();
|
|
}
|
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
try {
|
|
Files.writeString(CacheFile.toPath(), CacheData.toString());
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}));
|
|
|
|
OllamaFunctionTools.OllamaFunctionToolsBuilder builder = new OllamaFunctionTools.OllamaFunctionToolsBuilder();
|
|
|
|
try(ScanResult scanResult = new ClassGraph().enableAllInfo().acceptPackages("me.zacharias.neuro.dock.genius.endpoints").scan()) {
|
|
ClassInfoList endpoints = scanResult.getClassesWithAnnotation(GeniusEndpoint.class.getName());
|
|
for (ClassInfo classInfo : endpoints) {
|
|
Class<?> clazz = classInfo.loadClass();
|
|
if (OllamaFunctionTool.class.isAssignableFrom(clazz)) {
|
|
//System.out.println("Found endpoint: " + clazz.getName() + " With constructor: " + clazz.getDeclaredConstructors().f.getName() + " and arguments: " + Arrays.toString(clazz.getDeclaredConstructor().getParameterTypes()));
|
|
GeniusEndpointTool tool = (GeniusEndpointTool) clazz.getDeclaredConstructor(GeniusTools.class).newInstance(this);
|
|
builder.addTool(tool, Core.Source.CTP);
|
|
}
|
|
}
|
|
} catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
|
|
this.GeniusTools = builder.build();
|
|
}
|
|
|
|
public OllamaFunctionTools getGeniusTools() {
|
|
return this.GeniusTools;
|
|
}
|
|
|
|
public JSONObject getGeniusEndpoint(String endpoint, Map<String, String> params) {
|
|
try {
|
|
URL url = new URL(this.BaseURL + endpoint + "?" + ParameterStringBuilder.getParamsString(params));
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
conn.setRequestMethod("GET");
|
|
conn.setRequestProperty("Authorization", "Bearer " + this.Access_Token);
|
|
conn.setRequestProperty("Accept", "application/json");
|
|
|
|
if (conn.getResponseCode() != 200) {
|
|
throw new IOException("Failed to connect to Genius API: " + conn.getResponseCode());
|
|
}
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
String inputLine;
|
|
StringBuilder response = new StringBuilder();
|
|
|
|
while ((inputLine = in.readLine()) != null) {
|
|
response.append(inputLine);
|
|
}
|
|
in.close();
|
|
|
|
return new JSONObject(response.toString());
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public String hasCache(int song_id) {
|
|
if(CacheData.has(Integer.toString(song_id)))
|
|
return CacheData.getString(Integer.toString(song_id));
|
|
return null;
|
|
}
|
|
|
|
public void cacheLyrics(int song_id, String lyrics) {
|
|
CacheData.put(Integer.toString(song_id), lyrics);
|
|
}
|
|
}
|