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("@") &&
(profileSummaryRow.contains("Email") || profileSummaryRow.contains("E-mail"))) {
Timber.d("mpika");
Timber.d(profileSummaryRow);
String email = profileSummaryRow.substring(profileSummaryRow.indexOf(":</b> ") + 6);
profileSummaryRow = profileSummaryRow.replace(email,
"<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.widget.AppCompatButton;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
@ -41,11 +43,13 @@ import android.widget.RelativeLayout;
import android.widget.TextView;
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.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
@ -170,7 +174,13 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
if (poll.getAvailableVoteCount() > 1) {
for (Poll.Entry entry : entries) {
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));
holder.optionsLayout.addView(checkBox);
}
@ -181,7 +191,13 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
for (int i = 0; i < entries.length; i++) {
RadioButton radioButton = new RadioButton(context);
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));
radioGroup.addView(radioButton);
}
@ -206,7 +222,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
yAxisRight.setEnabled(false);
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.setGranularity(1f);
xAxis.setDrawGridLines(false);
@ -752,7 +768,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
final AppCompatButton removeVotesButton, showPollResultsButton, hidePollResultsButton;
final HorizontalBarChart voteChart;
public PollViewHolder(View itemView) {
PollViewHolder(View itemView) {
super(itemView);
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;
import android.graphics.Color;
import android.util.Log;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
import java.net.MalformedURLException;
@ -480,70 +480,67 @@ public class TopicParser {
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++) {
Element table = topic.select("table.tborder").first();
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;
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('>') + 1, columnString.indexOf('<', 2))
.replace("&nbsp;", " ").trim();
Element pollColumn = table.select("tr[class=windowbg]").first().child(1);
question = pollColumn.ownText().trim();
Element form = pollColumn.select("form").first();
Element form = secondColumn.select("form").first();
if (form != null) {
// poll in vote mode
pollFormUrl = form.attr("action");
sc = form.select("input[name=sc]").first().attr("value");
int rowIndex = -1;
Elements possibleEntriesRows = form.child(0).child(0).children();
for (int j = 0; j < possibleEntriesRows.size(); j++) {
if (possibleEntriesRows.get(j).select("input").size() > 0) {
rowIndex = j;
break;
}
List<Node> possibleEntriesRows = form.select("td:has(input[id^=options])").first().childNodes();
for (Node possibleEntry : possibleEntriesRows) {
String possibleEntryHtml = possibleEntry.outerHtml();
if (!possibleEntryHtml.equals(" ") && !possibleEntryHtml.equals("<br>") && !possibleEntryHtml.startsWith("<input")) {
entries.add(new Poll.Entry(possibleEntryHtml.trim()));
}
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);
String prompt = promptColumn.text();
Elements formTableRows = form.select("tbody>tr");
Elements links;
if (formTableRows.size() == 3) {
String prompt = formTableRows.first().child(0).text().trim();
Matcher integerMatcher = integerPattern.matcher(prompt);
if (integerMatcher.find())
if (integerMatcher.find()) {
availableVoteCount = Integer.parseInt(prompt.substring(integerMatcher.start(), integerMatcher.end()));
else
}
links = formTableRows.get(1).child(1).select("a");
} else {
availableVoteCount = 1;
links = formTableRows.first().child(1).select("a");
}
Elements links = form.select("a");
if (links != null && links.size() > 0) {
showVoteResultsUrl = links.first().attr("href");
}
} else {
// poll in results mode
Elements optionRows = secondColumn.child(0).child(0).select("table").first().child(0).children();
for (int j = 0; j < optionRows.size(); j++) {
String optionName = optionRows.get(j).child(0).text();
String voteCountDescription = optionRows.get(j).child(1).text();
Elements entryRows = pollColumn.select("table[cellspacing] tr");
for (Element entryRow : entryRows) {
Elements entryColumns = entryRow.select("td");
String optionName = entryColumns.first().html();
String voteCountDescription = entryColumns.last().text();
Matcher integerMatcher = integerPattern.matcher(voteCountDescription);
integerMatcher.find();
int voteCount = Integer.parseInt(voteCountDescription.substring(integerMatcher.start(),
int voteCount = 0;
if (integerMatcher.find()) {
voteCount = Integer.parseInt(voteCountDescription.substring(integerMatcher.start(),
integerMatcher.end()));
}
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.first().text().equals("Remove Vote") || links.first().text().equals("Αφαίρεση ψήφου"))
removeVoteUrl = links.first().attr("href");
@ -553,11 +550,10 @@ public class TopicParser {
}
return new Poll(question, entries.toArray(new Poll.Entry[0]), availableVoteCount,
pollFormUrl, sc, removeVoteUrl, showVoteResultsUrl, showOptionsUrl);
}
} catch (Exception e) {
Timber.v(e, "Could not parse a poll");
}
}
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;
}
public int totalVotes() {
private int totalVotes() {
int sum = 0;
for (Entry entry : entries) {
sum += entry.votes;

Loading…
Cancel
Save