Skip to content

Instantly share code, notes, and snippets.

@Arinerron
Last active March 17, 2022 17:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arinerron/24f123f81c95d9622ddff0b8d822d919 to your computer and use it in GitHub Desktop.
Save Arinerron/24f123f81c95d9622ddff0b8d822d919 to your computer and use it in GitHub Desktop.
Detect when a new post is made on @beardog108's blog (https://chaoswebs.net/blog/).
import java.util.*;
import java.util.regex.Pattern;
import java.net.*;
import java.io.*;
public class ChaosWebs {
public static double time = 6 * 60 * 60 * 1000;
public static String filename = ".last_post.txt";
public static void main(String[] args) {
if(args.length != 0)
try {
double time = Double.parseDouble(args[0]) * 60 * 60 * 1000;
} catch(Exception e) {
System.out.println("java ChaosWebs <hours>");
System.exit(0);
}
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
try {
boolean exists = new File(filename).exists();
String top = getPosts().get(0);
String contents = decode(read());
write(encode("" + top));
if(!contents.equals(top) && exists) {
// Put whatever code you want to run when a new post is detected here!
System.out.println(top);
alert("chaoswebs.net Blog Update", "New post on https://chaoswebs.net/blog/ titled \"" + top + "\".");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}, 0, (long) time);
}
public static String encode(String text) throws Exception { // encode post title so malicious code isn't written to the file.
return Arrays.toString(text.getBytes("UTF-8"));
}
public static String decode(String text) throws Exception {
if(text.length() == 0)
return "";
List<Byte> bytes = new ArrayList<>();
for(String s : text.substring(1, text.length() -1).split(Pattern.quote(", ")))
bytes.add((byte)Integer.parseInt(s));
byte[] asdf = new byte[bytes.size()];
int i = 0;
for(byte b : bytes) {
asdf[i] = b;
i++;
}
return new String(asdf);
}
public static List<String> getPosts() throws Exception {
List<String> list = new ArrayList<>();
String html = getText("https://chaoswebs.net/blog/");
int i = 0;
for(String s : html.split(Pattern.quote("<h2 class=\"post-title\"> "))) {
if(i != 0)
list.add(s.split(Pattern.quote(" </h2>"))[0]);
i++;
}
return list;
}
public static String read() throws Exception {
if(!new File(filename).exists()) {
try {new File(filename).createNewFile();} catch(Exception e) {e.printStackTrace();}
return "";
}
BufferedReader br = new BufferedReader(new FileReader(filename));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
return sb.toString();
} finally {
br.close();
}
}
public static void write(String text) {
try{
PrintWriter writer = new PrintWriter(filename, "UTF-8");
writer.println(text);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void alert(String title, String text) {
javax.swing.JOptionPane.showMessageDialog(null, text, title, javax.swing.JOptionPane.WARNING_MESSAGE);
}
public static String getText(String url) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(new URL(url).openConnection().getInputStream()));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
response.append(inputLine);
in.close();
return response.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment