Threaded sub emote downloading so it no longer hangs the program.
Because the JSON file size increased dramatically, cache it and only re-download once a day.
This commit is contained in:
		
							parent
							
								
									3eb312abf8
								
							
						
					
					
						commit
						11f1a40115
					
				
							
								
								
									
										
											BIN
										
									
								
								sigIRCv2.jar
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								sigIRCv2.jar
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							| @ -107,7 +107,9 @@ public class sigIRC{ | |||||||
| 	public static int twitchmodule_newfollowerImgLogoSize=32; | 	public static int twitchmodule_newfollowerImgLogoSize=32; | ||||||
| 	public static boolean testMode=false; | 	public static boolean testMode=false; | ||||||
| 	public final static String TWITCHEMOTEURL = "https://static-cdn.jtvnw.net/emoticons/v1/"; | 	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 long channel_id = -1; | ||||||
|  | 	public static int lastSubEmoteUpdate = -1; | ||||||
| 
 | 
 | ||||||
| 	public static Twitch manager = new Twitch(); | 	public static Twitch manager = new Twitch(); | ||||||
| 	 | 	 | ||||||
| @ -151,6 +153,7 @@ public class sigIRC{ | |||||||
| 		touhoumothermodule_width = config.getInteger("TOUHOUMOTHER_module_width",320); | 		touhoumothermodule_width = config.getInteger("TOUHOUMOTHER_module_width",320); | ||||||
| 		touhoumothermodule_height = config.getInteger("TOUHOUMOTHER_module_height",312); | 		touhoumothermodule_height = config.getInteger("TOUHOUMOTHER_module_height",312); | ||||||
| 		hardwareAcceleration = config.getBoolean("hardware_acceleration",true); | 		hardwareAcceleration = config.getBoolean("hardware_acceleration",true); | ||||||
|  | 		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()); | ||||||
| 
 | 
 | ||||||
| @ -170,7 +173,9 @@ public class sigIRC{ | |||||||
|             public void run() { |             public void run() { | ||||||
|                 window = createAndShowGUI(); |                 window = createAndShowGUI(); | ||||||
|         		InitializeModules(); |         		InitializeModules(); | ||||||
|  |         		//System.out.println("Modules initialized."); | ||||||
|         		performTwitchEmoteUpdate(); |         		performTwitchEmoteUpdate(); | ||||||
|  |         		//System.out.println("Twitch emote update done."); | ||||||
|         		downloadsComplete=true; |         		downloadsComplete=true; | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
| @ -212,6 +217,7 @@ public class sigIRC{ | |||||||
| 		manager = new FileManager("backcolor.png"); manager.verifyAndFetchFileFromServer(); | 		manager = new FileManager("backcolor.png"); manager.verifyAndFetchFileFromServer(); | ||||||
| 		manager = new FileManager("drag_bar.png"); manager.verifyAndFetchFileFromServer(); | 		manager = new FileManager("drag_bar.png"); manager.verifyAndFetchFileFromServer(); | ||||||
| 		manager = new FileManager("WSplits"); manager.verifyAndFetchFileFromServer(); | 		manager = new FileManager("WSplits"); manager.verifyAndFetchFileFromServer(); | ||||||
|  | 		System.out.println("Downloaded Dependencies."); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private static void InitializeModules() { | 	private static void InitializeModules() { | ||||||
| @ -315,21 +321,50 @@ public class sigIRC{ | |||||||
| 	private static void performTwitchEmoteUpdate() { | 	private static void performTwitchEmoteUpdate() { | ||||||
| 		try { | 		try { | ||||||
| 			JSONObject twitchemotes = FileUtils.readJsonFromUrl("https://twitchemotes.com/api_cache/v3/global.json"); | 			JSONObject twitchemotes = FileUtils.readJsonFromUrl("https://twitchemotes.com/api_cache/v3/global.json"); | ||||||
|  | 			System.out.println("Twitch emote Json read."); | ||||||
| 			for (String emotes : twitchemotes.keySet()) { | 			for (String emotes : twitchemotes.keySet()) { | ||||||
| 				JSONObject emote = twitchemotes.getJSONObject(emotes); | 				JSONObject emote = twitchemotes.getJSONObject(emotes); | ||||||
| 				int id = emote.getInt("id"); | 				int id = emote.getInt("id"); | ||||||
| 				String name = emote.getString("code"); | 				String name = emote.getString("code"); | ||||||
| 				emoticons.add(new Emoticon(name, new URL(TWITCHEMOTEURL+id+"/1.0"))); | 				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"); | 			Thread downloadThread = new Thread(){ | ||||||
| 			String[] sub_emotes = FileUtils.readFromFile(sigIRC.BASEDIR+"sigIRC/Emotes/subscribers.txt"); | 				public void run() { | ||||||
| 			for (String s : sub_emotes) { | 					JSONObject subemotes = null; | ||||||
| 				if (s.length()>0) { | 					try { | ||||||
| 					s=s.trim(); | 						File filer = new File(SUBEMOTELISTFILE); | ||||||
| 					//System.out.println("Got sub emote info for "+s); | 						if (!filer.exists()) { | ||||||
| 					downloadSubEmotes(subemotes,s); | 							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 emotelist = twitchemotes.getJSONObject("emotes"); | ||||||
| 			JSONObject templatelist = twitchemotes.getJSONObject("template"); | 			JSONObject templatelist = twitchemotes.getJSONObject("template"); | ||||||
| 			String templateurl = templatelist.getString("small"); | 			String templateurl = templatelist.getString("small"); | ||||||
|  | |||||||
| @ -60,6 +60,35 @@ public class FileUtils { | |||||||
| 	      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); | 	      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); | ||||||
| 	      String jsonText = readAll(rd); | 	      String jsonText = readAll(rd); | ||||||
| 	      JSONObject json = new JSONObject(jsonText); | 	      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; | 	      return json; | ||||||
| 	    } finally { | 	    } finally { | ||||||
| 	      is.close(); | 	      is.close(); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user