From ba4e40b0040eccf38298f72839411a0e4e239336 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Thu, 11 Jul 2019 14:47:25 +0300 Subject: [PATCH] Parse pms. Also move methods that use similar code with TopicParser to ParseHelpers and reuse code --- .../mthmmy/activities/inbox/InboxAdapter.java | 2 +- .../activities/inbox/tasks/InboxTask.java | 180 ++++++++++++++++++ .../mthmmy/activities/topic/TopicAdapter.java | 6 +- .../mthmmy/activities/topic/TopicParser.java | 125 +----------- .../activities/topic/tasks/TopicTask.java | 4 +- .../java/gr/thmmy/mthmmy/model/Inbox.java | 17 ++ .../main/java/gr/thmmy/mthmmy/model/PM.java | 131 ++++++++++++- .../mthmmy/utils/parsing/ParseHelpers.java | 115 ++++++++++- 8 files changed, 451 insertions(+), 129 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java index b52a3c25..ed5a1419 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java @@ -85,7 +85,7 @@ public class InboxAdapter extends RecyclerView.Adapter //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); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/tasks/InboxTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/tasks/InboxTask.java index 50c72745..252e5b72 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/inbox/tasks/InboxTask.java +++ b/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 { @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 pmList = parsePMs(document, language); + + return null; } @@ -17,6 +39,164 @@ public class InboxTask extends NewParseTask { protected int getResultCode(Response response, Inbox data) { return 0; } + + private ArrayList parsePMs(Document document, ParseHelpers.Language language) { + ArrayList 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 = "" + 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 infoList = Arrays.asList(authorInfoContainer.html().split("
")); + + 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(" { } 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(). diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java index 1a7922ff..bd8dfab2 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java +++ b/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. *

Class contains the methods: