Browse Source

Poll parsing fixes

pull/61/merge
Apostolos Fanakis 6 years ago
parent
commit
a2303c902f
  1. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java
  2. 24
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  3. 70
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java
  4. 2
      app/src/main/java/gr/thmmy/mthmmy/model/Poll.java

2
app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java

@ -180,8 +180,6 @@ public class SummaryFragment extends Fragment {
if (profileSummaryRow.contains("@") && if (profileSummaryRow.contains("@") &&
(profileSummaryRow.contains("Email") || profileSummaryRow.contains("E-mail"))) { (profileSummaryRow.contains("Email") || profileSummaryRow.contains("E-mail"))) {
Timber.d("mpika");
Timber.d(profileSummaryRow);
String email = profileSummaryRow.substring(profileSummaryRow.indexOf(":</b> ") + 6); String email = profileSummaryRow.substring(profileSummaryRow.indexOf(":</b> ") + 6);
profileSummaryRow = profileSummaryRow.replace(email, profileSummaryRow = profileSummaryRow.replace(email,
"<a href=\"mailto:" + email + "\">" + email + "</a>"); "<a href=\"mailto:" + email + "\">" + email + "</a>");

24
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

@ -17,8 +17,10 @@ import android.support.v7.app.AlertDialog;
import android.support.v7.content.res.AppCompatResources; import android.support.v7.content.res.AppCompatResources;
import android.support.v7.widget.AppCompatButton; import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.InputType; import android.text.InputType;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -41,11 +43,13 @@ import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import com.github.mikephil.charting.charts.HorizontalBarChart; import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry; import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import java.util.ArrayList; import java.util.ArrayList;
@ -170,7 +174,13 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
if (poll.getAvailableVoteCount() > 1) { if (poll.getAvailableVoteCount() > 1) {
for (Poll.Entry entry : entries) { for (Poll.Entry entry : entries) {
CheckBox checkBox = new CheckBox(context); CheckBox checkBox = new CheckBox(context);
checkBox.setText(entry.getEntryName()); checkBox.setMovementMethod(LinkMovementMethod.getInstance());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
checkBox.setText(Html.fromHtml(entry.getEntryName(), Html.FROM_HTML_MODE_LEGACY));
} else {
//noinspection deprecation
checkBox.setText(Html.fromHtml(entry.getEntryName()));
}
checkBox.setTextColor(context.getResources().getColor(R.color.primary_text)); checkBox.setTextColor(context.getResources().getColor(R.color.primary_text));
holder.optionsLayout.addView(checkBox); holder.optionsLayout.addView(checkBox);
} }
@ -181,7 +191,13 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
for (int i = 0; i < entries.length; i++) { for (int i = 0; i < entries.length; i++) {
RadioButton radioButton = new RadioButton(context); RadioButton radioButton = new RadioButton(context);
radioButton.setId(i); radioButton.setId(i);
radioButton.setText(entries[i].getEntryName()); radioButton.setMovementMethod(LinkMovementMethod.getInstance());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
radioButton.setText(Html.fromHtml(entries[i].getEntryName(), Html.FROM_HTML_MODE_LEGACY));
} else {
//noinspection deprecation
radioButton.setText(Html.fromHtml(entries[i].getEntryName()));
}
radioButton.setTextColor(context.getResources().getColor(R.color.primary_text)); radioButton.setTextColor(context.getResources().getColor(R.color.primary_text));
radioGroup.addView(radioButton); radioGroup.addView(radioButton);
} }
@ -206,7 +222,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
yAxisRight.setEnabled(false); yAxisRight.setEnabled(false);
XAxis xAxis = holder.voteChart.getXAxis(); XAxis xAxis = holder.voteChart.getXAxis();
xAxis.setValueFormatter((value, axis) -> entries[(int) value].getEntryName()); xAxis.setValueFormatter((value, axis) -> Html.fromHtml(entries[(int) value].getEntryName()).toString());
xAxis.setTextColor(context.getResources().getColor(R.color.primary_text)); xAxis.setTextColor(context.getResources().getColor(R.color.primary_text));
xAxis.setGranularity(1f); xAxis.setGranularity(1f);
xAxis.setDrawGridLines(false); xAxis.setDrawGridLines(false);
@ -752,7 +768,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
final AppCompatButton removeVotesButton, showPollResultsButton, hidePollResultsButton; final AppCompatButton removeVotesButton, showPollResultsButton, hidePollResultsButton;
final HorizontalBarChart voteChart; final HorizontalBarChart voteChart;
public PollViewHolder(View itemView) { PollViewHolder(View itemView) {
super(itemView); super(itemView);
question = itemView.findViewById(R.id.question_textview); question = itemView.findViewById(R.id.question_textview);

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

@ -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("&nbsp;", " ").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;
} }

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

@ -22,7 +22,7 @@ public class Poll extends TopicItem {
this.showOptionsUrl = showOptionsUrl; this.showOptionsUrl = showOptionsUrl;
} }
public int totalVotes() { private int totalVotes() {
int sum = 0; int sum = 0;
for (Entry entry : entries) { for (Entry entry : entries) {
sum += entry.votes; sum += entry.votes;

Loading…
Cancel
Save