Implement Github API file size detection.

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

Binary file not shown.

@ -15,7 +15,7 @@ public class UpdateEvent implements ActionListener{
final static int MSGTIMER = 300; final static int MSGTIMER = 300;
final static int AUTOSAVETIMER = 600; final static int AUTOSAVETIMER = 600;
int last_authentication_msg = MSGTIMER; int last_authentication_msg = MSGTIMER;
int last_autosave = AUTOSAVETIMER; int last_autosave = AUTOSAVETIMER;
long lasttime = System.currentTimeMillis(); long lasttime = System.currentTimeMillis();
int avgfps = 30; int avgfps = 30;
int counter = 0; int counter = 0;
@ -38,6 +38,7 @@ public class UpdateEvent implements ActionListener{
public void run() { public void run() {
JSONObject data = GetSubEmoteJson(); JSONObject data = GetSubEmoteJson();
sigIRC.downloadSubEmotes(data); sigIRC.downloadSubEmotes(data);
sigIRC.subEmotesCompleted=true;
} }
}; };
downloadThread.start(); downloadThread.start();

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

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

@ -17,6 +17,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -186,6 +187,10 @@ public class FileUtils {
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
return readJsonFromUrl(url,null,false); 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 { public static JSONObject readJsonFromFile(String file) throws IOException, JSONException {
InputStream is = new FileInputStream(new File(file)); InputStream is = new FileInputStream(new File(file));
@ -214,6 +219,21 @@ public class FileUtils {
is.close(); is.close();
} }
} }
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) { public static void logToFile(String message, String filename) {
File file = new File(filename); File file = new File(filename);

@ -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