|
@ -1,11 +1,11 @@ |
|
|
package gr.thmmy.mthmmy.activities.topic; |
|
|
package gr.thmmy.mthmmy.activities.topic; |
|
|
|
|
|
|
|
|
import android.graphics.Color; |
|
|
import android.graphics.Color; |
|
|
import android.util.Log; |
|
|
|
|
|
|
|
|
|
|
|
import org.jsoup.Jsoup; |
|
|
import org.jsoup.Jsoup; |
|
|
import org.jsoup.nodes.Document; |
|
|
import org.jsoup.nodes.Document; |
|
|
import org.jsoup.nodes.Element; |
|
|
import org.jsoup.nodes.Element; |
|
|
|
|
|
import org.jsoup.nodes.Node; |
|
|
import org.jsoup.select.Elements; |
|
|
import org.jsoup.select.Elements; |
|
|
|
|
|
|
|
|
import java.net.MalformedURLException; |
|
|
import java.net.MalformedURLException; |
|
@ -480,70 +480,67 @@ public class TopicParser { |
|
|
|
|
|
|
|
|
private static Poll findPoll(Document topic) { |
|
|
private static Poll findPoll(Document topic) { |
|
|
Pattern integerPattern = Pattern.compile("[0-9]+"); |
|
|
Pattern integerPattern = Pattern.compile("[0-9]+"); |
|
|
Elements tables = topic.select("table"); |
|
|
Element table = topic.select("table.tborder").first(); |
|
|
for (int i = 0; i < tables.size(); i++) { |
|
|
|
|
|
try { |
|
|
try { |
|
|
Element image = tables.get(i).child(0).child(0).child(0); |
|
|
|
|
|
if (image.html().contains("Poll") || image.html().contains("Ψηφοφορία")) { |
|
|
|
|
|
// has poll in english
|
|
|
|
|
|
String question; |
|
|
String question; |
|
|
ArrayList<Poll.Entry> entries = new ArrayList<>(); |
|
|
ArrayList<Poll.Entry> entries = new ArrayList<>(); |
|
|
int availableVoteCount = 0; |
|
|
int availableVoteCount = 0; |
|
|
String pollFormUrl = null, sc = null, removeVoteUrl = null, showVoteResultsUrl = null, |
|
|
String pollFormUrl = null, sc = null, removeVoteUrl = null, showVoteResultsUrl = null, |
|
|
showOptionsUrl = null; |
|
|
showOptionsUrl = null; |
|
|
|
|
|
|
|
|
Element secondRow = tables.get(i).select("tr[class=windowbg]").first(); |
|
|
Element pollColumn = table.select("tr[class=windowbg]").first().child(1); |
|
|
Element secondColumn = secondRow.child(1); |
|
|
question = pollColumn.ownText().trim(); |
|
|
String columnString = secondColumn.outerHtml(); |
|
|
Element form = pollColumn.select("form").first(); |
|
|
question = columnString.substring(columnString.indexOf('>') + 1, columnString.indexOf('<', 2)) |
|
|
|
|
|
.replace(" ", " ").trim(); |
|
|
|
|
|
|
|
|
|
|
|
Element form = secondColumn.select("form").first(); |
|
|
|
|
|
if (form != null) { |
|
|
if (form != null) { |
|
|
// poll in vote mode
|
|
|
// poll in vote mode
|
|
|
pollFormUrl = form.attr("action"); |
|
|
pollFormUrl = form.attr("action"); |
|
|
sc = form.select("input[name=sc]").first().attr("value"); |
|
|
sc = form.select("input[name=sc]").first().attr("value"); |
|
|
|
|
|
|
|
|
int rowIndex = -1; |
|
|
List<Node> possibleEntriesRows = form.select("td:has(input[id^=options])").first().childNodes(); |
|
|
Elements possibleEntriesRows = form.child(0).child(0).children(); |
|
|
for (Node possibleEntry : possibleEntriesRows) { |
|
|
for (int j = 0; j < possibleEntriesRows.size(); j++) { |
|
|
String possibleEntryHtml = possibleEntry.outerHtml(); |
|
|
if (possibleEntriesRows.get(j).select("input").size() > 0) { |
|
|
if (!possibleEntryHtml.equals(" ") && !possibleEntryHtml.equals("<br>") && !possibleEntryHtml.startsWith("<input")) { |
|
|
rowIndex = j; |
|
|
entries.add(new Poll.Entry(possibleEntryHtml.trim())); |
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
String entriesRaw = form.child(0).child(0).child(rowIndex).child(0).html(); |
|
|
|
|
|
Matcher entryMatcher = Pattern.compile(">[^<]+<br").matcher(entriesRaw); |
|
|
|
|
|
while (entryMatcher.find()) { |
|
|
|
|
|
entries.add(new Poll.Entry(entriesRaw.substring(entryMatcher.start() + 1, entryMatcher.end() - 3).trim())); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Element promptColumn = form.child(0).child(0).child(0); |
|
|
Elements formTableRows = form.select("tbody>tr"); |
|
|
String prompt = promptColumn.text(); |
|
|
Elements links; |
|
|
|
|
|
|
|
|
|
|
|
if (formTableRows.size() == 3) { |
|
|
|
|
|
String prompt = formTableRows.first().child(0).text().trim(); |
|
|
Matcher integerMatcher = integerPattern.matcher(prompt); |
|
|
Matcher integerMatcher = integerPattern.matcher(prompt); |
|
|
if (integerMatcher.find()) |
|
|
if (integerMatcher.find()) { |
|
|
availableVoteCount = Integer.parseInt(prompt.substring(integerMatcher.start(), integerMatcher.end())); |
|
|
availableVoteCount = Integer.parseInt(prompt.substring(integerMatcher.start(), integerMatcher.end())); |
|
|
else |
|
|
} |
|
|
|
|
|
links = formTableRows.get(1).child(1).select("a"); |
|
|
|
|
|
} else { |
|
|
availableVoteCount = 1; |
|
|
availableVoteCount = 1; |
|
|
|
|
|
links = formTableRows.first().child(1).select("a"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Elements links = form.select("a"); |
|
|
|
|
|
if (links != null && links.size() > 0) { |
|
|
if (links != null && links.size() > 0) { |
|
|
showVoteResultsUrl = links.first().attr("href"); |
|
|
showVoteResultsUrl = links.first().attr("href"); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
// poll in results mode
|
|
|
// poll in results mode
|
|
|
Elements optionRows = secondColumn.child(0).child(0).select("table").first().child(0).children(); |
|
|
Elements entryRows = pollColumn.select("table[cellspacing] tr"); |
|
|
for (int j = 0; j < optionRows.size(); j++) { |
|
|
for (Element entryRow : entryRows) { |
|
|
String optionName = optionRows.get(j).child(0).text(); |
|
|
Elements entryColumns = entryRow.select("td"); |
|
|
String voteCountDescription = optionRows.get(j).child(1).text(); |
|
|
String optionName = entryColumns.first().html(); |
|
|
|
|
|
String voteCountDescription = entryColumns.last().text(); |
|
|
Matcher integerMatcher = integerPattern.matcher(voteCountDescription); |
|
|
Matcher integerMatcher = integerPattern.matcher(voteCountDescription); |
|
|
integerMatcher.find(); |
|
|
int voteCount = 0; |
|
|
int voteCount = Integer.parseInt(voteCountDescription.substring(integerMatcher.start(), |
|
|
if (integerMatcher.find()) { |
|
|
|
|
|
voteCount = Integer.parseInt(voteCountDescription.substring(integerMatcher.start(), |
|
|
integerMatcher.end())); |
|
|
integerMatcher.end())); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
entries.add(0, new Poll.Entry(optionName, voteCount)); |
|
|
entries.add(0, new Poll.Entry(optionName, voteCount)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Elements links = secondColumn.select("a"); |
|
|
Elements links = pollColumn.child(0).child(0).child(0).child(1).select("a"); |
|
|
if (links != null && links.size() > 0) { |
|
|
if (links != null && links.size() > 0) { |
|
|
if (links.first().text().equals("Remove Vote") || links.first().text().equals("Αφαίρεση ψήφου")) |
|
|
if (links.first().text().equals("Remove Vote") || links.first().text().equals("Αφαίρεση ψήφου")) |
|
|
removeVoteUrl = links.first().attr("href"); |
|
|
removeVoteUrl = links.first().attr("href"); |
|
@ -553,11 +550,10 @@ public class TopicParser { |
|
|
} |
|
|
} |
|
|
return new Poll(question, entries.toArray(new Poll.Entry[0]), availableVoteCount, |
|
|
return new Poll(question, entries.toArray(new Poll.Entry[0]), availableVoteCount, |
|
|
pollFormUrl, sc, removeVoteUrl, showVoteResultsUrl, showOptionsUrl); |
|
|
pollFormUrl, sc, removeVoteUrl, showVoteResultsUrl, showOptionsUrl); |
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
Timber.v(e, "Could not parse a poll"); |
|
|
Timber.v(e, "Could not parse a poll"); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|