Implement Github API file size detection.

dev
sigonasr2 7 years ago
parent 254d82d3e2
commit 57932c58a3
  1. BIN
      sigIRCv2.jar
  2. 1
      src/sig/UpdateEvent.java
  3. 2
      src/sig/modules/ChatLogModule.java
  4. 15
      src/sig/sigIRC.java
  5. 20
      src/sig/utils/FileUtils.java
  6. 35
      src/sig/utils/GithubUtils.java

Binary file not shown.

@ -38,6 +38,7 @@ public class UpdateEvent implements ActionListener{
public void run() {
JSONObject data = GetSubEmoteJson();
sigIRC.downloadSubEmotes(data);
sigIRC.subEmotesCompleted=true;
}
};
downloadThread.start();

@ -50,7 +50,7 @@ public class ChatLogModule extends Module{
public void run() {
super.run();
if (delay>0) {
if (delay>0 && sigIRC.subEmotesCompleted) {
delay--;
} else
if (!initialized)

@ -19,6 +19,7 @@ import sig.modules.TwitchModule;
import sig.modules.ChatLog.ChatLogMessage;
import sig.modules.ChatLog.ChatLogTwitchEmote;
import sig.utils.FileUtils;
import sig.utils.GithubUtils;
import sig.utils.TextUtils;
import java.awt.Color;
@ -124,11 +125,13 @@ public class sigIRC{
public final static String SUBEMOTELISTFILE = "sigIRC/subemotes.json";
public static long channel_id = -1;
public static int lastSubEmoteUpdate = -1;
public static boolean autoUpdateProgram = true;
public static int subchannelCount = 0;
public static HashMap<Long,String> subchannelIds = new HashMap<Long,String>();
public static boolean downloadedSubEmotes=false;
public static boolean subEmotesCompleted=false;
static int lastWindowX = 0;
static int lastWindowY = 0;
@ -181,6 +184,7 @@ public class sigIRC{
chatlogmodule_height = config.getInteger("CHATLOG_module_height",312);
chatlogMessageHistory = config.getInteger("CHATLOG_module_MessageHistory",50);
hardwareAcceleration = config.getBoolean("hardware_acceleration",true);
autoUpdateProgram = config.getBoolean("Automatically_Update_Program", true);
lastSubEmoteUpdate = config.getInteger("lastSubEmote_APIUpdate",Calendar.getInstance().get(Calendar.DAY_OF_YEAR));
manager.setClientId("o4c2x0l3e82scgar4hpxg6m5dfjbem");
//System.out.println(manager.auth().hasAccessToken());
@ -229,8 +233,11 @@ public class sigIRC{
manager = new FileManager("sigIRC/logs/",true); manager.verifyAndFetchFileFromServer();
manager = new FileManager("sigIRC/sounds/",true); manager.verifyAndFetchFileFromServer();
//manager = new FileManager("sigIRC/sounds/Glaceon_cry.wav"); manager.verifyAndFetchFileFromServer();
File follower_sounds_folder = new File(BASEDIR+"sigIRC/follower_sounds");
if (!follower_sounds_folder.exists()) {
manager = new FileManager("sigIRC/follower_sounds/Glaceon_cry.wav"); manager.verifyAndFetchFileFromServer();
manager = new FileManager("sigIRC/follower_sounds/README.txt"); manager.verifyAndFetchFileFromServer();
}
manager = new FileManager("sigIRC/record"); manager.verifyAndFetchFileFromServer();
manager = new FileManager("sigIRC/glaceon_follower.png"); manager.verifyAndFetchFileFromServer();
manager = new FileManager("sigIRC/sigIRCicon.png"); manager.verifyAndFetchFileFromServer();
@ -253,9 +260,15 @@ public class sigIRC{
}
private static void DownloadProgramUpdate() {
//System.out.println("Last commit size was "+GithubUtils.getSizeOfFileFromLatestGithubCommit("sigIRCv2.jar")+"B");
if (autoUpdateProgram) {
File updatedir = new File(sigIRC.BASEDIR+"sigIRC/updates/");
updatedir.mkdirs();
File programFile = new File(sigIRC.BASEDIR+"sigIRC/updates/sigIRCv2.jar");
System.out.println("File size is "+programFile.length());
long fileSize = GithubUtils.getSizeOfFileFromLatestGithubCommit("sigIRCv2.jar");
System.out.println("File size on Github is "+fileSize);
if ((programFile.exists() && fileSize!=programFile.length()) || !programFile.exists()) {
try {
if (programFile.exists()) {
programFile.delete();
@ -265,6 +278,8 @@ public class sigIRC{
e.printStackTrace();
}
}
}
}
private static void InitializeModules() {
try {

@ -17,6 +17,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -187,6 +188,10 @@ public class FileUtils {
return readJsonFromUrl(url,null,false);
}
public static JSONArray readJsonArrayFromUrl(String url) throws IOException, JSONException {
return readJsonArrayFromUrl(url,null,false);
}
public static JSONObject readJsonFromFile(String file) throws IOException, JSONException {
InputStream is = new FileInputStream(new File(file));
try {
@ -215,6 +220,21 @@ public class FileUtils {
}
}
public static JSONArray readJsonArrayFromUrl(String url, String file, boolean writeToFile) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
if (writeToFile) {
writetoFile(new String[]{jsonText},file);
}
JSONArray json = new JSONArray(jsonText);
return json;
} finally {
is.close();
}
}
public static void logToFile(String message, String filename) {
File file = new File(filename);
try {

@ -0,0 +1,35 @@
package sig.utils;
import java.io.IOException;
import java.util.Arrays;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class GithubUtils {
public static int getSizeOfFileFromLatestGithubCommit(String filename) {
try {
JSONObject data = FileUtils.readJsonFromUrl("https://api.github.com/repos/sigonasr2/sigIRCv2");
String url = data.getString("commits_url").replace("{/sha}", "");
JSONArray data_array = FileUtils.readJsonArrayFromUrl(url);
JSONObject dat = data_array.getJSONObject(0);
JSONObject datapoint1 = dat.getJSONObject("commit");
datapoint1 = datapoint1.getJSONObject("tree");
url = datapoint1.getString("url");
data = FileUtils.readJsonFromUrl(url);
data_array = data.getJSONArray("tree");
for (int i=0;i<data_array.length();i++) {
JSONObject file_data = data_array.getJSONObject(i);
if (file_data.getString("path").equals(filename)) {
return file_data.getInt("size");
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return -1;
}
}
Loading…
Cancel
Save