From 43567e03b58b34f07bfafe5de7f2e71267fbc047 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Fri, 19 Jun 2020 12:07:19 +0300 Subject: [PATCH] Include edge cases in BoardActivity parsing --- .../activities/board/BoardActivity.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 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 c941269e..9e297dcf 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 @@ -229,6 +229,9 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo if (newTopicButton == null) newTopicButton = boardPage.select("a:has(img[alt=Νέο θέμα])").first(); if (newTopicButton != null) newTopicUrl = newTopicButton.attr("href"); + + final Pattern pLastPostPattern = Pattern.compile("((?:(?!(?:by|από)).)*)\\s(?:by|από)\\s(.*)"); + if(pagesLoaded == 0) { //Finds sub boards Elements subBoardRows = boardPage.select("div.tborder>table>tbody>tr"); if (subBoardRows != null && !subBoardRows.isEmpty()) { @@ -253,12 +256,25 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo if (matcher.find()){ String pLastPostDateTime = matcher.group(1); String pSubject = subBoardCol.select("a").first().attr("title"); - String pLastUser = subBoardCol.select("a").last().text(); + + // Purification for extreme edge cases + String pSubjectConcat = subBoardCol.select("a").first().text(); + pLastPost = pLastPost.replaceAll(pSubjectConcat, ""); + + String pLastUser; + matcher = pLastPostPattern.matcher(pLastPost); //Don't even try simply grabbing , user might be guest + if (matcher.find()) + pLastUser = matcher.group(2); + else { + parsingFailed = true; + break; + } + pLastPost = "Last post on: " + pLastPostDateTime + "\nin: " + pSubject + "\nby " +pLastUser; pLastPostUrl = subBoardCol.select("a").first().attr("href"); } - else{ + else { parsingFailed = true; break; } @@ -306,8 +322,7 @@ 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("((?:(?!(?:by|από)).)*)\\s(?:by|από)\\s(.*)"); - Matcher matcher = pattern.matcher(pLastPost); + Matcher matcher = pLastPostPattern.matcher(pLastPost); if (matcher.find()){ pLastPostDateTime = matcher.group(1); pLastUser = matcher.group(2);