Guest User

Untitled

a guest
Sep 6th, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.PrintStream;
  3. import java.net.URL;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.*;
  7. import java.util.regex.*;
  8.  
  9. public class ThreadNecromancyScorer {
  10.     static long parse(String datetime){
  11.         long d=0;
  12.         try {
  13.             d=new SimpleDateFormat("MMM dd, yyyy, hh:mm:ss a zzzz").parse(datetime+" AEST").getTime();
  14.         } catch (ParseException e) {
  15.             RuntimeException r=new RuntimeException();
  16.             r.initCause(e);
  17.             throw r;
  18.         }
  19.         return d;
  20.     }
  21.     public static void main(String[]args) throws IOException{
  22.         URL url=new URL("https://tbgforums.com/forums/index.php?topic=2111");
  23.         Scanner s=new Scanner(url.openStream());
  24.         String htmlSource=s.useDelimiter("\\Z").next().replaceAll("\n","").replaceAll("\\s+"," ").replaceAll(".+<body.+?>(.+)</body>.+","$1");
  25.         System.setOut(new PrintStream("out.txt"));
  26.         s.close();
  27.         Matcher m=Pattern.compile("<a class=\"nav_page\" href=\"https://tbgforums.com/forums/[^<>]+?\">([0-9]+)</a>").matcher(htmlSource);
  28.         int lastPage=0;
  29.         while(m.find()){
  30.             lastPage=Integer.parseInt(m.group(1));
  31.         }
  32.         List<String>posters=new LinkedList<>();
  33.         List<String>timestamps=new LinkedList<>();
  34.         for(int i=0;i<lastPage;i++){
  35.             System.err.printf("Fetching page %s/%s\n",i+1,lastPage);
  36.             url=new URL("https://tbgforums.com/forums/index.php?topic=2111."+(25*i));
  37.             s=new Scanner(url.openStream());
  38.             htmlSource=s.useDelimiter("\\Z").next().replaceAll("\n","").replaceAll("\\s+"," ").replaceAll(".+<body.+?>(.+)</body>.+","$1");
  39.             m=Pattern.compile("<div class=\"poster\"> <h4> <a href=\"https://tbgforums.com/.+?\">(.+?)</a> </h4>").matcher(htmlSource);
  40.             while(m.find()){
  41.                 posters.add(m.group(1).replaceAll("&#039;","'"));
  42.             }
  43.             m=Pattern.compile("<span class=\"messageicon\".+?</span> <a href=\"https://tbgforums.com/.+?\">(.+?)</a>").matcher(htmlSource);
  44.             while(m.find()){
  45.                 timestamps.add(m.group(1).replace("&#8201;",""));
  46.             }
  47.         }
  48.         List<Long>timestampsInt=new LinkedList<>();
  49.         for(String tim:timestamps){
  50.             long t=parse(tim);
  51.             timestampsInt.add(t);
  52.         }
  53.         timestamps=null;
  54.         System.gc();
  55.         Iterator<String>post=posters.iterator();
  56.         Iterator<Long>times=timestampsInt.iterator();
  57.         String poster=post.next();
  58.         long tsA=times.next();
  59.         Map<String,Long>scores=new HashMap<>();
  60.         while(post.hasNext()){
  61.             long tsB=tsA;          
  62.             String p2=poster;
  63.             poster=post.next();
  64.             //begin aliases
  65.             if(poster.equals("TBGbobe"))poster="Seanbobe";
  66.             if(poster.equals("seanbobe3"))poster="Seanbobe";
  67.             //end aliases
  68.             if(poster.equals(p2)){//double post detection
  69.                 times.next();
  70.                 continue;
  71.             }
  72.             tsA=times.next();
  73.             scores.put(poster,scores.getOrDefault(poster,0L)+Math.min((59000+tsA-tsB)/60000,10080));
  74.         }
  75.         List<Map.Entry<String,Long>>scoreList=new ArrayList<>(scores.entrySet());
  76.         scoreList.sort((a,b)->{
  77.             return (int)(b.getValue()-a.getValue());
  78.         });
  79.         for(Map.Entry<String,Long> e:scoreList){
  80.             System.out.println(e.getKey()+" - "+(e.getValue()/60)+":"+String.format("%1$02d",e.getValue()%60));
  81.         }
  82.         //System.out.println(htmlSource);
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment