From ecf6363c742f47e385ce1b90c46ab05fda865b16 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sat, 24 Dec 2016 17:14:56 +0200 Subject: [PATCH] Code cleanup and yt embedding fixes --- app/src/main/assets/style.css | 7 + .../activities/topic/TopicActivity.java | 8 +- .../mthmmy/activities/topic/TopicParser.java | 431 ++++++++++-------- 3 files changed, 248 insertions(+), 198 deletions(-) diff --git a/app/src/main/assets/style.css b/app/src/main/assets/style.css index 3dead8e1..d7101e39 100644 --- a/app/src/main/assets/style.css +++ b/app/src/main/assets/style.css @@ -843,4 +843,11 @@ img { max-width:100% !important; height:auto !important; +} + +.embedded-video-play{ + display: block; + width: 5%; + opacity: 0.7; + z-index: 2; } \ No newline at end of file diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index 0c6358d8..23e613d1 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java @@ -406,6 +406,8 @@ public class TopicActivity extends BaseActivity { /* Parse method */ private void parse(Document document) { + String language = TopicParser.defineLanguage(document); + //Find topic title if missing if (topicTitle == null || Objects.equals(topicTitle, "")) { parsedTitle = document.select("td[id=top_subject]").first().text(); @@ -420,10 +422,10 @@ public class TopicActivity extends BaseActivity { } { //Find current page's index - thisPage = TopicParser.parseCurrentPageIndex(document); + thisPage = TopicParser.parseCurrentPageIndex(document, language); } { //Find number of pages - numberOfPages = TopicParser.parseTopicNumberOfPages(document, thisPage); + numberOfPages = TopicParser.parseTopicNumberOfPages(document, thisPage, language); for (int i = 0; i < numberOfPages; i++) { //Generate each page's url from topic's base url +".15*numberOfPage" @@ -431,7 +433,7 @@ public class TopicActivity extends BaseActivity { } } - postsList = TopicParser.parseTopic(document); + postsList = TopicParser.parseTopic(document, language); } /* Parse method end */ } 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 804229d6..7a74acfc 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,6 @@ package gr.thmmy.mthmmy.activities.topic; import android.graphics.Color; -import android.util.Log; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -16,21 +15,9 @@ import java.util.Objects; import gr.thmmy.mthmmy.data.Post; class TopicParser { - //Parsing variables - private static String usersViewingTopic; - private static String currentPage; - private static String postRowSelection; - private static String userNameSelection; - private static String guestSelection; - private static int postDateSubstrSelection; - private static String postNumberSelection; - private static int postNumSubstrSelection; - private static String postAttachedDiv; - private static String postAttachedSubstr; - private static String numberOfPostsSelection; - private static String genderSelection; - private static String genderAltMale; - private static String genderAltFemale; + //Languages supported + private static final String LANGUAGE_GREEK = "Greek"; + private static final String LANGUAGE_ENGLISH = "English"; //User colors variables private static final int USER_COLOR_BLACK = Color.parseColor("#000000"); @@ -43,57 +30,82 @@ class TopicParser { @SuppressWarnings("unused") private static final String TAG = "TopicParser"; - static String parseUsersViewingThisTopic(Document doc){ - defineLanguage(doc); - Log.d(TAG, doc.select("td:containsOwn(" + usersViewingTopic + ")").first().text()); - return doc.select("td:containsOwn(" + usersViewingTopic + ")").first().html(); + static String parseUsersViewingThisTopic(Document doc, String language) { + if (Objects.equals(language, LANGUAGE_GREEK)) + return doc.select("td:containsOwn(διαβάζουν αυτό το θέμα)").first().html(); + return doc.select("td:containsOwn(are viewing this topic)").first().html(); } - static int parseCurrentPageIndex(Document doc) { - defineLanguage(doc); - + static int parseCurrentPageIndex(Document doc, String language) { int returnPage = 1; - //Contains pages - Elements findCurrentPage = doc.select("td:contains(" + currentPage + ")>b"); - - for (Element item : findCurrentPage) { - if (!item.text().contains("...") //It's not "..." - && !item.text().contains(currentPage)) { //Nor "Pages:"/"Σελίδες:" - returnPage = Integer.parseInt(item.text()); - break; + + if (Objects.equals(language, LANGUAGE_GREEK)) { + //Contains pages + Elements findCurrentPage = doc.select("td:contains(Σελίδες:)>b"); + + for (Element item : findCurrentPage) { + if (!item.text().contains("...") //It's not "..." + && !item.text().contains("Σελίδες:")) { //Nor "Σελίδες:" + returnPage = Integer.parseInt(item.text()); + break; + } + } + } else { + Elements findCurrentPage = doc.select("td:contains(Pages:)>b"); + + for (Element item : findCurrentPage) { + if (!item.text().contains("...") && !item.text().contains("Pages:")) { + returnPage = Integer.parseInt(item.text()); + break; + } } } + return returnPage; } - static int parseTopicNumberOfPages(Document doc, int thisPage) { - defineLanguage(doc); - + static int parseTopicNumberOfPages(Document doc, int thisPage, String language) { //Method's variables int returnPages = 1; - //Contains all pages - Elements pages = doc.select("td:contains(" + currentPage + ")>a.navPages"); + if (Objects.equals(language, LANGUAGE_GREEK)) { + //Contains all pages + Elements pages = doc.select("td:contains(Σελίδες:)>a.navPages"); - if (pages.size() != 0) { - returnPages = thisPage; //Initialize the number - for (Element item : pages) { //Just a max - if (Integer.parseInt(item.text()) > returnPages) - returnPages = Integer.parseInt(item.text()); + if (pages.size() != 0) { + returnPages = thisPage; //Initialize the number + for (Element item : pages) { //Just a max + if (Integer.parseInt(item.text()) > returnPages) + returnPages = Integer.parseInt(item.text()); + } + } + } else { + //Contains all pages + Elements pages = doc.select("td:contains(Pages:)>a.navPages"); + + if (pages.size() != 0) { + returnPages = thisPage; + for (Element item : pages) { + if (Integer.parseInt(item.text()) > returnPages) + returnPages = Integer.parseInt(item.text()); + } } } + return returnPages; } - static ArrayList parseTopic(Document doc) { - defineLanguage(doc); - + static ArrayList parseTopic(Document doc, String language) { //Method's variables final int NO_INDEX = -1; ArrayList returnList = new ArrayList<>(); + Elements rows; - Elements rows = doc.select("form[id=quickModForm]>table>tbody>tr:matches(" - + postRowSelection +")"); + if (Objects.equals(language, LANGUAGE_GREEK)) + rows = doc.select("form[id=quickModForm]>table>tbody>tr:matches(στις)"); + else { + rows = doc.select("form[id=quickModForm]>table>tbody>tr:matches(on)"); + } for (Element item : rows) { //For every post //Variables to pass @@ -114,21 +126,7 @@ class TopicParser { p_userColor = USER_COLOR_YELLOW; p_attachedFiles = new ArrayList<>(); - //Find the Username - Element userName = item.select("a[title^=" + userNameSelection + "]").first(); - if (userName == null) { //Deleted profile - p_isDeleted = true; - p_userName = item - .select("td:has(div.smalltext:containsOwn(" - + guestSelection + "))[style^=overflow]") - .first().text(); - p_userName = p_userName.substring(0, p_userName.indexOf(" " + guestSelection)); - p_userColor = USER_COLOR_BLACK; - } else { - p_userName = userName.html(); - p_profileURL = userName.attr("href"); - } - + //Language independent parsing //Find thumbnail url Element thumbnailUrl = item.select("img.avatar").first(); p_thumbnailUrl = null; //In case user doesn't have an avatar @@ -142,7 +140,7 @@ class TopicParser { //Find post's text p_post = item.select("div").select(".post").first().outerHtml(); - { + { //Fix embedded videos Elements noembedTag = item.select("div").select(".post").first().select("noembed"); ArrayList embededVideosUrls = new ArrayList<>(); @@ -158,32 +156,20 @@ class TopicParser { break; p_post = p_post.replace( p_post.substring(p_post.indexOf("") + 9) - , "" - ); + , "
" + + "" + + "\"\"" + + "" + //+ "" + + "
"); } } //Add stuff to make it work in WebView - p_post = ("" - + p_post); //style.css - - //Find post's submit date - Element postDate = item.select("div.smalltext:matches(" + postRowSelection + ":)").first(); - p_postDate = postDate.text(); - p_postDate = p_postDate.substring(p_postDate.indexOf(postRowSelection + ":") + postDateSubstrSelection - , p_postDate.indexOf(" »")); - - //Find post's number - Element postNum = item.select("div.smalltext:matches(" + postNumberSelection + ")").first(); - if (postNum == null) { //Topic starter - p_postNum = 0; - } else { - String tmp_str = postNum.text().substring(postNumSubstrSelection); - p_postNum = Integer.parseInt(tmp_str.substring(0, tmp_str.indexOf(" " + postRowSelection))); - } + //style.css + p_post = ("" + p_post); //Find post's index Element postIndex = item.select("a[name^=msg]").first(); @@ -194,30 +180,117 @@ class TopicParser { p_postIndex = Integer.parseInt(tmp.substring(tmp.indexOf("msg") + 3)); } - //Find attached file's urls, names and info, if present - Elements postAttachments = item.select("div:containsOwn(" + postAttachedDiv - + "):containsOwn(" + postAttachedSubstr + ")"); - if (postAttachments != null) { - Elements attachedFiles = postAttachments.select("a"); - String postAttachmentsText = postAttachments.text(); + //Language dependent parsing + Element userName; + if (Objects.equals(language, LANGUAGE_GREEK)) { + //Find username + userName = item.select("a[title^=Εμφάνιση προφίλ του μέλους]").first(); + if (userName == null) { //Deleted profile + p_isDeleted = true; + p_userName = item + .select("td:has(div.smalltext:containsOwn(Επισκέπτης))[style^=overflow]") + .first().text(); + p_userName = p_userName.substring(0, p_userName.indexOf(" Επισκέπτης")); + p_userColor = USER_COLOR_BLACK; + } else { + p_userName = userName.html(); + p_profileURL = userName.attr("href"); + } - for(int i = 0; i infoList = Arrays.asList(info.html().split("
")); - for (String line : infoList) { - if (line.contains(numberOfPostsSelection)) { - postsLineIndex = infoList.indexOf(line); - //Remove any line breaks and spaces on the start and end - p_numberOfPosts = line.replace("\n", "") - .replace("\r", "").trim(); - } - if (line.contains(genderSelection)) { - if (line.contains("alt=\"" + genderAltMale + "\"")) - p_gender = genderSelection + " " + genderAltMale; - else - p_gender = genderSelection + " " + genderAltFemale; + if (Objects.equals(language, LANGUAGE_GREEK)) { + for (String line : infoList) { + if (line.contains("Μηνύματα:")) { + postsLineIndex = infoList.indexOf(line); + //Remove any line breaks and spaces on the start and end + p_numberOfPosts = line.replace("\n", "").replace("\r", "").trim(); + } + if (line.contains("Φύλο:")) { + if (line.contains("alt=\"Άντρας\"")) + p_gender = "Φύλο: Άντρας"; + else + p_gender = "Φύλο: Γυναίκα"; + } + if (line.contains("alt=\"*\"")) { + starsLineIndex = infoList.indexOf(line); + Document starsHtml = Jsoup.parse(line); + p_numberOfStars = starsHtml.select("img[alt]").size(); + p_userColor = colorPicker(starsHtml.select("img[alt]").first() + .attr("abs:src")); + } } - if (line.contains("alt=\"*\"")) { - starsLineIndex = infoList.indexOf(line); - Document starsHtml = Jsoup.parse(line); - p_numberOfStars = starsHtml.select("img[alt]").size(); - p_userColor = 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 + p_numberOfPosts = line.replace("\n", "").replace("\r", "").trim(); + } + if (line.contains("Gender:")) { + if (line.contains("alt=\"Male\"")) + p_gender = "Gender: Male"; + else + p_gender = "Gender: Female"; + } + if (line.contains("alt=\"*\"")) { + starsLineIndex = infoList.indexOf(line); + Document starsHtml = Jsoup.parse(line); + p_numberOfStars = starsHtml.select("img[alt]").size(); + p_userColor = colorPicker(starsHtml.select("img[alt]").first() + .attr("abs:src")); + } } } @@ -272,99 +368,44 @@ class TopicParser { && !thisLine.contains("