From 423573cbc102702d3ce7b61859b3946d23eafc7c Mon Sep 17 00:00:00 2001 From: Ezerous Date: Fri, 19 Jun 2020 00:10:48 +0300 Subject: [PATCH] BoardActivity fixes --- .../activities/board/BoardActivity.java | 42 +++++++++++-------- .../mthmmy/activities/topic/TopicParser.java | 11 ----- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java index e9111365..c941269e 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java @@ -236,6 +236,7 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo if (!Objects.equals(subBoardRow.className(), "titlebg")) { String pUrl = "", pTitle = "", pMods = "", pStats = "", pLastPost = "No posts yet", pLastPostUrl = ""; + boolean parsingFailed = false; Elements subBoardColumns = subBoardRow.select(">td"); for (Element subBoardCol : subBoardColumns) { if (Objects.equals(subBoardCol.className(), "windowbg")){ @@ -246,20 +247,22 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo else if (Objects.equals(subBoardCol.className(), "smalltext")) { pLastPost = subBoardCol.text(); - if (pLastPost.contains(" in ")) { - pLastPost = pLastPost.substring(0, pLastPost.indexOf(" in ")) + - "\n" + - pLastPost.substring(pLastPost.indexOf(" in ") + 1, pLastPost.indexOf(" by ")) + - "\n" + - pLastPost.substring(pLastPost.lastIndexOf(" by ") + 1); - pLastPostUrl = subBoardCol.select("a").first().attr("href"); - } else if (pLastPost.contains(" σε ")) { - pLastPost = pLastPost.substring(0, pLastPost.indexOf(" σε ")) + - "\n" + - pLastPost.substring(pLastPost.indexOf(" σε ") + 1, pLastPost.indexOf(" από ")) + - "\n" + - pLastPost.substring(pLastPost.lastIndexOf(" από ") + 1); - pLastPostUrl = subBoardCol.select("a").first().attr("href"); + if (pLastPost.contains(" in ") || pLastPost.contains(" σε ")) { + Pattern pattern = Pattern.compile("(?:Last post on |Τελευταίο μήνυμα στις )((?:(?!(?:in|σε)).)*)\\s(?:in|σε)\\s.*"); + Matcher matcher = pattern.matcher(pLastPost); + if (matcher.find()){ + String pLastPostDateTime = matcher.group(1); + String pSubject = subBoardCol.select("a").first().attr("title"); + String pLastUser = subBoardCol.select("a").last().text(); + pLastPost = "Last post on: " + pLastPostDateTime + "\nin: " + pSubject + "\nby " +pLastUser; + + pLastPostUrl = subBoardCol.select("a").first().attr("href"); + } + else{ + parsingFailed = true; + break; + } + } else if (pLastPost.contains("redirected clicks")||pLastPost.contains("N/A")) pLastPost = ""; else @@ -271,7 +274,10 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo pMods = subBoardCol.select("div.smalltext").first().text(); } } - tempSubboards.add(new Board(pUrl, pTitle, pMods, pStats, pLastPost, pLastPostUrl)); + if(!parsingFailed) + tempSubboards.add(new Board(pUrl, pTitle, pMods, pStats, pLastPost, pLastPostUrl)); + else + Timber.e("Parsing failed (pLastPost came with: \"%s\", subBoardColumns html was \"%s\")", pLastPost, subBoardColumns); } } } @@ -300,14 +306,14 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo pStats = "Replies: " + topicColumns.get(4).text() + ", Views: " + topicColumns.get(5).text(); pLastPost = topicColumns.last().text(); - Pattern pattern = Pattern.compile("(.+)\\s(by|από)\\s(.+)$"); + Pattern pattern = Pattern.compile("((?:(?!(?:by|από)).)*)\\s(?:by|από)\\s(.*)"); Matcher matcher = pattern.matcher(pLastPost); if (matcher.find()){ pLastPostDateTime = matcher.group(1); - pLastUser = matcher.group(3); + pLastUser = matcher.group(2); } else{ - Timber.e("Parsing failed (pLastPost came with: \"%s\", html was \"%s\")", pLastPost, topicColumns); + Timber.e("Parsing failed (pLastPost came with: \"%s\", topicColumns html was \"%s\")", pLastPost, topicColumns); continue; } 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 901ce553..1a7922ff 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 @@ -18,7 +18,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import gr.thmmy.mthmmy.base.BaseActivity; -import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.model.Poll; import gr.thmmy.mthmmy.model.Post; import gr.thmmy.mthmmy.model.ThmmyFile; @@ -26,8 +25,6 @@ import gr.thmmy.mthmmy.model.TopicItem; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import timber.log.Timber; -import static gr.thmmy.mthmmy.utils.parsing.ThmmyDateTimeParser.convertToTimestamp; - /** * Singleton used for parsing a topic. @@ -178,7 +175,6 @@ public class TopicParser { p_specialRank, p_gender, p_personalText, p_numberOfPosts, p_postLastEditDate, p_postURL, p_deletePostURL, p_editPostURL; int p_postNum, p_postIndex, p_numberOfStars, p_userColor; - long p_timestamp; boolean p_isDeleted = false, p_isUserMentionedInPost = false; ArrayList p_attachedFiles; @@ -195,7 +191,6 @@ public class TopicParser { p_postLastEditDate = null; p_deletePostURL = null; p_editPostURL = null; - p_timestamp = 0; //Language independent parsing //Finds thumbnail url @@ -272,12 +267,6 @@ public class TopicParser { p_postDate = p_postDate.substring(p_postDate.indexOf("στις:") + 6 , p_postDate.indexOf(" »")); - if (BaseApplication.getInstance().isDisplayRelativeTimeEnabled()) { - String timestamp = convertToTimestamp(p_postDate); - if(timestamp!=null) - p_timestamp = Long.valueOf(timestamp); - } - //Finds post's reply index number Element postNum = thisRow.select("div.smalltext:matches(Απάντηση #)").first(); if (postNum == null) { //Topic starter