Version 2:
package MainPackage;
//I may have fucked up package assignment. I normally use default package.
//purpose of this is to grab votes from Bay12 forums
//TODO: Almost everything. Compile in action or list form. Start and end posts.
//Note to self: Program is NOT logged in, uses default 15 posts per page.
import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class voteGrabber {
public static void main(String[] args) throws Exception{
Scanner console = new Scanner(System.in);
System.out.println("Enter thread URL:");
String input = new String(console.nextLine());
//get full thread URL from different input types, later
input = input.substring(0, input.lastIndexOf("."));
System.out.println("Thread URL: "+input);
URL threadURL = new URL(input);
int pageCount = getPages(threadURL);
int currentPage = 1;
String nextLine = null;
String threadTitle = null;
String currentUserName = null;
User currentUser = null;
ArrayList<User> userList = new ArrayList<User>();//create a list of User objects
int postCount = 0;
while (currentPage<pageCount+1){
BufferedReader threadReader = new BufferedReader(new InputStreamReader(threadURL.openStream()));
while ((nextLine = threadReader.readLine())!=null){//note to self replace if statements with switch?
if (nextLine.contains("<meta name=\"description\"") && threadTitle == null){//this sets thread title
threadTitle = nextLine.substring(35, nextLine.length()-4);
}
else if (nextLine.contains("View the profile of") && !nextLine.contains("<div class=\"inner\"")){
//Add user to userlist if they are not present, and set current user. The increment user's post count.
currentUserName = nextLine.substring(nextLine.lastIndexOf(">", nextLine.length()-9)+1, nextLine.length()-9);
//System.out.println("User: "+ currentUserName);
boolean userExists = false;//check whether user is already listed
if (!userList.isEmpty()){
for (User u: userList){
if (u.getName().equals(currentUserName)){
currentUser = u;
userExists=true;
}
}
}
if (!userExists){//if they're not listed, add them
System.out.println("Adding new user: "+currentUserName);
currentUser = new User(currentUserName);
userList.add(currentUser);
}
currentUser.post();
}
else if (nextLine.contains("<div class=\"inner\"")){
postCount++;
//TODO handle post for current user
}
}
threadReader.close();
System.out.println("Page: "+currentPage);
currentPage++;
threadURL = new URL(input+"."+(15*(currentPage-1)));
}
//begin report
System.out.println("Reading: "+threadTitle);
System.out.println(pageCount+" Pages");
System.out.println(postCount+" Posts");//post count is 1 higher than the displayed number of replies to the thread which does not include OP
System.out.println("Begin user report:");
for (User u: userList){
System.out.println(u.getName()+ ", Post Count: "+u.getPosts());
}
}
public static int getPages(URL myURL) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(myURL.openStream()));
String line = null;
String pageNumber = null;
while ((line = reader.readLine())!=null && pageNumber == null){//change this, it does its thing twice
if (line.contains("navPages")){//find the line containing page numbers
int endIndex = line.lastIndexOf("</a>");
int startIndex = line.lastIndexOf(">", endIndex);
pageNumber = line.substring(startIndex+1,endIndex);
}
}
reader.close();
return Integer.parseInt(pageNumber);//temp
}
}
public class User {
String name = "default";
int postCount;
boolean isOP;
//todo: Add vote/post content
public User(String inputName, boolean OP){
name = inputName;
postCount = 0;
isOP = OP;
}
public User(String inputName){
name = inputName;
postCount = 0;
isOP = false;
}
public int getPosts(){
return postCount;
}
public String getName(){
return name;
}
public void post(){
postCount++;
}
public boolean getOP(){
return isOP;
}
}
Now it iterates through every page of the thread, and reports the number pages (at default count of 15 posts per page), number of posts (including OP, so 1 more than the number of replies) and collects a list of every user who has posted in the thread, with their number of posts. It gives an output like this:
Reading: Intercontinental Arms Race: Spring 1939 (Design Phase)
135 Pages
2020 Posts
Begin user report:
Sensei, Post Count: 72
andrea, Post Count: 148
NUKE9.13, Post Count: 141
evictedSaint, Post Count: 249
Happerry, Post Count: 12
Taricus, Post Count: 173
Zanzetkuken The Great, Post Count: 103
Powder Miner, Post Count: 110
stabbymcstabstab, Post Count: 39
Khan Boyzitbig, Post Count: 45
Kot, Post Count: 57
Hibou, Post Count: 22
S34N1C, Post Count: 10
mastahcheese, Post Count: 2
Light forger, Post Count: 49
VoidSlayer, Post Count: 35
Azzuro, Post Count: 83
GUNINANRUNIN, Post Count: 70
Baffler, Post Count: 51
3_14159, Post Count: 19
Mardent23, Post Count: 8
aDwarfNamedUrist, Post Count: 1
Kashyyk, Post Count: 29
Mulisa, Post Count: 67
MidnightJaguar, Post Count: 3
helmacon, Post Count: 36
Madman198237, Post Count: 95
Devastator, Post Count: 7
Funk, Post Count: 9
Wolfhunter107, Post Count: 11
Strongpoint, Post Count: 92
RAM, Post Count: 60
Chiefwaffles, Post Count: 23
piratejoe, Post Count: 51
Tyrant Leviathan, Post Count: 1
Radio Controlled, Post Count: 7
NAV, Post Count: 27
Olith McHuman, Post Count: 2
10ebbor10, Post Count: 1
It actually does something! This is accomplished with a "User" object, currently it just stores a name and number of posts. The next step will be to get user votes/actions, and assign them to users. For the time being my plan is to replace user actions every time a new action is found for a new user, in effect only tallying their latest action. At the end of the program I should have it spit out either a list of actions and all the users who voted for that action, or a list of users and which action each user wanted to take. I'll probably have the program ask the user this, or read from a configuration file. The last thing which will be necessary to make this a useable vote program is for it to recognize turns, and only collate votes from after the lastest turn in the thread (I will have it forget every user's action when it hits a new turn marker).