This commit is contained in:
2022-11-30 00:06:57 -06:00
parent ecf1907b3e
commit 82d8e6f8ca
31 changed files with 280 additions and 16 deletions

View File

@@ -0,0 +1,51 @@
package com.calebfontenot;
import org.json.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class Common {
public static List<String> parse(String fetchURL, String keyToFetch) throws Exception {
//System.out.println("CURLing API...");
URL url = new URL(fetchURL);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
for (String line; (line = reader.readLine()) != null; ) {
JSONObject json = new JSONObject(line);
//System.out.println(line);
JSONArray key = json.getJSONArray(keyToFetch);
List<String> returnList = new ArrayList<String>();
for (int i = 0; i < key.length() - 1; i++) {
String returnString = (String) key.get(i);
returnList.add(returnString);
}
return returnList;
}
} catch (Exception IllegalArgumentException) {
return null;
}
return null;
}
public static String rawFetch(String fetchURL) throws Exception {
//System.out.println("CURLing API...");
URL url = new URL(fetchURL);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
for (String line; (line = reader.readLine()) != null; ) {
//JSONObject json = new JSONObject(line);
//System.out.println(line);
return line;
}
} catch (Exception IllegalArgumentException) {
System.out.println("Illegal Argument!");
IllegalArgumentException.printStackTrace();
return null;
}
return null;
}
}

View File

@@ -1,28 +1,87 @@
package com.calebfontenot;
import java.net.*;
import java.io.*;
import com.calebfontenot.parsers.Velocity;
import java.util.*;
import org.json.*;
public class Main {
//TODO: Move these into the parser classes?
public static final String PAPER_API_URL = "https://api.papermc.io/v2";
public static void main(String[] args) throws Exception {
for (int i = 0; i < args.length; i++) {
System.out.println("args used: " + args[i]);
}
System.out.println("CURLing API...");
URL url = new URL("https://api.papermc.io/v2/projects");
// Check if user passed arguments, if none have been passed, go into interactive mode:
if (args.length == 0) {
menu();
} else if (args[0].equals("download")) {
if (args[1].equals("modrinth")) {
Velocity.fetchVersions(args[2]);
}
}
/*
List<String> projects = Common.parse("https://api.papermc.io/v2/projects", "projects");
System.out.println("The projects returned by Common.parse() are: ");
for(String projectNames:projects) {
System.out.println(projectNames);
}
*/
}
public static void menu() throws Exception {
Scanner input = new Scanner(System.in);
int userInput;
System.out.println("No arguments passed, entering interactive mode!");
System.out.println("Note: Typing '-1' at any point in this menu will exit the program.");
do {
System.out.println("What would you like to do?");
System.out.println("1. Download a jar file from a parser.");
System.out.println("2. Obtain information from an API.");
userInput = input.nextInt();
if (userInput == 1) { // Download
} else if (userInput == 2) { // Obtain info
System.out.println("Selected: Obtain info");
System.out.println("Select which parser to use:");
System.out.println("1. PaperMC (Not finished)");
System.out.println("2. Modrinth");
userInput = input.nextInt();
if (userInput == 1) { // user has selected PaperMC
System.out.println("Available Projects: ");
List<String> projects = Common.parse(PAPER_API_URL + "/projects", "projects");
int i = 1;
for (String projectNames : projects) {
System.out.println(i + ". " + projectNames);
i++;
}
System.out.print("Select project: ");
userInput = input.nextInt();
System.out.println(Common.rawFetch(PAPER_API_URL + "/projects/" + projects.get(userInput - 1)));
}
if (userInput == 2) {
System.out.println("What would you like to do?");
System.out.println("1. Search for mods");
System.out.println("2. Get info about a slug/ID");
userInput = input.nextInt();
if(userInput == 1) {
System.out.print("Enter query:");
String inputString = input.next();
System.out.println(Common.rawFetch(com.calebfontenot.parsers.Velocity.MODRINTH_API_URL + "/search?query=" + inputString));
}
if(userInput == 2) {
System.out.print("Enter slug/ID: ");
String inputString = input.next();
System.out.println(Common.rawFetch(com.calebfontenot.parsers.Velocity.MODRINTH_API_URL + "/project/" + inputString + "/version"));
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
for (String line; (line = reader.readLine()) != null; ) {
JSONObject json = new JSONObject(line);
System.out.println(line);
JSONArray projects = json.getJSONArray("projects");
for (int i = 0; i < projects.length() - 1; i++) {
System.out.println(projects.get(i));
}
}
}
} while (userInput != -1);
}
}

View File

@@ -0,0 +1,8 @@
package com.calebfontenot.parsers;
public class PaperMC {
public static String download() {
return "";
}
}

View File

@@ -0,0 +1,10 @@
package com.calebfontenot.parsers;
import com.calebfontenot.Common;
public class Velocity {
public static final String MODRINTH_API_URL = "https://api.modrinth.com/v2";
public static void fetchVersions(String modID) throws Exception { // Fetch information about mod versions from the API.
System.out.println(Common.rawFetch(MODRINTH_API_URL + "/project/" + modID + "/version"));
}
}