mirror of
https://github.com/Anvilcraft/modpacktools
synced 2024-06-02 10:39:59 +02:00
implemented modlist
This commit is contained in:
parent
05d4919bc4
commit
a58a45ead0
|
@ -4,7 +4,7 @@ plugins {
|
|||
}
|
||||
|
||||
group 'ley.anvil'
|
||||
version '1.0-SNAPSHOT'
|
||||
version '1.1-SNAPSHOT'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
|
||||
|
@ -16,6 +16,7 @@ repositories {
|
|||
dependencies {
|
||||
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
|
||||
compile "com.github.TheRandomLabs:CurseAPI:master-SNAPSHOT"
|
||||
compile 'org.apache.commons:commons-csv:1.8'
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,20 @@
|
|||
package ley.anvil.modpacktools;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import ley.anvil.modpacktools.util.Util;
|
||||
import org.apache.commons.csv.CSVFormat;
|
||||
import org.apache.commons.csv.CSVPrinter;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
|
||||
public class Commands {
|
||||
|
||||
/**
|
||||
|
@ -68,7 +83,116 @@ public class Commands {
|
|||
* @param format Can be html or csv
|
||||
*/
|
||||
public static void createModlist(String[] format) {
|
||||
if(format[1].equalsIgnoreCase("csv")) {
|
||||
File csvFile = new File(Main.CONFIG.CONFIG.get("csvExportFile").getAsString());
|
||||
if(csvFile.exists()) {
|
||||
System.out.println("Delete " + csvFile);
|
||||
return;
|
||||
}
|
||||
System.out.println("Printing CSV into " + csvFile);
|
||||
Appendable out;
|
||||
CSVFormat format1;
|
||||
try(CSVPrinter printer = new CSVPrinter(new FileWriter(csvFile), CSVFormat.EXCEL.withDelimiter(';'))) {
|
||||
printer.printRecord("Name", "Authors", "Link", "Downloads", "ID");
|
||||
printer.println();
|
||||
ArrayList<ModInfo> modlist = getModInfo();
|
||||
Collections.sort(modlist, Comparator.comparing(ModInfo :: getName));
|
||||
for(ModInfo mod : modlist) {
|
||||
String name = mod.getName();
|
||||
String[] authorArr = mod.getAuthors();
|
||||
String link = mod.getLink();
|
||||
int downloads = mod.getDownloads();
|
||||
int id = mod.getId();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(String author : authorArr) {
|
||||
sb.append(author);
|
||||
sb.append(", ");
|
||||
}
|
||||
String authors = sb.toString();
|
||||
authors = authors.substring(0, authors.length() - 2);
|
||||
|
||||
printer.printRecord(name, authors, link, downloads, id);
|
||||
}
|
||||
}catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if(format[1].equalsIgnoreCase("html")) {
|
||||
//TODO implement html mod list
|
||||
}else {
|
||||
System.out.println("Expected Either HTML or CSV as format");
|
||||
}
|
||||
}
|
||||
private class ModInfo {
|
||||
private String name;
|
||||
private JsonArray authors;
|
||||
@SerializedName("websiteUrl")
|
||||
private String link;
|
||||
@SerializedName("downloadCount")
|
||||
private int downloads;
|
||||
private int id;
|
||||
|
||||
private ModInfo(String name, JsonArray authors, String link, int downloads, int id) {
|
||||
this.name = name;
|
||||
this.authors = authors;
|
||||
this.link = link;
|
||||
this.downloads = downloads;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String[] getAuthors() {
|
||||
ArrayList<String> authorArr = new ArrayList<>();
|
||||
for(JsonElement author : authors) {
|
||||
JsonObject authorObj = (JsonObject) author;
|
||||
authorArr.add(authorObj.get("name").getAsString());
|
||||
}
|
||||
return authorArr.toArray(new String[authorArr.size()]);
|
||||
}
|
||||
|
||||
public String getLink() {
|
||||
return link;
|
||||
}
|
||||
|
||||
public int getDownloads() {
|
||||
return downloads;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
private static ArrayList<ModInfo> getModInfo() {
|
||||
try {
|
||||
System.out.println("Getting Info From Curse API");
|
||||
File manifestFile = new File(Main.CONFIG.JAR_LOCATION, Main.CONFIG.CONFIG.get("manifestFile").getAsString());
|
||||
//Read manifest
|
||||
JsonObject manifest = Util.readJsonFile(manifestFile);
|
||||
JsonArray files = manifest.getAsJsonArray("files");
|
||||
|
||||
ArrayList<Integer> fileIds = new ArrayList<>();
|
||||
for(JsonElement file : files) {
|
||||
fileIds.add(((JsonObject) file).get("projectID").getAsInt());
|
||||
}
|
||||
String responseStr = Util.httpPostString(new URL("https://addons-ecs.forgesvc.net/api/v2/addon"),
|
||||
fileIds.toString(),
|
||||
"application/json; utf-8",
|
||||
"application/json");
|
||||
JsonArray response = (JsonArray) JsonParser.parseString(responseStr);
|
||||
|
||||
ArrayList<ModInfo> modInfos = new ArrayList<>();
|
||||
Gson gson = new Gson();
|
||||
for(JsonElement mod : response) {
|
||||
modInfos.add(gson.fromJson(mod, ModInfo.class));
|
||||
}
|
||||
return modInfos;
|
||||
} catch(MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//Commands for users (available outside a modpack dev environment)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package ley.anvil.modpacktools.util;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URISyntaxException;
|
||||
|
@ -41,18 +40,7 @@ public class Config {
|
|||
private JsonObject readConfig() {
|
||||
if(CONFIG_LOCATION.exists()) {
|
||||
//parse file to json
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new FileReader(CONFIG_LOCATION));
|
||||
String inputLine;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
while((inputLine = br.readLine()) != null) {
|
||||
sb.append(inputLine);
|
||||
}
|
||||
br.close();
|
||||
return (JsonObject) JsonParser.parseString(sb.toString());
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Util.readJsonFile(CONFIG_LOCATION);
|
||||
}else {
|
||||
//copy from resources
|
||||
try {
|
||||
|
|
63
src/main/java/ley/anvil/modpacktools/util/Util.java
Normal file
63
src/main/java/ley/anvil/modpacktools/util/Util.java
Normal file
|
@ -0,0 +1,63 @@
|
|||
package ley.anvil.modpacktools.util;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class Util {
|
||||
/**
|
||||
* Reads a Json File
|
||||
* @param file the file to read
|
||||
* @return the file content as JsonObject
|
||||
*/
|
||||
public static JsonObject readJsonFile(File file) {
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new FileReader(file));
|
||||
String inputLine;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
while((inputLine = br.readLine()) != null) {
|
||||
sb.append(inputLine);
|
||||
}
|
||||
br.close();
|
||||
return (JsonObject) JsonParser.parseString(sb.toString());
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* sends a http post request
|
||||
* @param url the url to send the request to
|
||||
* @param contentType what content type should be used. Example: "application/json; utf-8"
|
||||
* @param accept what content is accepted. Example: "application/json"
|
||||
* @return the response as string
|
||||
*/
|
||||
public static String httpPostString(URL url, String payload, String contentType, String accept) {
|
||||
try {
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("POST");
|
||||
con.setRequestProperty("Content-Type", contentType);
|
||||
con.setRequestProperty("Accept", accept);
|
||||
con.setDoOutput(true);
|
||||
|
||||
OutputStream outs = con.getOutputStream();
|
||||
String s;
|
||||
byte[] input = payload.getBytes("UTF-8");
|
||||
outs.write(input);
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
|
||||
StringBuffer sb = new StringBuffer();
|
||||
String responseLine;
|
||||
while((responseLine = br.readLine()) != null) {
|
||||
sb.append(responseLine.trim());
|
||||
}
|
||||
return sb.toString();
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}return null;
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
{
|
||||
|
||||
"csvExportFile": "mods.csv",
|
||||
"manifestFile": "src/twitch/manifest.json"
|
||||
}
|
Loading…
Reference in a new issue