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.time.Instant;
- import java.time.ZoneId;
- import java.time.ZonedDateTime;
- import java.time.format.TextStyle;
- import java.time.temporal.ChronoUnit;
- import java.util.*;
- import java.util.regex.*;
- public class ThreadNecromancyScorer {
- static long parse(String datetime){
- long d=0;
- String date=datetime.replaceAll("(.+?) (.+)","$1");
- String time=datetime.replaceAll("(.+?) (.+)","$2");
- ZonedDateTime t=Instant.now().atZone(ZoneId.of("GMT-8"));
- if(date.equals("Today")){
- date=t.getYear()+"-"+t.getMonth().getDisplayName(TextStyle.SHORT,Locale.ENGLISH)+"-"+String.format("%1$02d",t.getDayOfMonth());
- }else if(datetime.startsWith("Yesterday")){
- t=t.minus(1,ChronoUnit.DAYS);
- date=t.getYear()+"-"+t.getMonth().getDisplayName(TextStyle.SHORT,Locale.ENGLISH)+"-"+String.format("%1$02d",t.getDayOfMonth());
- };
- try {
- d=new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss Z").parse(date+" "+time+" -0800").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("http://tbgforums.com/forums/viewtopic.php?id=2111&p=1");
- Scanner s=new Scanner(url.openStream());
- String htmlSource=s.useDelimiter("\\Z").next().replaceAll("\n","").replaceAll(".+<body>(.+)</body>.+","$1");
- System.setOut(new PrintStream("out.txt"));
- s.close();
- Matcher m=Pattern.compile("<a href=\"viewtopic.php.id=2111.+?p=.+?\">(\\d+)</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=1;i<=lastPage;i++){
- System.err.printf("Fetching page %s/%s\n",i,lastPage);
- url=new URL("http://tbgforums.com/forums/viewtopic.php?id=2111&p="+i);
- s=new Scanner(url.openStream());
- htmlSource=s.useDelimiter("\\Z").next().replaceAll("\n","").replaceAll("\\s+"," ").replaceAll(".+<body>(.+)</body>.+","$1");
- m=Pattern.compile("<dt><strong>(.+?)</strong></dt>").matcher(htmlSource);
- while(m.find()){
- posters.add(m.group(1).replaceAll("'","'"));
- }
- m=Pattern.compile("<a href=\"viewtopic.php.pid=.+?#p.+?\">(.+?\\d{2})</a></span>").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));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment