Browse Source

finish poll parsing

pull/55/head
Thodoris1999 6 years ago
parent
commit
4f7d8ba017
  1. 73
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java
  2. 9
      app/src/main/java/gr/thmmy/mthmmy/model/Poll.java

73
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java

@ -13,6 +13,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import gr.thmmy.mthmmy.base.BaseActivity;
@ -152,7 +153,7 @@ public class TopicParser {
//Method's variables
final int NO_INDEX = -1;
Poll poll = findPoll(topic, language);
Poll poll = findPoll(topic);
ArrayList<TopicItem> parsedPostsList = new ArrayList<>();
Elements postRows;
@ -473,18 +474,76 @@ public class TopicParser {
return parsedPostsList;
}
private static Poll findPoll(Document topic, ParseHelpers.Language language) {
private static Poll findPoll(Document topic) {
Pattern integerPattern = Pattern.compile("[0-9]+");
Elements tables = topic.select("table");
for (int i = 0; i < tables.size(); i++) {
try {
Element image = tables.get(i).child(0).child(0).child(0);
if (image.html().contains("Poll")) {
return new Poll();
} else if (image.html().contains("Ψηφοφορία")) {
return new Poll();
if (image.html().contains("Poll") || image.html().contains("Ψηφοφορία")) {
// has poll in english
String question;
ArrayList<Poll.Entry> entries = new ArrayList<>();
int availableVoteCount = 0;
String pollFormUrl = null, sc = null, removeVoteUrl = null, showVoteResultsUrl = null,
showOptionsUrl = null;
Element secondRow = tables.get(i).select("tr[class=windowbg]").first();
Element secondColumn = secondRow.child(1);
String columnString = secondColumn.outerHtml();
question = columnString.substring(columnString.indexOf('>'), columnString.indexOf('<', 2)).trim();
Element form = secondColumn.select("form").first();
if (form != null) {
// english poll in vote mode
pollFormUrl = form.attr("action");
Elements formInputs = form.select("input");
for (int j = 0; j < formInputs.size(); j++) {
if (formInputs.get(i).attr("name").equals("options[]")) {
entries.add(new Poll.Entry(formInputs.get(i).text()));
} else if (formInputs.get(i).attr("name").equals("sc")) {
sc = formInputs.get(i).attr("value");
}
}
Element promptColumn = form.child(0).child(0).child(0);
String prompt = promptColumn.text();
Matcher integerMatcher = integerPattern.matcher(prompt);
availableVoteCount = Integer.parseInt(prompt.substring(integerMatcher.start(), integerMatcher.end()));
Elements links = form.select("a");
if (links != null && links.size() > 0) {
showVoteResultsUrl = links.first().attr("href");
}
} else {
// english poll in results mode
Elements optionRows = secondColumn.child(0).select("table").first().children();
for (int j = 0; j < optionRows.size(); j++) {
String optionName = optionRows.get(i).child(0).text();
String voteCountDescription = optionRows.get(i).child(1).text();
Matcher integerMatcher = integerPattern.matcher(voteCountDescription);
int voteCount = Integer.parseInt(voteCountDescription.substring(integerMatcher.start(),
integerMatcher.end()));
entries.add(new Poll.Entry(optionName, voteCount));
}
Elements links = secondColumn.select("a");
if (links != null && links.size() > 0) {
if (links.first().text().equals("Remove Vote") || links.first().text().equals("Αφαίρεση ψήφου"))
removeVoteUrl = links.first().attr("href");
else if (links.first().text().equals("Voting options") || links.first().text().equals("Επιλογές ψηφοφορίας"))
showOptionsUrl = links.first().attr("href");
}
}
return new Poll(question, entries.toArray(new Poll.Entry[0]), availableVoteCount,
pollFormUrl, sc, removeVoteUrl, showVoteResultsUrl, showOptionsUrl);
}
} catch (Exception ignored) { }
} catch (Exception e) {
Timber.v(e, "Could not parse a poll");
}
}
return null;
}
/**

9
app/src/main/java/gr/thmmy/mthmmy/model/Poll.java

@ -8,10 +8,10 @@ public class Poll extends TopicItem {
private final String question;
private Entry[] entries;
private int availableVoteCount;
private String pollFormUrl, sc, removeVoteUrl, showVoteResultsUrl;
private String pollFormUrl, sc, removeVoteUrl, showVoteResultsUrl, showOptionsUrl;
public Poll(String question, Entry[] entries, int availableVoteCount, String pollFormUrl, String sc,
String removeVoteUrl, String showVoteResultsUrl) {
String removeVoteUrl, String showVoteResultsUrl, String showOptionsUrl) {
this.question = question;
this.entries = entries;
this.availableVoteCount = availableVoteCount;
@ -19,6 +19,7 @@ public class Poll extends TopicItem {
this.sc = sc;
this.removeVoteUrl = removeVoteUrl;
this.showVoteResultsUrl = showVoteResultsUrl;
this.showOptionsUrl = showOptionsUrl;
}
public int totalVotes() {
@ -63,6 +64,10 @@ public class Poll extends TopicItem {
return showVoteResultsUrl;
}
public String getShowOptionsUrl() {
return showOptionsUrl;
}
public static class Entry {
private final String entryName;
private int votes;

Loading…
Cancel
Save