diff --git a/sigIRCv2.jar b/sigIRCv2.jar index 3198187..855788f 100644 Binary files a/sigIRCv2.jar and b/sigIRCv2.jar differ diff --git a/src/sig/UpdateEvent.java b/src/sig/UpdateEvent.java index a31d6e9..d4d34c4 100644 --- a/src/sig/UpdateEvent.java +++ b/src/sig/UpdateEvent.java @@ -2,6 +2,14 @@ package sig; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.File; +import java.io.IOException; +import java.util.Calendar; + +import org.json.JSONException; +import org.json.JSONObject; + +import sig.utils.FileUtils; public class UpdateEvent implements ActionListener{ final static int MSGTIMER = 300; @@ -19,7 +27,57 @@ public class UpdateEvent implements ActionListener{ UpdateScrollingText(); UpdateAuthenticationCountdownMessage(); UpdateWindowPosition(); + UpdateSubEmoticons(); + } + } + + private void UpdateSubEmoticons() { + if (!sigIRC.downloadedSubEmotes && + sigIRC.subchannelCount==sigIRC.subchannelIds.size()) { + Thread downloadThread = new Thread(){ + public void run() { + JSONObject data = GetSubEmoteJson(); + sigIRC.downloadSubEmotes(data); + } + }; + downloadThread.start(); + sigIRC.downloadedSubEmotes=true; + } + } + + private JSONObject GetSubEmoteJson() { + JSONObject subemotes = null; + try { + File filer = new File(sigIRC.SUBEMOTELISTFILE); + if (!filer.exists()) { + System.out.println("Local copy of Sub emotes not found. Downloading in background..."); + subemotes = FileUtils.readJsonFromUrlWithFilter("https://twitchemotes.com/api_cache/v3/subscriber.json",sigIRC.subchannelIds,sigIRC.SUBEMOTELISTFILE,true); + } else { + if (sigIRC.lastSubEmoteUpdate == Calendar.getInstance().get(Calendar.DAY_OF_YEAR)) { + System.out.println("Using local copy of Sub emote JSON."); + subemotes = FileUtils.readJsonFromFileWithFilter(sigIRC.SUBEMOTELISTFILE,sigIRC.subchannelIds); + } else { + System.out.println("Local copy of Sub emote JSON out-of-date! Re-downloading in background..."); + subemotes = FileUtils.readJsonFromFileWithFilter(sigIRC.SUBEMOTELISTFILE,sigIRC.subchannelIds); + new Thread(){ + public void run() { + try { + FileUtils.readJsonFromUrlWithFilter("https://twitchemotes.com/api_cache/v3/subscriber.json",sigIRC.subchannelIds,sigIRC.SUBEMOTELISTFILE,true); + } catch (JSONException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }.start(); + } + } + } catch (JSONException | IOException e) { + e.printStackTrace(); } + sigIRC.lastSubEmoteUpdate = Calendar.getInstance().get(Calendar.DAY_OF_YEAR); + sigIRC.config.setInteger("lastSubEmote_APIUpdate", sigIRC.lastSubEmoteUpdate); + return subemotes; } private void UpdateWindowPosition() { diff --git a/src/sig/sigIRC.java b/src/sig/sigIRC.java index a83eb10..0aaae4d 100644 --- a/src/sig/sigIRC.java +++ b/src/sig/sigIRC.java @@ -42,6 +42,7 @@ import java.net.URL; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Calendar; +import java.util.HashMap; import java.util.List; import javax.imageio.ImageIO; @@ -121,6 +122,11 @@ public class sigIRC{ public static long channel_id = -1; public static int lastSubEmoteUpdate = -1; + public static int subchannelCount = 0; + public static HashMap subchannelIds = new HashMap(); + + public static boolean downloadedSubEmotes=false; + static int lastWindowX = 0; static int lastWindowY = 0; @@ -310,9 +316,8 @@ public class sigIRC{ } } - private static void downloadSubEmotes(JSONObject emotes, String channelName) { + private static void getSubChannels(String channelName) { manager.channels().get(channelName, new ChannelResponseHandler() { - @Override public void onFailure(Throwable arg0) { } @@ -323,23 +328,8 @@ public class sigIRC{ @Override public void onSuccess(Channel arg0) { - if (arg0!=null) { - channel_id=arg0.getId(); - JSONObject channel = emotes.getJSONObject(Long.toString(channel_id)); - JSONArray arr = channel.getJSONArray("emotes"); - //System.out.println("Channel: "+channel); - for (int i=0;i0) { s=s.trim(); - System.out.println("Got sub emote info for "+s); - downloadSubEmotes(subemotes,s); + //System.out.println("Got sub emote info for "+s); + getSubChannels(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 6e79c78..7a5a267 100644 --- a/src/sig/utils/FileUtils.java +++ b/src/sig/utils/FileUtils.java @@ -14,6 +14,7 @@ import java.net.URL; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import org.json.JSONException; @@ -55,12 +56,110 @@ public class FileUtils { } return sb.toString(); } + + private static String readFilter(Reader rd, HashMap channel_ids) throws IOException { + StringBuilder sb = new StringBuilder(); + boolean allowed=false; + boolean quotation_mark=false; + boolean endquotation_mark=false; + boolean foundChannel=false; + boolean nextBrace=false; + boolean outputStuff=false; + String numb = ""; + int braceCount=0; + int channelCount=0; + int vals=0; + int cp; + while ((cp = rd.read()) != -1) { + if (braceCount==0) { + allowed=true; + } else + if (braceCount==1 && !quotation_mark){ + quotation_mark=true; + numb=""; + allowed=false; + } else + if (!endquotation_mark) { + if ((char)cp >= '0' && + (char)cp <= '9') { + allowed=false; + numb+=(char)cp; + } else { + allowed=false; + endquotation_mark=true; + try { + if (channel_ids.containsKey(Long.parseLong(numb))) { + if (channelCount>=1) { + sb.append(","); + } + sb.append("\""+numb+"\""); + foundChannel=true; + System.out.println("Found channel "+numb); + outputStuff=true; + } + } catch (NumberFormatException e) { + + } + } + } else + if (!nextBrace && foundChannel) { + allowed=true; + if ((char)cp == '{') { + nextBrace=true; + } + } else + if (foundChannel) { + allowed=true; + if (braceCount==1) { + allowed=false; + channelCount++; + quotation_mark=false; + endquotation_mark=false; + foundChannel=false; + nextBrace=false; + } + } else { + allowed=false; + if (braceCount==1) { + allowed=false; + quotation_mark=false; + endquotation_mark=false; + foundChannel=false; + nextBrace=false; + } + } + + /*if (outputStuff && vals++<1000) { + System.out.print((char)cp); + }*/ + if ((char)cp == '{') { + braceCount++; + //System.out.println("Brace count is "+braceCount+"."); + } else + if ((char)cp == '}') { + braceCount--; + //System.out.println("Brace count is "+braceCount+"."); + } + + if (allowed) { + sb.append((char) cp); + } + } + sb.append("}"); + // System.out.println("============="); + System.out.println(sb.toString()); + return sb.toString(); + } - public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { - InputStream is = new URL(url).openStream(); + public static JSONObject readJsonFromUrlWithFilter(String url, HashMap filter) throws IOException, JSONException { + return readJsonFromUrlWithFilter(url,filter,null,false); + } + + public static JSONObject readJsonFromFileWithFilter(String file, HashMap filter) 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); + String jsonText = readFilter(rd,filter); JSONObject json = new JSONObject(jsonText); jsonText=null; return json; @@ -69,6 +168,25 @@ public class FileUtils { } } + public static JSONObject readJsonFromUrlWithFilter(String url, HashMap filter, 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 = readFilter(rd,filter); + if (writeToFile) { + writetoFile(new String[]{jsonText},file); + } + JSONObject json = new JSONObject(jsonText); + return json; + } finally { + is.close(); + } + } + + public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { + return readJsonFromUrl(url,null,false); + } + public static JSONObject readJsonFromFile(String file) throws IOException, JSONException { InputStream is = new FileInputStream(new File(file)); try {