Posted By

sethsavage on 06/28/17


Tagged


Versions (?)

Reddit Scanner


 / Published in: Java
 

Used during 2016 to determine likelihood that a Reddit user was a habitual troll against Bernie Sanders supporters.

  1. //Author: Seth Savage
  2.  
  3. package redditscanner;
  4. import java.util.*;
  5. import java.io.*;
  6. import javax.swing.JFileChooser;
  7. import java.util.Scanner;
  8. import java.util.StringTokenizer;
  9. import java.text.DecimalFormat;
  10. import javax.swing.JOptionPane;
  11. import java.io.IOException;
  12. import java.nio.file.Files;
  13. import java.nio.file.Paths;
  14. import java.nio.file.StandardOpenOption;
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17.  
  18. public class RedditScanner
  19. {
  20. public static void main(String[] args) throws IOException
  21. {
  22. //***** Build rawContent String *****************************************
  23. File txtFile = null;
  24. JFileChooser txtChooser = new JFileChooser();
  25.  
  26. if(txtChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
  27. txtFile = txtChooser.getSelectedFile();
  28.  
  29. FileReader txtReader = new FileReader(txtFile);
  30. String rawContent = new Scanner(txtFile).useDelimiter("\\A").next();
  31. rawContent = rawContent.replaceAll("subreddit\\shover\"\\s>"," subredditSTART");
  32. rawContent = rawContent.replaceAll("<li><a\\shref\\S\\S"," linkSTART");
  33. rawContent = rawContent.replaceAll("class\\S\\Smd\\S><p>"," commentSTART");
  34. rawContent = rawContent.replaceAll("class\\S\\Smd\\S><blockquote>"," commentSTART");
  35. rawContent = rawContent.replaceAll("&#39;","'");
  36. rawContent = rawContent.replaceAll("&quot;","\"");
  37. rawContent = rawContent.replaceAll("<p>"," ");
  38. rawContent = rawContent.replaceAll("</p>"," ");
  39. rawContent = rawContent.replaceAll("\\s+"," ");
  40. //***** rawContent String COMPLETE ***************************************
  41.  
  42.  
  43. // ****** Build SubReddit Array ******************************************
  44. int subIndex = 0;
  45. String[] subArray = new String[101];
  46. Pattern subP = Pattern.compile("(subredditSTART)([a-zA-Z]*)"); //Finds Subreddits
  47. Matcher subM = subP.matcher(rawContent);
  48. while (subM.find())
  49. {
  50. subArray[subIndex] = (subM.group(2));
  51. //System.out.println("Subreddit: " + subArray[subIndex]);
  52. subIndex = (subIndex + 1);
  53. }
  54. // ***** SubReddit Array Complete ****************************************
  55.  
  56.  
  57. // ****** Build Comment Array ******************************************
  58. int comIndex = 0;
  59. String[] comArray = new String[101];
  60. Pattern comP = Pattern.compile("(commentSTART)(.*?)(</div>)"); //Finds Comments
  61. Matcher comM = comP.matcher(rawContent);
  62. while (comM.find())
  63. {
  64. comArray[comIndex] = (comM.group(2));
  65. //System.out.println("Comment: " + comArray[comIndex]);
  66. comIndex = (comIndex + 1);
  67. }
  68. // ***** Comment Array Complete ****************************************
  69.  
  70.  
  71. // ****** Build Link Array **********************************************
  72. int linkIndex = 0;
  73. String[] linkArray = new String[101];
  74. Pattern linkP = Pattern.compile("(linkSTART)(.*?)(\")"); //Finds Links
  75. Matcher linkM = linkP.matcher(rawContent);
  76. while (linkM.find())
  77. {
  78. linkArray[linkIndex] = (linkM.group(2));
  79. //System.out.println("Link: " + linkArray[linkIndex]);
  80. linkIndex = (linkIndex + 1);
  81. }
  82. // ***** Link Array Complete ******************************************
  83.  
  84.  
  85. // ****** Build USER Array **********************************************
  86.  
  87. int userIndex = 0;
  88. String[][] userArray = new String[101][3];
  89. int[] subDirNum = new int[18];
  90. String[] subDirectory = {"drama","trump","SubredditDrama","subredditcancer","The_Donald","enoughsandersspam","berniesanderssucks","berniesandersvetting","hillaryclinton","AnybodyButBernie","democrats","Liberal","Feminism","progressive","politics2","POLITIC","politics","SandersForPresident"};
  91. int trollRating = 0;
  92. for(int j=0; j<subIndex; j++)
  93. {
  94. for(int i=0; i<=17;i++)
  95. {
  96. if(subArray[j].equals(subDirectory[i]))
  97. {
  98. userArray[userIndex][0]=subArray[j];
  99. //System.out.println("Subreddit: " + userArray[userIndex][0]);
  100. if(comArray[j]==null)
  101. {
  102. comArray[j]="empty";
  103. }
  104.  
  105.  
  106. if(userArray[userIndex][0].equals(subDirectory[5]))
  107. {
  108. trollRating = (trollRating + 1000);
  109. }
  110.  
  111.  
  112. for(int idx=0; idx<=17;idx++)
  113. {
  114. if(userArray[userIndex][0].equals(subDirectory[idx]))
  115. {
  116. subDirNum[idx]= (subDirNum[idx] + 1);
  117. }
  118. }
  119.  
  120. userArray[userIndex][1]=comArray[j].toLowerCase();
  121. //System.out.println("Comment: " + userArray[userIndex][1]);
  122. userArray[userIndex][2]=linkArray[j];
  123. //System.out.println("Link: " + userArray[userIndex][2]);
  124. userIndex = userIndex +1;
  125. }
  126. }
  127. }
  128.  
  129.  
  130. // ***** USER Array Complete *********************************************
  131.  
  132.  
  133.  
  134. // ****** STATEMENT SCAN **********************************************
  135. //Scan Pattern Directory********************************************
  136. Pattern[] scanP =
  137. {
  138. Pattern.compile("(berniebots)"),Pattern.compile("(sanders\\seffect)"),
  139. Pattern.compile("(bernie\\seffect)"),Pattern.compile("(bernie\\sfatigue)"),
  140. Pattern.compile("(sandersbots)"),Pattern.compile("(sore\\slosers)"),
  141. Pattern.compile("(horseshit)"),Pattern.compile("(bernie\\sspam)"),
  142. Pattern.compile("(bernietroll)"),Pattern.compile("(bernie\\stroll)"),
  143. Pattern.compile("(he\\swill\\slose)"),Pattern.compile("(more\\sthan\\ssanders)"),
  144. Pattern.compile("(more\\sthan\\sbernie)"),Pattern.compile("(no\\sroad)"),
  145. Pattern.compile("(delusional)"),Pattern.compile("(bernie\\sbro)"),
  146. Pattern.compile("(berniebro)"),Pattern.compile("(endorses\\shillary)"),
  147. Pattern.compile("(endorses\\sclinton)"),Pattern.compile("(drop\\sout)"),
  148. Pattern.compile("(full\\stax\\streturns)"),Pattern.compile("(withher)"),
  149. Pattern.compile("(imwithher)"),Pattern.compile("(with\\sher)"),
  150. Pattern.compile("(weakens\\sprogressives)"),Pattern.compile("(vilification)"),
  151. Pattern.compile("(they\'re\\schildren)"),Pattern.compile("(forgive\\sbernie)"),
  152. Pattern.compile("(forgive\\ssanders)"),Pattern.compile("(pack\\sit\\sin)"),
  153. Pattern.compile("(support\\shillary)"),Pattern.compile("(support\\sclinton)"),
  154. Pattern.compile("(vicious)"),Pattern.compile("(drop\\sout)"),
  155. Pattern.compile("(former\\sbernie)"),Pattern.compile("(former\\ssanders)"),
  156. Pattern.compile("(former\\ssupporter)"),Pattern.compile("(fuck\\shimself)"),
  157. Pattern.compile("(done\\swith)"),Pattern.compile("(dont\\strust)"),
  158. Pattern.compile("(don\'t\\strust)"),Pattern.compile("(bernie\\ssucks)"),
  159. Pattern.compile("(sanders\\ssucks)"),Pattern.compile("(smashes\\ssanders)"),
  160. Pattern.compile("(smashes\\sbernie)"),Pattern.compile("(bernout)"),
  161. Pattern.compile("(burnout)"),Pattern.compile("(sandroid)"),
  162. Pattern.compile("(bernie\\sbrigade)"),Pattern.compile("(sanders\\sbrigade)"),
  163. Pattern.compile("(witch\\shunt)"),Pattern.compile("(the\\scoffin)"),
  164. Pattern.compile("(techbros)"),Pattern.compile("(bros)"),
  165. Pattern.compile("(sandinistas)"),Pattern.compile("(cultist)"),
  166. Pattern.compile("(cuck)"),Pattern.compile("(lost\\shis\\smind)"),
  167. Pattern.compile("(shill)"),Pattern.compile("(rabid)"),
  168. Pattern.compile("(conspiracy)"),Pattern.compile("(violent)"),
  169. Pattern.compile("(whore)"),Pattern.compile("(make\\sme\\ssick)"),
  170. Pattern.compile("(my\\sgod)"),Pattern.compile("(messiah)"),
  171. Pattern.compile("(propaganda)"),Pattern.compile("(false)"),
  172. Pattern.compile("(negative)"),Pattern.compile("(positive)"),
  173. Pattern.compile("(drag\\sher)"),Pattern.compile("(embarrassment)"),
  174. Pattern.compile("(bernie\\sbots)"),Pattern.compile("(s4p)"),
  175. Pattern.compile("(vote\\sclinton)"),Pattern.compile("(gleefully)"),
  176. Pattern.compile("(annoying)"),Pattern.compile("(hate\\sfor\\sher)"),
  177. Pattern.compile("(any\\sstraw)"),Pattern.compile("(garbage)"),
  178. Pattern.compile("(bernie\\sloses)"),Pattern.compile("(fade\\saway)"),
  179. Pattern.compile("(up\\svoted)"),Pattern.compile("(devil)"),
  180. Pattern.compile("(oppose\\sbernie)"),Pattern.compile("(oppose\\ssanders)"),
  181. Pattern.compile("(fuck\\soff)"),Pattern.compile("(no\\swonder\\ssanders\\slost)"),
  182. Pattern.compile("(no\\swonder\\sbernie\\slost)"),Pattern.compile("(alleged)"),
  183. Pattern.compile("(smear)"),Pattern.compile("(most\\skids)"),
  184. Pattern.compile("(smears)"),Pattern.compile("(bernie\\smath)"),
  185. Pattern.compile("(sanders\\smath)"),Pattern.compile("(embarrassing)"),
  186. Pattern.compile("(berning\\smoney)"),Pattern.compile("(fuck\\sbernie)"),
  187. Pattern.compile("(fuck\\ssanders)"),Pattern.compile("(shills)"),
  188. Pattern.compile("(dementia)"),Pattern.compile("(not\\selectable)"),
  189. Pattern.compile("(undermine\\shillary)"),Pattern.compile("(the\\scouch)"),
  190. Pattern.compile("(she\\searned)"),Pattern.compile("(bernie\\ssupporters)"),
  191. Pattern.compile("(scapegoating)"),Pattern.compile("(demagoguery)"),
  192. Pattern.compile("(falling\\sfor\\sbernie)"),Pattern.compile("(hateful//srhetoric)"),
  193. Pattern.compile("(he\\scries)"),Pattern.compile("(violent\\srhetoric)"),
  194. Pattern.compile("(goodman)"),Pattern.compile("(salon)"),
  195. Pattern.compile("(terrifying)"),Pattern.compile("(retirement\\shome)"),
  196. Pattern.compile("(bernard)"),Pattern.compile("(threatening)"),
  197. Pattern.compile("(no\\spath)"),Pattern.compile("(childish)"),
  198. Pattern.compile("(reality)"),Pattern.compile("(insufferable)"),
  199. Pattern.compile("(bitter)"),Pattern.compile("(promises)"),
  200. Pattern.compile("(promised)"),Pattern.compile("(goober)"),
  201. Pattern.compile("(unicorn)"),Pattern.compile("(cry)"),
  202. Pattern.compile("(front\\spage)"),Pattern.compile("(massive)"),
  203. Pattern.compile("(destroyed)"),Pattern.compile("(truth\\shurts)"),
  204. Pattern.compile("(desperate)"),Pattern.compile("(crack)"),
  205. Pattern.compile("(nut)"),Pattern.compile("(pseudo)"),
  206. Pattern.compile("(faux)"),Pattern.compile("(sore)"),
  207. Pattern.compile("(attacking\\sclinton)"),Pattern.compile("(retard)"),
  208. Pattern.compile("(misinformation)"),Pattern.compile("(dumb)"),
  209. Pattern.compile("(moron)"),Pattern.compile("(idiot)"),
  210. Pattern.compile("(stupid)"),Pattern.compile("(retard)"),
  211. Pattern.compile("(smug)"),Pattern.compile("(disgusting)"),
  212. Pattern.compile("(ignorant)"),Pattern.compile("(faggot)"),
  213. Pattern.compile("(clueless)"),Pattern.compile("(stupidity)"),
  214. Pattern.compile("(psycho)"),Pattern.compile("(moronic)"),
  215. Pattern.compile("(racist)"),Pattern.compile("(really\\sshould)"),
  216. Pattern.compile("(worthless)"),Pattern.compile("(\\sking)"),
  217. Pattern.compile("(blind)"),Pattern.compile("(deaf)"),
  218. Pattern.compile("(asshole)"),Pattern.compile("(bitch)"),
  219. Pattern.compile("(fake)"),Pattern.compile("(basement)"),
  220. Pattern.compile("(parasite)"),Pattern.compile("(like\\syou)"),
  221. Pattern.compile("(fucking)"),Pattern.compile("(masturbating)"),
  222. Pattern.compile("(free\\sstuff)"),Pattern.compile("(ass\\shanded)"),
  223. Pattern.compile("(almighty)"),Pattern.compile("(witty)"),
  224. Pattern.compile("(\\skid)"),Pattern.compile("(cute\\s)"),
  225. Pattern.compile("(shit)"),Pattern.compile("(arrogant)"),
  226. Pattern.compile("(universe)"),Pattern.compile("(real\\sworld)"),
  227. Pattern.compile("(contrived)"),Pattern.compile("(stereotypical)"),
  228. Pattern.compile("(unfounded)"),Pattern.compile("(joke)"),
  229. Pattern.compile("(hairline)"),Pattern.compile("(was\\sover)"),
  230. Pattern.compile("(evil)"),Pattern.compile("(disgusting)")
  231.  
  232. };
  233.  
  234.  
  235.  
  236. for(int j=0; j<userIndex; j++)
  237. {
  238. for(int i=0; i<scanP.length-1; i++)
  239. {
  240. Matcher scanM = scanP[i].matcher(userArray[j][1]);
  241. if (scanM.find())
  242. {
  243. System.out.println();
  244. System.out.println("STATEMENT MATCHED: " + scanM.group());
  245. System.out.println("COMMENT: " +userArray[j][1]);
  246. System.out.println("SUBREDDIT: " +userArray[j][0]);
  247. System.out.println("LINK: " +userArray[j][2]);
  248. trollRating = (trollRating + 100);
  249. }
  250.  
  251. }
  252.  
  253. }
  254.  
  255. System.out.println();
  256. System.out.println("==============================================================");
  257. System.out.println();
  258. for(int idx=0; idx<=17;idx++)
  259. {
  260. System.out.println("[" +subDirNum[idx] +"]" + subDirectory[idx]);
  261. }
  262.  
  263. for(int idx=0; idx<=15;idx++)
  264. {
  265. trollRating = subDirNum[idx] + trollRating;
  266. }
  267.  
  268.  
  269.  
  270.  
  271. System.out.println();
  272. System.out.println("==============================================================");
  273. System.out.println();
  274. System.out.println("Troll Rating: " + trollRating);
  275.  
  276. System.out.println();
  277.  
  278. // ***** STATEMENT SCAN Complete ********************************************
  279.  
  280. }
  281.  
  282. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: ccc19776 on July 8, 2017

You do know that reddit has a JSON API? Meaning you can parse the response data into objects and arrays with just one method call?

You need to login to post a comment.