!747 m { static new Map cache; // "122" (or "t123") to md5 static long lastUpdate; p { readLocally("cache lastUpdate"); Android3 android = new Android3("Boss Bot."); android.startPort = 4990; makeAndroid3(android); } static synchronized S answer(S s) { new Matches m; if (match3("get cache size", s)) return cache.size() + " entries"; if (match3("when was your last update", s)) return "" + lastUpdate; if (match3("please update", s)) { update(); return "OK, done."; } if (match3("get md5 of *", s, m)) { S md5 = cache.get("" + parseSnippetID(unquote(m.m[0]))); return md5 != null ? format3("MD5: *", md5) : "Snippet not found."; } if (match3("get text of *", s, m) || match3("get text for *", s, m)) { S snippetID = "" + parseSnippetID(unquote(m.m[0])); S md5 = cache.get(snippetID); if (md5 == null) ret "Snippet not found."; S text = ""; if (!md5.equals("-")) text = loadText(snippetID, md5); ret format3("Text: *", text); } if (match3("get transpilation of *", s, m) || match3("get transpilation for *", s, m)) { S snippetID = "t" + parseSnippetID(unquote(m.m[0])); S md5 = cache.get(snippetID); if (md5 == null) ret "Transpilation not found."; S text = ""; if (!md5.equals("-")) text = loadText(snippetID, md5); ret format3("Text: *", text); } ret null; } static synchronized void update() { S s = loadPage("http://tinybrain.de:8080/tb-int/all-md5s.php?withtranspiled=1"); cache.clear(); for (S line : toLinesFullTrim(s)) { S[] tok = line.split(":"); if (tok.length != 2) fail("huch: " + structure(tok)); S snippetID = tok[0]; S md5 = tok[1]; cache.put(snippetID, md5); } saveLocally("cache"); lastUpdate = now(); saveLocally("lastUpdate"); } static File getTextFile(S snippetID, S md5) { return new File(programDir(), snippetID + "-" + md5 + ".txt"); } static synchronized S loadText(S snippetID, S md5) { File f = getTextFile(snippetID, md5); if (f.exists()) return loadTextFile(f); S text = loadTextFromSite(snippetID, md5); saveTextFile(f, text); return text; } static S loadTextFromSite(S snippetID, S md5) { S url; if (snippetID.startsWith("t")) url = "http://tinybrain.de:8080/tb-int/get-transpiled.php?raw=1&withlibs=1&id=" + snippetID.substring(1); else url = "http://tinybrain.de:8080/tb/get-version-raw.php?id=" + parseSnippetID(snippetID) + "&md5=" + urlencode(md5); // TODO: try a few times :) return loadPage(url); } }