diff --git a/sigIRCv2.jar b/sigIRCv2.jar index 7c933c6..c48502d 100644 Binary files a/sigIRCv2.jar and b/sigIRCv2.jar differ diff --git a/src/sig/sigIRC.java b/src/sig/sigIRC.java index a723b95..5a0c57c 100644 --- a/src/sig/sigIRC.java +++ b/src/sig/sigIRC.java @@ -107,7 +107,9 @@ public class sigIRC{ public static int twitchmodule_newfollowerImgLogoSize=32; public static boolean testMode=false; public final static String TWITCHEMOTEURL = "https://static-cdn.jtvnw.net/emoticons/v1/"; + public final static String SUBEMOTELISTFILE = "sigIRC/subemotes.json"; public static long channel_id = -1; + public static int lastSubEmoteUpdate = -1; public static Twitch manager = new Twitch(); @@ -151,9 +153,10 @@ public class sigIRC{ touhoumothermodule_width = config.getInteger("TOUHOUMOTHER_module_width",320); touhoumothermodule_height = config.getInteger("TOUHOUMOTHER_module_height",312); hardwareAcceleration = config.getBoolean("hardware_acceleration",true); + lastSubEmoteUpdate = config.getInteger("lastSubEmote_APIUpdate",Calendar.getInstance().get(Calendar.DAY_OF_YEAR)); manager.setClientId("o4c2x0l3e82scgar4hpxg6m5dfjbem"); //System.out.println(manager.auth().hasAccessToken()); - + DownloadAllRequiredDependencies(); String[] filedata = FileUtils.readFromFile(BASEDIR+"sigIRC/oauthToken.txt"); @@ -170,7 +173,9 @@ public class sigIRC{ public void run() { window = createAndShowGUI(); InitializeModules(); + //System.out.println("Modules initialized."); performTwitchEmoteUpdate(); + //System.out.println("Twitch emote update done."); downloadsComplete=true; } }); @@ -212,6 +217,7 @@ public class sigIRC{ manager = new FileManager("backcolor.png"); manager.verifyAndFetchFileFromServer(); manager = new FileManager("drag_bar.png"); manager.verifyAndFetchFileFromServer(); manager = new FileManager("WSplits"); manager.verifyAndFetchFileFromServer(); + System.out.println("Downloaded Dependencies."); } private static void InitializeModules() { @@ -315,21 +321,50 @@ public class sigIRC{ private static void performTwitchEmoteUpdate() { try { JSONObject twitchemotes = FileUtils.readJsonFromUrl("https://twitchemotes.com/api_cache/v3/global.json"); + System.out.println("Twitch emote Json read."); for (String emotes : twitchemotes.keySet()) { JSONObject emote = twitchemotes.getJSONObject(emotes); int id = emote.getInt("id"); String name = emote.getString("code"); emoticons.add(new Emoticon(name, new URL(TWITCHEMOTEURL+id+"/1.0"))); + System.out.println("Emote "+id+" with name "+name); } - JSONObject subemotes = FileUtils.readJsonFromUrl("https://twitchemotes.com/api_cache/v3/subscriber.json"); - String[] sub_emotes = FileUtils.readFromFile(sigIRC.BASEDIR+"sigIRC/Emotes/subscribers.txt"); - for (String s : sub_emotes) { - if (s.length()>0) { - s=s.trim(); - //System.out.println("Got sub emote info for "+s); - downloadSubEmotes(subemotes,s); + Thread downloadThread = new Thread(){ + public void run() { + JSONObject subemotes = null; + try { + File filer = new File(SUBEMOTELISTFILE); + if (!filer.exists()) { + System.out.println("Local copy of Sub emotes not found. Downloading in background..."); + subemotes = FileUtils.readJsonFromUrl("https://twitchemotes.com/api_cache/v3/subscriber.json",SUBEMOTELISTFILE,true); + } else { + if (lastSubEmoteUpdate == Calendar.getInstance().get(Calendar.DAY_OF_YEAR)) { + System.out.println("Using local copy of Sub emote JSON."); + subemotes = FileUtils.readJsonFromFile(SUBEMOTELISTFILE); + } else { + System.out.println("Local copy of Sub emote JSON out-of-date! Re-downloading in background..."); + subemotes = FileUtils.readJsonFromUrl("https://twitchemotes.com/api_cache/v3/subscriber.json",SUBEMOTELISTFILE,true); + } + } + } catch (JSONException | IOException e) { + e.printStackTrace(); + } + lastSubEmoteUpdate = Calendar.getInstance().get(Calendar.DAY_OF_YEAR); + config.setInteger("lastSubEmote_APIUpdate", lastSubEmoteUpdate); + //System.out.println("Subscriber object: "+subemotes); + String[] sub_emotes = FileUtils.readFromFile(sigIRC.BASEDIR+"sigIRC/Emotes/subscribers.txt"); + //System.out.println("Sub emotes read."); + for (String s : sub_emotes) { + if (s.length()>0) { + s=s.trim(); + System.out.println("Got sub emote info for "+s); + downloadSubEmotes(subemotes,s); + } + } + subemotes=null; } - } + }; + downloadThread.start(); /*JSONObject emotelist = twitchemotes.getJSONObject("emotes"); JSONObject templatelist = twitchemotes.getJSONObject("template"); String templateurl = templatelist.getString("small"); diff --git a/src/sig/utils/FileUtils.java b/src/sig/utils/FileUtils.java index 049481d..c7ff7f3 100644 --- a/src/sig/utils/FileUtils.java +++ b/src/sig/utils/FileUtils.java @@ -60,6 +60,35 @@ public class FileUtils { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONObject json = new JSONObject(jsonText); + jsonText=null; + return json; + } finally { + is.close(); + } + } + + public static JSONObject readJsonFromFile(String file) throws IOException, JSONException { + InputStream is = new FileInputStream(new File(file)); + try { + BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); + String jsonText = readAll(rd); + JSONObject json = new JSONObject(jsonText); + jsonText=null; + return json; + } finally { + is.close(); + } + } + + public static JSONObject readJsonFromUrl(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); + } + JSONObject json = new JSONObject(jsonText); return json; } finally { is.close();