Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.PrintStream;
- import java.net.URL;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import java.util.regex.*;
- public class ThreadNecromancyScorer {
- static long parse(String datetime){
- long d=0;
- try {
- d=new SimpleDateFormat("MMM dd, yyyy, hh:mm:ss a zzzz").parse(datetime+" AEST").getTime();
- } catch (ParseException e) {
- RuntimeException r=new RuntimeException();
- r.initCause(e);
- throw r;
- }
- return d;
- }
- public static void main(String[]args) throws IOException{
- URL url=new URL("https://tbgforums.com/forums/index.php?topic=2111");
- Scanner s=new Scanner(url.openStream());
- String htmlSource=s.useDelimiter("\\Z").next().replaceAll("\n","").replaceAll("\\s+"," ").replaceAll(".+<body.+?>(.+)</body>.+","$1");
- System.setOut(new PrintStream("out.txt"));
- s.close();
- Matcher m=Pattern.compile("<a class=\"nav_page\" href=\"https://tbgforums.com/forums/[^<>]+?\">([0-9]+)</a>").matcher(htmlSource);
- int lastPage=0;
- while(m.find()){
- lastPage=Integer.parseInt(m.group(1));
- }
- List<String>posters=new LinkedList<>();
- List<String>timestamps=new LinkedList<>();
- for(int i=0;i<lastPage;i++){
- System.err.printf("Fetching page %s/%s\n",i+1,lastPage);
- url=new URL("https://tbgforums.com/forums/index.php?topic=2111."+(25*i));
- s=new Scanner(url.openStream());
- htmlSource=s.useDelimiter("\\Z").next().replaceAll("\n","").replaceAll("\\s+"," ").replaceAll(".+<body.+?>(.+)</body>.+","$1");
- m=Pattern.compile("<div class=\"poster\"> <h4> <a href=\"https://tbgforums.com/.+?\">(.+?)</a> </h4>").matcher(htmlSource);
- while(m.find()){
- posters.add(m.group(1).replaceAll("'","'"));
- }
- m=Pattern.compile("<span class=\"messageicon\".+?</span> <a href=\"https://tbgforums.com/.+?\">(.+?)</a>").matcher(htmlSource);
- while(m.find()){
- timestamps.add(m.group(1).replace(" ",""));
- }
- }
- List<Long>timestampsInt=new LinkedList<>();
- for(String tim:timestamps){
- long t=parse(tim);
- timestampsInt.add(t);
- }
- timestamps=null;
- System.gc();
- Iterator<String>post=posters.iterator();
- Iterator<Long>times=timestampsInt.iterator();
- String poster=post.next();
- long tsA=times.next();
- Map<String,Long>scores=new HashMap<>();
- while(post.hasNext()){
- long tsB=tsA;
- String p2=poster;
- poster=post.next();
- //begin aliases
- if(poster.equals("TBGbobe"))poster="Seanbobe";
- if(poster.equals("seanbobe3"))poster="Seanbobe";
- //end aliases
- if(poster.equals(p2)){//double post detection
- times.next();
- continue;
- }
- tsA=times.next();
- scores.put(poster,scores.getOrDefault(poster,0L)+Math.min((59000+tsA-tsB)/60000,10080));
- }
- List<Map.Entry<String,Long>>scoreList=new ArrayList<>(scores.entrySet());
- scoreList.sort((a,b)->{
- return (int)(b.getValue()-a.getValue());
- });
- for(Map.Entry<String,Long> e:scoreList){
- System.out.println(e.getKey()+" - "+(e.getValue()/60)+":"+String.format("%1$02d",e.getValue()%60));
- }
- //System.out.println(htmlSource);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment