Browse Source

Parse pms. Also move methods that use similar code with TopicParser to ParseHelpers and reuse code

pms
Thodoris1999 6 years ago
parent
commit
ba4e40b004
  1. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java
  2. 180
      app/src/main/java/gr/thmmy/mthmmy/activities/inbox/tasks/InboxTask.java
  3. 6
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  4. 125
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java
  5. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java
  6. 17
      app/src/main/java/gr/thmmy/mthmmy/model/Inbox.java
  7. 131
      app/src/main/java/gr/thmmy/mthmmy/model/PM.java
  8. 115
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java

2
app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java

@ -85,7 +85,7 @@ public class InboxAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
//Sets username,submit date, index number, subject, post's and attached files texts
holder.username.setText(currentPM.getAuthor());
holder.pmDate.setText(currentPM.getPostDate());
holder.pmDate.setText(currentPM.getPmDate());
holder.subject.setText(currentPM.getSubject());
holder.pm.loadDataWithBaseURL("file:///android_asset/", currentPM.getContent(),
"text/html", "UTF-8", null);

180
app/src/main/java/gr/thmmy/mthmmy/activities/inbox/tasks/InboxTask.java

@ -1,15 +1,37 @@
package gr.thmmy.mthmmy.activities.inbox.tasks;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.model.Inbox;
import gr.thmmy.mthmmy.model.PM;
import gr.thmmy.mthmmy.utils.parsing.NewParseTask;
import gr.thmmy.mthmmy.utils.parsing.ParseException;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import okhttp3.Response;
public class InboxTask extends NewParseTask<Inbox> {
@Override
protected Inbox parse(Document document, Response response) throws ParseException {
Inbox inbox = new Inbox();
ParseHelpers.deobfuscateElements(document.select("span.__cf_email__,a.__cf_email__"), true);
ParseHelpers.Language language = ParseHelpers.Language.getLanguage(document);
inbox.setCurrentPageIndex(ParseHelpers.parseCurrentPageIndex(document, language));
inbox.setNumberOfPages(ParseHelpers.parseNumberOfPages(document, inbox.getCurrentPageIndex(), language));
ArrayList<PM> pmList = parsePMs(document, language);
return null;
}
@ -17,6 +39,164 @@ public class InboxTask extends NewParseTask<Inbox> {
protected int getResultCode(Response response, Inbox data) {
return 0;
}
private ArrayList<PM> parsePMs(Document document, ParseHelpers.Language language) {
ArrayList<PM> pms = new ArrayList<>();
Elements pmContainerContainers = document.select("td[style=padding: 1px 1px 0 1px;]");
for (Element pmContainerContainer : pmContainerContainers) {
PM pm = new PM();
boolean isAuthorDeleted;
Element pmContainer = pmContainerContainer.select("table[style=table-layout: fixed;]").first().child(0);
Element thumbnail = pmContainer.select("img.avatar").first();
pm.setThumbnailUrl(thumbnail.attr("src"));
Element subjectAndDateContainer = pmContainer.select("td[align=left]").first();
pm.setSubject(subjectAndDateContainer.select("b").first().text());
Element dateContainer = subjectAndDateContainer.select("div").first();
pm.setPmDate(subjectAndDateContainer.select("div").first().text());
String content = ParseHelpers.youtubeEmbeddedFix(pmContainer.select("div.personalmessage").first());
//Adds stuff to make it work in WebView
//style.css
content = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" + content;
pm.setContent(content);
pm.setQuoteUrl(pmContainer.select("img[src=https://www.thmmy.gr/smf/Themes/scribbles2_114/images/buttons/quote.gif]")
.first().parent().attr("href"));
pm.setReplyUrl(pmContainer.select("img[src=https://www.thmmy.gr/smf/Themes/scribbles2_114/images/buttons/im_reply.gif]")
.first().parent().attr("href"));
pm.setDeleteUrl(pmContainer.select("img[src=https://www.thmmy.gr/smf/index.php?actio" +
"n=pm;sa=pmactions;pm_actions[321639]=delete;f=inbox;start=45;;sesc=07776660021fecb42ad23f8c5dba6aff]")
.first().parent().attr("href"));
// language specific parsing
Element username;
if (language == ParseHelpers.Language.GREEK) {
//Finds username and profile's url
username = pmContainer.select("a[title^=Εμφάνιση προφίλ του μέλους]").first();
if (username == null) { //Deleted profile
isAuthorDeleted = true;
String authorName = pmContainer.select("td:has(div.smalltext:containsOwn(Επισκέπτης))[style^=overflow]")
.first().text();
authorName = authorName.substring(0, authorName.indexOf(" Επισκέπτης"));
pm.setAuthor(authorName);
pm.setAuthorColor(ParseHelpers.USER_COLOR_YELLOW);
} else {
isAuthorDeleted = false;
pm.setAuthor(username.html());
pm.setAuthor(username.attr("href"));
}
String date = dateContainer.text();
date = date.substring(date.indexOf("στις:") + 6, date.indexOf(" »"));
pm.setPmDate(date);
} else {
//Finds username
username = pmContainer.select("a[title^=View the profile of]").first();
if (username == null) { //Deleted profile
isAuthorDeleted = true;
String authorName = pmContainer
.select("td:has(div.smalltext:containsOwn(Guest))[style^=overflow]")
.first().text();
authorName = authorName.substring(0, authorName.indexOf(" Guest"));
pm.setAuthor(authorName);
pm.setAuthorColor(ParseHelpers.USER_COLOR_YELLOW);
} else {
isAuthorDeleted = false;
pm.setAuthor(username.html());
pm.setAuthor(username.attr("href"));
}
String date = dateContainer.text();
date = date.substring(date.indexOf("on:") + 4, date.indexOf(" »"));
pm.setPmDate(date);
}
if (!isAuthorDeleted) {
int postsLineIndex = -1;
int starsLineIndex = -1;
Element authorInfoContainer = pmContainer.select("div.smalltext").first();
List<String> infoList = Arrays.asList(authorInfoContainer.html().split("<br>"));
if (language == ParseHelpers.Language.GREEK) {
for (String line : infoList) {
if (line.contains("Μηνύματα:")) {
postsLineIndex = infoList.indexOf(line);
//Remove any line breaks and spaces on the start and end
pm.setAuthorNumberOfPosts(line.replace("\n", "").replace("\r", "").trim());
}
if (line.contains("Φύλο:")) {
if (line.contains("alt=\"Άντρας\""))
pm.setAuthorGender("Φύλο: Άντρας");
else
pm.setAuthorGender("Φύλο: Γυναίκα");
}
if (line.contains("alt=\"*\"")) {
starsLineIndex = infoList.indexOf(line);
Document starsHtml = Jsoup.parse(line);
pm.setAuthorNumberOfStars(starsHtml.select("img[alt]").size());
pm.setAuthorColor(ParseHelpers.colorPicker(starsHtml.select("img[alt]").first()
.attr("abs:src")));
}
}
} else {
for (String line : infoList) {
if (line.contains("Posts:")) {
postsLineIndex = infoList.indexOf(line);
//Remove any line breaks and spaces on the start and end
pm.setAuthorNumberOfPosts(line.replace("\n", "").replace("\r", "").trim());
}
if (line.contains("Gender:")) {
if (line.contains("alt=\"Male\""))
pm.setAuthorGender("Gender: Male");
else
pm.setAuthorGender("Gender: Female");
}
if (line.contains("alt=\"*\"")) {
starsLineIndex = infoList.indexOf(line);
Document starsHtml = Jsoup.parse(line);
pm.setAuthorNumberOfStars(starsHtml.select("img[alt]").size());
pm.setAuthorColor(ParseHelpers.colorPicker(starsHtml.select("img[alt]").first()
.attr("abs:src")));
}
}
}
//If this member has no stars yet ==> New member,
//or is just a member
if (starsLineIndex == -1 || starsLineIndex == 1) {
pm.setAuthorRank(infoList.get(0).trim()); //First line has the rank
//They don't have a special rank
} else if (starsLineIndex == 2) { //This member has a special rank
pm.setAuthorSpecialRank(infoList.get(0).trim());//First line has the special rank
pm.setAuthorRank(infoList.get(1).trim());//Second line has the rank
}
for (int i = postsLineIndex + 1; i < infoList.size() - 1; ++i) {
//Searches under "Posts:"
//and above "Personal Message", "View Profile" etc buttons
String thisLine = infoList.get(i);
if (!Objects.equals(thisLine, "") && thisLine != null
&& !Objects.equals(thisLine, " \n")
&& !thisLine.contains("avatar")
&& !thisLine.contains("<a href=")) {
String personalText = thisLine;
personalText = personalText.replace("\n", "").replace("\r", "").trim();
pm.setAuthorPersonalText(personalText);
}
}
//Checks post for mentions of this user (if the user is logged in)
if (BaseActivity.getSessionManager().isLoggedIn() &&
ParseHelpers.mentionsPattern.matcher(pm.getContent()).find()) {
pm.setUserMentioned(true);
}
}
pms.add(pm);
}
return pms;
}
}

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

@ -84,8 +84,8 @@ import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_URL;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_USERNAME;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL;
import static gr.thmmy.mthmmy.activities.topic.TopicParser.USER_COLOR_WHITE;
import static gr.thmmy.mthmmy.activities.topic.TopicParser.USER_COLOR_YELLOW;
import static gr.thmmy.mthmmy.utils.parsing.ParseHelpers.USER_COLOR_WHITE;
import static gr.thmmy.mthmmy.utils.parsing.ParseHelpers.USER_COLOR_YELLOW;
import static gr.thmmy.mthmmy.base.BaseActivity.getSessionManager;
/**
@ -493,7 +493,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
} else //noinspection deprecation
holder.cardChildLinear.setBackground(context.getResources().
getDrawable(R.drawable.mention_card));
} else if (mUserColor == TopicParser.USER_COLOR_PINK) {
} else if (mUserColor == ParseHelpers.USER_COLOR_PINK) {
//Special card for special member of the month!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
holder.cardChildLinear.setBackground(context.getResources().

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

@ -1,7 +1,5 @@
package gr.thmmy.mthmmy.activities.topic;
import android.graphics.Color;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
@ -30,23 +28,9 @@ import timber.log.Timber;
* Singleton used for parsing a topic.
* <p>Class contains the methods:<ul><li>{@link #parseUsersViewingThisTopic(Document,
* ParseHelpers.Language)}</li>
* <li>{@link #parseCurrentPageIndex(Document, ParseHelpers.Language)}</li>
* <li>{@link #parseTopicNumberOfPages(Document, int, ParseHelpers.Language)}</li>
* <li>{@link #parseTopic(Document, ParseHelpers.Language)}</li>
*/
public class TopicParser {
private static Pattern mentionsPattern = Pattern.
compile("<div class=\"quoteheader\">\\n\\s+?<a href=.+?>(Quote from|Παράθεση από): "
+ BaseActivity.getSessionManager().getUsername() +"\\s(στις|on)");
//User colors
private static final int USER_COLOR_BLACK = Color.parseColor("#000000");
private static final int USER_COLOR_RED = Color.parseColor("#F44336");
private static final int USER_COLOR_GREEN = Color.parseColor("#4CAF50");
private static final int USER_COLOR_BLUE = Color.parseColor("#536DFE");
static final int USER_COLOR_PINK = Color.parseColor("#FF4081");
static final int USER_COLOR_YELLOW = Color.parseColor("#FFEB3B");
static final int USER_COLOR_WHITE = Color.WHITE;
/**
* Returns users currently viewing this topic.
@ -64,82 +48,6 @@ public class TopicParser {
return topic.select("td:containsOwn(are viewing this topic)").first().html();
}
/**
* Returns current topic's page index.
*
* @param topic {@link Document} object containing this topic's source code
* @param language a {@link ParseHelpers.Language} containing this topic's
* language set, this is returned by
* {@link ParseHelpers.Language#getLanguage(Document)}
* @return int containing parsed topic's current page
* @see org.jsoup.Jsoup Jsoup
*/
public static int parseCurrentPageIndex(Document topic, ParseHelpers.Language language) {
int parsedPage = 1;
if (language == ParseHelpers.Language.GREEK) {
Elements findCurrentPage = topic.select("td:contains(Σελίδες:)>b");
for (Element item : findCurrentPage) {
if (!item.text().contains("...")
&& !item.text().contains("Σελίδες:")) {
parsedPage = Integer.parseInt(item.text());
break;
}
}
} else {
Elements findCurrentPage = topic.select("td:contains(Pages:)>b");
for (Element item : findCurrentPage) {
if (!item.text().contains("...") && !item.text().contains("Pages:")) {
parsedPage = Integer.parseInt(item.text());
break;
}
}
}
return parsedPage;
}
/**
* Returns the number of this topic's pages.
*
* @param topic {@link Document} object containing this topic's source code
* @param currentPage an int containing current page of this topic
* @param language a {@link ParseHelpers.Language} containing this topic's
* language set, this is returned by
* {@link ParseHelpers.Language#getLanguage(Document)}
* @return int containing the number of pages
* @see org.jsoup.Jsoup Jsoup
*/
public static int parseTopicNumberOfPages(Document topic, int currentPage, ParseHelpers.Language language) {
int returnPages = 1;
if (language == ParseHelpers.Language.GREEK) {
Elements pages = topic.select("td:contains(Σελίδες:)>a.navPages");
if (pages.size() != 0) {
returnPages = currentPage;
for (Element item : pages) {
if (Integer.parseInt(item.text()) > returnPages)
returnPages = Integer.parseInt(item.text());
}
}
} else {
Elements pages = topic.select("td:contains(Pages:)>a.navPages");
if (pages.size() != 0) {
returnPages = currentPage;
for (Element item : pages) {
if (Integer.parseInt(item.text()) > returnPages)
returnPages = Integer.parseInt(item.text());
}
}
}
return returnPages;
}
/**
* This method parses all the information of a topic and it's posts.
*
@ -186,7 +94,7 @@ public class TopicParser {
p_personalText = "";
p_numberOfPosts = "";
p_numberOfStars = 0;
p_userColor = USER_COLOR_YELLOW;
p_userColor = ParseHelpers.USER_COLOR_YELLOW;
p_attachedFiles = new ArrayList<>();
p_postLastEditDate = null;
p_deletePostURL = null;
@ -245,7 +153,7 @@ public class TopicParser {
.select("td:has(div.smalltext:containsOwn(Επισκέπτης))[style^=overflow]")
.first().text();
p_userName = p_userName.substring(0, p_userName.indexOf(" Επισκέπτης"));
p_userColor = USER_COLOR_YELLOW;
p_userColor = ParseHelpers.USER_COLOR_YELLOW;
} else {
p_userName = userName.html();
p_profileURL = userName.attr("href");
@ -315,7 +223,7 @@ public class TopicParser {
.select("td:has(div.smalltext:containsOwn(Guest))[style^=overflow]")
.first().text();
p_userName = p_userName.substring(0, p_userName.indexOf(" Guest"));
p_userColor = USER_COLOR_YELLOW;
p_userColor = ParseHelpers.USER_COLOR_YELLOW;
} else {
p_userName = userName.html();
p_profileURL = userName.attr("href");
@ -405,7 +313,7 @@ public class TopicParser {
starsLineIndex = infoList.indexOf(line);
Document starsHtml = Jsoup.parse(line);
p_numberOfStars = starsHtml.select("img[alt]").size();
p_userColor = colorPicker(starsHtml.select("img[alt]").first()
p_userColor = ParseHelpers.colorPicker(starsHtml.select("img[alt]").first()
.attr("abs:src"));
}
}
@ -426,7 +334,7 @@ public class TopicParser {
starsLineIndex = infoList.indexOf(line);
Document starsHtml = Jsoup.parse(line);
p_numberOfStars = starsHtml.select("img[alt]").size();
p_userColor = colorPicker(starsHtml.select("img[alt]").first()
p_userColor = ParseHelpers.colorPicker(starsHtml.select("img[alt]").first()
.attr("abs:src"));
}
}
@ -456,7 +364,7 @@ public class TopicParser {
//Checks post for mentions of this user (if the user is logged in)
if (BaseActivity.getSessionManager().isLoggedIn() &&
mentionsPattern.matcher(p_post).find()) {
ParseHelpers.mentionsPattern.matcher(p_post).find()) {
p_isUserMentionedInPost = true;
}
@ -567,25 +475,4 @@ public class TopicParser {
return null;
}
/**
* Returns the color of a user according to user's rank on forum.
*
* @param starsUrl String containing the URL of a user's stars
* @return an int corresponding to the right color
*/
private static int colorPicker(String starsUrl) {
if (starsUrl.contains("/star.gif"))
return USER_COLOR_YELLOW;
else if (starsUrl.contains("/starmod.gif"))
return USER_COLOR_GREEN;
else if (starsUrl.contains("/stargmod.gif"))
return USER_COLOR_BLUE;
else if (starsUrl.contains("/staradmin.gif"))
return USER_COLOR_RED;
else if (starsUrl.contains("/starweb.gif"))
return USER_COLOR_BLACK;
else if (starsUrl.contains("/oscar.gif"))
return USER_COLOR_PINK;
return USER_COLOR_YELLOW;
}
}

4
app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/TopicTask.java

@ -85,10 +85,10 @@ public class TopicTask extends AsyncTask<String, Void, TopicTaskResult> {
, topicTitle.indexOf("(Αναγνώστηκε") - 2);
//Finds current page's index
int currentPageIndex = TopicParser.parseCurrentPageIndex(topic, language);
int currentPageIndex = ParseHelpers.parseCurrentPageIndex(topic, language);
//Finds number of pages
int pageCount = TopicParser.parseTopicNumberOfPages(topic, currentPageIndex, language);
int pageCount = ParseHelpers.parseNumberOfPages(topic, currentPageIndex, language);
ArrayList<TopicItem> newPostsList = TopicParser.parseTopic(topic, language);

17
app/src/main/java/gr/thmmy/mthmmy/model/Inbox.java

@ -4,6 +4,23 @@ import java.util.ArrayList;
public class Inbox {
private ArrayList<PM> pms;
private int currentPageIndex, numberOfPages;
public int getCurrentPageIndex() {
return currentPageIndex;
}
public void setCurrentPageIndex(int currentPageIndex) {
this.currentPageIndex = currentPageIndex;
}
public int getNumberOfPages() {
return numberOfPages;
}
public void setNumberOfPages(int numberOfPages) {
this.numberOfPages = numberOfPages;
}
public ArrayList<PM> getPms() {
return pms;

131
app/src/main/java/gr/thmmy/mthmmy/model/PM.java

@ -1,11 +1,136 @@
package gr.thmmy.mthmmy.model;
public class PM {
private String thumbnailUrl;
private String author;
private String authorProfileUrl;
private String subject;
private String content;
private String postDate;
private String pmDate;
private String deleteUrl, replyUrl, quoteUrl;
private int authorColor;
private String authorGender;
private String authorNumberOfPosts;
private String authorRank, authorSpecialRank, authorPersonalText;
private int authorNumberOfStars;
private boolean isUserMentioned;
public int getAuthorColor() {
return authorColor;
}
public String getAuthorPersonalText() {
return authorPersonalText;
}
public void setAuthorPersonalText(String authorPersonalText) {
this.authorPersonalText = authorPersonalText;
}
public String getAuthorNumberOfPosts() {
return authorNumberOfPosts;
}
public String getAuthorRank() {
return authorRank;
}
public void setAuthorRank(String rank) {
this.authorRank = rank;
}
public String getAuthorSpecialRank() {
return authorSpecialRank;
}
public void setAuthorSpecialRank(String authorSpecialRank) {
this.authorSpecialRank = authorSpecialRank;
}
public String getAuthorGender() {
return authorGender;
}
public void setAuthorGender(String authorGender) {
this.authorGender = authorGender;
}
public int getAuthorNumberOfStars() {
return authorNumberOfStars;
}
public void setAuthorNumberOfStars(int authorNumberOfStars) {
this.authorNumberOfStars = authorNumberOfStars;
}
public void setAuthorNumberOfPosts(String authorNumberOfPosts) {
this.authorNumberOfPosts = authorNumberOfPosts;
}
public String getReplyUrl() {
return replyUrl;
}
public void setReplyUrl(String replyUrl) {
this.replyUrl = replyUrl;
}
public String getQuoteUrl() {
return quoteUrl;
}
public void setQuoteUrl(String quoteUrl) {
this.quoteUrl = quoteUrl;
}
public String getDeleteUrl() {
return deleteUrl;
}
public void setDeleteUrl(String deleteUrl) {
this.deleteUrl = deleteUrl;
}
public void setAuthorColor(int authorColor) {
this.authorColor = authorColor;
}
public boolean isUserMentioned() {
return isUserMentioned;
}
public void setUserMentioned(boolean userMentioned) {
isUserMentioned = userMentioned;
}
public String getAuthorProfileUrl() {
return authorProfileUrl;
}
public void setAuthorProfileUrl(String authorProfileUrl) {
this.authorProfileUrl = authorProfileUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
public void setAuthor(String author) {
this.author = author;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setContent(String content) {
this.content = content;
}
public void setPmDate(String pmDate) {
this.pmDate = pmDate;
}
public String getAuthor() {
return author;
@ -23,7 +148,7 @@ public class PM {
return thumbnailUrl;
}
public String getPostDate() {
return postDate;
public String getPmDate() {
return pmDate;
}
}

115
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java

@ -1,5 +1,7 @@
package gr.thmmy.mthmmy.utils.parsing;
import android.graphics.Color;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
@ -10,15 +12,50 @@ import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import gr.thmmy.mthmmy.base.BaseActivity;
import timber.log.Timber;
/**
* This class consists exclusively of static classes (enums) and methods (excluding methods of inner
* classes). It can be used to resolve a page's language and state or fix embedded videos html code
* classes). It can be used to resolve a page's language, number of pages and state or fix embedded videos html code
* and obfuscated emails.
*/
public class ParseHelpers {
public static final int USER_COLOR_PINK = Color.parseColor("#FF4081");
public static final int USER_COLOR_YELLOW = Color.parseColor("#FFEB3B");
public static final int USER_COLOR_WHITE = Color.WHITE;
//User colors
private static final int USER_COLOR_BLACK = Color.parseColor("#000000");
private static final int USER_COLOR_RED = Color.parseColor("#F44336");
private static final int USER_COLOR_GREEN = Color.parseColor("#4CAF50");
private static final int USER_COLOR_BLUE = Color.parseColor("#536DFE");
public static Pattern mentionsPattern = Pattern.
compile("<div class=\"quoteheader\">\\n\\s+?<a href=.+?>(Quote from|Παράθεση από): "
+ BaseActivity.getSessionManager().getUsername() +"\\s(στις|on)");
/**
* Returns the color of a user according to user's rank on forum.
*
* @param starsUrl String containing the URL of a user's stars
* @return an int corresponding to the right color
*/
public static int colorPicker(String starsUrl) {
if (starsUrl.contains("/star.gif"))
return USER_COLOR_YELLOW;
else if (starsUrl.contains("/starmod.gif"))
return USER_COLOR_GREEN;
else if (starsUrl.contains("/stargmod.gif"))
return USER_COLOR_BLUE;
else if (starsUrl.contains("/staradmin.gif"))
return USER_COLOR_RED;
else if (starsUrl.contains("/starweb.gif"))
return USER_COLOR_BLACK;
else if (starsUrl.contains("/oscar.gif"))
return USER_COLOR_PINK;
return USER_COLOR_YELLOW;
}
/**
* An enum describing a forum page's language by defining the types:<ul>
* <li>{@link #PAGE_INCOMPLETE}</li>
@ -170,6 +207,82 @@ public class ParseHelpers {
return fixed;
}
/**
* Returns the number of this page's pages.
*
* @param topic {@link Document} object containing this page's source code
* @param currentPage an int containing current page of this page
* @param language a {@link ParseHelpers.Language} containing this topic's
* language set, this is returned by
* {@link ParseHelpers.Language#getLanguage(Document)}
* @return int containing the number of pages
* @see org.jsoup.Jsoup Jsoup
*/
public static int parseNumberOfPages(Document topic, int currentPage, ParseHelpers.Language language) {
int returnPages = 1;
if (language == ParseHelpers.Language.GREEK) {
Elements pages = topic.select("td:contains(Σελίδες:)>a.navPages");
if (pages.size() != 0) {
returnPages = currentPage;
for (Element item : pages) {
if (Integer.parseInt(item.text()) > returnPages)
returnPages = Integer.parseInt(item.text());
}
}
} else {
Elements pages = topic.select("td:contains(Pages:)>a.navPages");
if (pages.size() != 0) {
returnPages = currentPage;
for (Element item : pages) {
if (Integer.parseInt(item.text()) > returnPages)
returnPages = Integer.parseInt(item.text());
}
}
}
return returnPages;
}
/**
* Returns current pages's page index.
*
* @param topic {@link Document} object containing this page's source code
* @param language a {@link ParseHelpers.Language} containing this page's
* language set, this is returned by
* {@link ParseHelpers.Language#getLanguage(Document)}
* @return int containing parsed topic's current page
* @see org.jsoup.Jsoup Jsoup
*/
public static int parseCurrentPageIndex(Document topic, ParseHelpers.Language language) {
int parsedPage = 1;
if (language == ParseHelpers.Language.GREEK) {
Elements findCurrentPage = topic.select("td:contains(Σελίδες:)>b");
for (Element item : findCurrentPage) {
if (!item.text().contains("...")
&& !item.text().contains("Σελίδες:")) {
parsedPage = Integer.parseInt(item.text());
break;
}
}
} else {
Elements findCurrentPage = topic.select("td:contains(Pages:)>b");
for (Element item : findCurrentPage) {
if (!item.text().contains("...") && !item.text().contains("Pages:")) {
parsedPage = Integer.parseInt(item.text());
break;
}
}
}
return parsedPage;
}
/**
* Method that extracts the base URL from a topic's page URL. For example a topic with url similar to
* "https://www.thmmy.gr/smf/index.php?topic=1.15;topicseen" or

Loading…
Cancel
Save