Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BukkitDev Plugin Version Info Fetcher. [REQUIRES] JSOUP-1.11.3
package com.test;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class BukkitSiteUtil {
private static final String GET_BY_NAME = "https://dev.bukkit.org/projects/%s/files?page=%s";
private static final int timeout_seconds = 10;
public static void getVersions(BukkitSiteCallBack<List<BukkitVersion>> callBack, String projectName, Plugin plugin) {
getVersions(callBack, projectName, -1, plugin);
}
public static void getVersions(BukkitSiteCallBack<List<BukkitVersion>> callBack, String projectName, int location_page, Plugin plugin) {
getPage(new BukkitSiteCallBack<Document>() {
@Override
public void callback(boolean success, Document result, Exception exception, int responseCode, String responseMessage) {
if(!success || exception != null || responseCode != HttpsURLConnection.HTTP_OK) {
callBack.callback(false, null, exception, responseCode, responseMessage);
} else {
int pages = getMaxPages(result);
List<BukkitVersion> list = new ArrayList<>();
if(location_page <= pages && location_page > 0) {
getPage(new BukkitSiteCallBack<Document>() {
@Override
public void callback(boolean success, Document result, Exception result_exception, int result_responseCode, String result_responseMessage) {
if(!success || exception != null || responseCode != HttpsURLConnection.HTTP_OK) {
callBack.callback(false, null, result_exception, result_responseCode, result_responseMessage);
} else list.addAll(getVersions(result));
}
}, projectName, location_page, plugin);
callBack.callback(true, list, null, -1, "");
return;
} else if(location_page != -1){
callBack.callback(false, list, null, -1, "");
return;
}
for(int page = 1; page < pages; page++) {
if(page == 1){
list.addAll(getVersions(result));
} else {
getPage(new BukkitSiteCallBack<Document>() {
@Override
public void callback(boolean success, Document result, Exception result_exception, int result_responseCode, String result_responseMessage) {
if(!success || exception != null || responseCode != HttpsURLConnection.HTTP_OK) {
callBack.callback(false, null, result_exception, result_responseCode, result_responseMessage);
} else list.addAll(getVersions(result));
}
}, projectName, page, plugin);
}
}
callBack.callback(true, list, null, responseCode, responseMessage);
}
}
}, projectName, 1, plugin);
}
private static List<BukkitVersion> getVersions(Document doc) {
List<BukkitVersion> list = new ArrayList<>();
for(Element element : doc.getElementsByClass("project-file-list-item")) {
list.add(new BukkitVersion(element));
}
return list;
}
private static void getPage(BukkitSiteCallBack<Document> callBack, String projectName, int page, Plugin plugin) {
new BukkitRunnable() {
@Override
public void run() {
HttpsURLConnection connection = null;
InputStream is = null;
try {
connection = (HttpsURLConnection) new URL(String.format(GET_BY_NAME, projectName, page)).openConnection();
connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36");
connection.setRequestMethod("GET");
connection.setReadTimeout(1000*timeout_seconds);
int responseCode = connection.getResponseCode();
String responseMessage = connection.getResponseMessage();
if(responseCode != HttpsURLConnection.HTTP_OK) {
new BukkitRunnable() {
@Override
public void run() {
callBack.callback(false, null, null, responseCode, responseMessage);
}
}.runTask(plugin);
} else {
List<Byte> result = new ArrayList<Byte>();
int read;
is = connection.getInputStream();
while((read = is.read()) != -1) {
result.add((byte) read);
}
new BukkitRunnable() {
@Override
public void run() {
callBack.callback(true, Jsoup.parse(new String(to_byte_array(result))), null, responseCode, responseMessage);
}
}.runTask(plugin);
}
} catch(Exception e) {
new BukkitRunnable() {
@Override
public void run() {
callBack.callback(false, null, e, -1, "");
}
}.runTask(plugin);
} finally {
if(connection != null) {
connection.disconnect();
}
if(is != null) {
try {
is.close();
} catch (IOException e) {}
}
}
}
}.runTaskAsynchronously(plugin);
}
private static int getMaxPages(Document document) {
if(document.getElementsByClass("paging-list").size() == 0) return 1;
final String page = document.getElementsByClass("paging-list").get(0).getElementsByClass("b-pagination-item").last().getAllElements().get(1).attr("href");
return Integer.parseInt(page.substring(page.indexOf("=")+1));
}
private static byte[] to_byte_array(List<Byte> list) {
byte[] arr = new byte[list.size()];
for(int x = 0; x < list.size(); x++) {
arr[x] = list.get(x);
}
return arr;
}
public static interface BukkitSiteCallBack<T> {
void callback(boolean success, T result, Exception exception, int responseCode, String responseMessage);
}
public static class BukkitVersion {
private String releasetype;
private String downloads;
private String bukkit_version;
private String[] bukkit_versions;
private String file_size;
private String upload_date;
private String upload_date_formatted;
private String upload_date_timestamp;
private String download_url;
private String display_name;
private String file_name;
private String file_id;
public BukkitVersion(String releasetype, String downloads, String bukkit_version, String[] bukkit_versions, String file_size, String upload_date, String upload_date_formatted, String upload_date_timestamp) {
this.releasetype = releasetype;
this.downloads = downloads;
this.bukkit_version = bukkit_version;
this.bukkit_versions = bukkit_versions;
this.file_size = file_size;
this.upload_date = upload_date;
this.upload_date_formatted = upload_date_formatted;
this.upload_date_timestamp = upload_date_timestamp;
}
public BukkitVersion() {}
public BukkitVersion(Element element) {
this.releasetype = element.getElementsByClass("project-file-release-type").get(0).getAllElements().get(1).attr("title");
this.downloads = element.getElementsByClass("project-file-downloads").text();
this.bukkit_version = element.getElementsByClass("version-label").text();
this.bukkit_versions = element.getElementsByClass("additional-versions").size() == 0 ? new String[] {bukkit_version} : element.getElementsByClass("additional-versions").attr("title").replaceAll("<[/|d]\\w+>", ",").replace(",,", ",").substring(1).split(",");
this.file_size = element.getElementsByClass("project-file-size").text();
final Element raw_upload_date = element.getElementsByClass("project-file-date-uploaded").get(0).getAllElements().get(1);
this.upload_date = raw_upload_date.attr("title");
this.upload_date_timestamp = raw_upload_date.attr("data-epoch");
this.upload_date_formatted = raw_upload_date.text();
final Element info = element.getElementsByClass("project-file-name-container").get(0).getAllElements().get(1);
this.download_url = "https://dev.bukkit.org" + info.attr("href") + "/download";
this.display_name = info.text();
this.file_id = info.attr("data-id");
this.file_name = info.attr("data-name");
}
public String getReleasetype() {
return releasetype;
}
public void setReleasetype(String releasetype) {
this.releasetype = releasetype;
}
public String getDownloads() {
return downloads;
}
public void setDownloads(String downloads) {
this.downloads = downloads;
}
public String getBukkit_version() {
return bukkit_version;
}
public void setBukkit_version(String bukkit_version) {
this.bukkit_version = bukkit_version;
}
public String[] getBukkit_versions() {
return bukkit_versions;
}
public void setBukkit_versions(String[] bukkit_versions) {
this.bukkit_versions = bukkit_versions;
}
public String getFile_size() {
return file_size;
}
public void setFile_size(String file_size) {
this.file_size = file_size;
}
public String getUpload_date() {
return upload_date;
}
public void setUpload_date(String upload_date) {
this.upload_date = upload_date;
}
public String getUpload_date_formatted() {
return upload_date_formatted;
}
public void setUpload_date_formatted(String upload_date_formatted) {
this.upload_date_formatted = upload_date_formatted;
}
public String getUpload_date_timestamp() {
return upload_date_timestamp;
}
public void setUpload_date_timestamp(String upload_date_timestamp) {
this.upload_date_timestamp = upload_date_timestamp;
}
public String getDownload_url() {
return download_url;
}
public void setDownload_url(String download_url) {
this.download_url = download_url;
}
public String getDisplay_name() {
return display_name;
}
public void setDisplay_name(String display_name) {
this.display_name = display_name;
}
public String getFile_name() {
return file_name;
}
public void setFile_name(String file_name) {
this.file_name = file_name;
}
public String getFile_id() {
return file_id;
}
public void setFile_id(String file_id) {
this.file_id = file_id;
}
@Override
public String toString() {
return "BukkitVersions [releasetype=" + releasetype + ", downloads=" + downloads + ", bukkit_version="
+ bukkit_version + ", bukkit_versions=" + Arrays.toString(bukkit_versions) + ", file_size="
+ file_size + ", upload_date=" + upload_date + ", upload_date_formatted=" + upload_date_formatted
+ ", upload_date_timestamp=" + upload_date_timestamp + ", download_url=" + download_url
+ ", display_name=" + display_name + ", file_name=" + file_name + ", file_id=" + file_id + "]";
}
public String toJsonString() throws Exception {
Class<?> c = this.getClass();
StringBuilder string = new StringBuilder("{\n \""+c.getSimpleName() +"\""+ ": {\n");
Field[] fields = c.getDeclaredFields();
Field field;
for(int x = 0; x < fields.length; x++) {
field = fields[x];
Object value = field.get(this);
string.append(String.format(" \"%s\": \"%s\",\n", field.getName(), value.getClass().isArray() ? Arrays.asList((Object[])value) : value));
if(x+1 == fields.length) string.setLength(string.length()-2);
}
string.append("\n }\n}");
return string.toString();
}
public String toFancyString() throws Exception {
Class<?> c = this.getClass();
StringBuilder string = new StringBuilder(c.getSimpleName() + ": {\n");
for(Field field : c.getDeclaredFields()) {
Object value = field.get(this);
string.append(String.format(" %s=%s,\n", field.getName(), value.getClass().isArray() ? Arrays.asList((Object[])value) : value));
}
string.append("}");
return string.toString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.