SuperJedi224

Thread Necromancy Scorer

Oct 22nd, 2018
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 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.time.Instant;
  7. import java.time.ZoneId;
  8. import java.time.ZonedDateTime;
  9. import java.time.format.TextStyle;
  10. import java.time.temporal.ChronoUnit;
  11. import java.util.*;
  12. import java.util.regex.*;
  13.  
  14. public class ThreadNecromancyScorer {
  15. static long parse(String datetime){
  16. long d=0;
  17. String date=datetime.replaceAll("(.+?) (.+)","$1");
  18. String time=datetime.replaceAll("(.+?) (.+)","$2");
  19. ZonedDateTime t=Instant.now().atZone(ZoneId.of("GMT-8"));
  20. if(date.equals("Today")){
  21. date=t.getYear()+"-"+t.getMonth().getDisplayName(TextStyle.SHORT,Locale.ENGLISH)+"-"+String.format("%1$02d",t.getDayOfMonth());
  22. }else if(datetime.startsWith("Yesterday")){
  23. t=t.minus(1,ChronoUnit.DAYS);
  24. date=t.getYear()+"-"+t.getMonth().getDisplayName(TextStyle.SHORT,Locale.ENGLISH)+"-"+String.format("%1$02d",t.getDayOfMonth());
  25. };
  26. try {
  27. d=new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss Z").parse(date+" "+time+" -0800").getTime();
  28. } catch (ParseException e) {
  29. RuntimeException r=new RuntimeException();
  30. r.initCause(e);
  31. throw r;
  32. }
  33. return d;
  34. }
  35. public static void main(String[]args) throws IOException{
  36. URL url=new URL("http://tbgforums.com/forums/viewtopic.php?id=2111&p=1");
  37. Scanner s=new Scanner(url.openStream());
  38. String htmlSource=s.useDelimiter("\\Z").next().replaceAll("\n","").replaceAll(".+<body>(.+)</body>.+","$1");
  39. System.setOut(new PrintStream("out.txt"));
  40. s.close();
  41. Matcher m=Pattern.compile("<a href=\"viewtopic.php.id=2111.+?p=.+?\">(\\d+)</a>").matcher(htmlSource);
  42. int lastPage=0;
  43. while(m.find())lastPage=Integer.parseInt(m.group(1));
  44. List<String>posters=new LinkedList<>();
  45. List<String>timestamps=new LinkedList<>();
  46. for(int i=1;i<=lastPage;i++){
  47. System.err.printf("Fetching page %s/%s\n",i,lastPage);
  48. url=new URL("http://tbgforums.com/forums/viewtopic.php?id=2111&p="+i);
  49. s=new Scanner(url.openStream());
  50. htmlSource=s.useDelimiter("\\Z").next().replaceAll("\n","").replaceAll("\\s+"," ").replaceAll(".+<body>(.+)</body>.+","$1");
  51. m=Pattern.compile("<dt><strong>(.+?)</strong></dt>").matcher(htmlSource);
  52. while(m.find()){
  53. posters.add(m.group(1).replaceAll("&#039;","'"));
  54. }
  55. m=Pattern.compile("<a href=\"viewtopic.php.pid=.+?#p.+?\">(.+?\\d{2})</a></span>").matcher(htmlSource);
  56. while(m.find()){
  57. timestamps.add(m.group(1).replace("&#8201;",""));
  58. }
  59. }
  60. List<Long>timestampsInt=new LinkedList<>();
  61. for(String tim:timestamps){
  62. long t=parse(tim);
  63. timestampsInt.add(t);
  64. }
  65. timestamps=null;
  66. System.gc();
  67. Iterator<String>post=posters.iterator();
  68. Iterator<Long>times=timestampsInt.iterator();
  69. String poster=post.next();
  70. long tsA=times.next();
  71. Map<String,Long>scores=new HashMap<>();
  72. while(post.hasNext()){
  73. long tsB=tsA;
  74. String p2=poster;
  75. poster=post.next();
  76. //begin aliases
  77. if(poster.equals("TBGbobe"))poster="seanbobe";
  78. if(poster.equals("seanbobe3"))poster="seanbobe";
  79. //end aliases
  80. if(poster.equals(p2)){//double post detection
  81. times.next();
  82. continue;
  83. }
  84. tsA=times.next();
  85. scores.put(poster,scores.getOrDefault(poster,0L)+Math.min((59000+tsA-tsB)/60000,10080));
  86. }
  87. List<Map.Entry<String,Long>>scoreList=new ArrayList<>(scores.entrySet());
  88. scoreList.sort((a,b)->{
  89. return (int)(b.getValue()-a.getValue());
  90. });
  91. for(Map.Entry<String,Long> e:scoreList){
  92. System.out.println(e.getKey()+" - "+(e.getValue()/60)+":"+String.format("%1$02d",e.getValue()%60));
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment