Advertisement
gaz_lloyd

Untitled

Sep 12th, 2014
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. package com.wikia.runescape;
  2.  
  3. import org.mediawiki.MediaWiki;
  4.  
  5. import org.w3c.dom.Document;
  6. import org.w3c.dom.NamedNodeMap;
  7. import org.w3c.dom.NodeList;
  8.  
  9. import java.io.*;
  10. import java.net.*;
  11. import java.util.Map;
  12. import java.util.TreeMap;
  13.  
  14. public class AbuseFilterReader {
  15.  
  16. public static void main(String[] args) throws IOException {
  17. // Which wiki are we working on?
  18. final MediaWiki wiki = new MediaWiki("runescape.wikia.com").setUsingCompression(true);
  19.  
  20. while (true) {
  21. try {
  22. wiki.logIn("Gaz Lloyd", "ilovemol".toCharArray());
  23. break;
  24. } catch (final MediaWiki.LoginFailureException e) {
  25. System.err.println("Login failed; please check LoginName and LoginPassword in the configuration file");
  26. e.printStackTrace();
  27. System.exit(1);
  28. return;
  29. } catch (final MediaWiki.LoginDelayException t) {
  30. System.err.println("Login throttled; retrying in " + t.getWaitTime() + " seconds");
  31. try {
  32. Thread.sleep((long) t.getWaitTime() * 1000);
  33. } catch (InterruptedException e) {
  34. // don't care
  35. }
  36. } catch (final MediaWiki.BlockException b) {
  37. System.err.println("User blocked; please check its block log");
  38. b.printStackTrace();
  39. System.exit(1);
  40. return;
  41. } catch (IOException e) {
  42. System.err.println("Network error occurred while logging in; retrying shortly");
  43. e.printStackTrace();
  44. shortDelay();
  45. } catch (MediaWiki.MediaWikiException e) {
  46. System.err.println("Network error occurred while logging in; retrying shortly");
  47. e.printStackTrace();
  48. shortDelay();
  49. }
  50. }
  51.  
  52. Map<String, String> params = new TreeMap<String, String>();
  53. params.put("format", "xml");
  54. params.put("action", "query");
  55. params.put("list", "abuselog");
  56. params.put("aflfilter", "45");
  57. params.put("aflprop", "details");
  58. params.put("afllimit", "50");
  59.  
  60. //InputStream in = null;
  61. Document in = null;
  62. try {
  63. in = wiki.getABFLog(params);
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. System.exit(1);
  67. }
  68.  
  69. NodeList eles = in.getElementsByTagName("details");
  70.  
  71. for (int i = 0; i < eles.getLength(); i++) {
  72. NamedNodeMap n = eles.item(i).getAttributes();
  73. System.out.println(parse2(n));
  74. }
  75. }
  76.  
  77. public static String parse2(NamedNodeMap n) {
  78. String time = n.getNamedItem("timestamp").getNodeValue();
  79. String user = n.getNamedItem("user_name").getNodeValue();
  80. String article = n.getNamedItem("article_text").getNodeValue().replace("Feedback/", "");
  81. String txt = n.getNamedItem("new_wikitext").getNodeValue();
  82. txt = txt.replaceAll("&quot;", "");
  83. txt = txt.replace("\"", "");
  84. String rating = txt.replaceAll(".*?\\{rating:(\\d),comment:(.*?)\\}.*?", "$1");
  85. String comment = txt.replaceAll(".*?\\{rating:(\\d),comment:(.*?)\\}.*?", "$2");
  86. return "[" + time + "]: " + user + " rated " + article + ": Rating: " + rating + "; Comment: " + comment;
  87. }
  88.  
  89.  
  90. public static String parse(String s) {
  91. int locDet = s.indexOf("new_wikitext=\"");
  92. int locDetEnd = s.indexOf("\"", locDet + 14);
  93. int locTitle = s.indexOf("article_text=\"");
  94. int locTitleEnd = s.indexOf("\"", locTitle + 14);
  95. int locTime = s.indexOf("timestamp=");
  96. int locTimeEnd = s.indexOf("\"", locTime + 11);
  97. String details = s.substring(locDet + 14, locDetEnd);
  98. details = details.replace("&quot;", "\u7812");
  99. String title = s.substring(locTitle + 14, locTitleEnd);
  100. title = title.replace("Feedback/", "");
  101. String timestamp = s.substring(locTime + 11, locTimeEnd);
  102. int ratst = details.indexOf("rating") + 9;
  103. String rating = details.substring(ratst, ratst + 1);
  104. int comst = details.indexOf("comment") + 9;
  105. int comend = details.indexOf('\u7812', comst + 1);
  106. String comment = details.substring(comst + 1, comend);
  107. comment = comment.trim().replace("\\n", "<br>");
  108. comment = comment.replaceAll("(.)\\1{5,}", "$1[trimmed]");
  109. return "|-\n| [[" + title + "]] || {{subst:#time:d&\\n\\b\\s\\p;F; H:i|January 1 1970 00:00:00 + " + timestamp + "seconds}} || " + rating + " || " + comment;
  110. }
  111.  
  112. private static void shortDelay() {
  113. try {
  114. Thread.sleep(45000);
  115. } catch (final InterruptedException e) {
  116. // don't care
  117. }
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement