Browse Source

Code cleanup and yt embedding fixes

pull/24/head
Apostolos Fanakis 8 years ago
parent
commit
ecf6363c74
  1. 7
      app/src/main/assets/style.css
  2. 8
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  3. 431
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java

7
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;
}

8
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 */
}

431
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<Post> parseTopic(Document doc) {
defineLanguage(doc);
static ArrayList<Post> parseTopic(Document doc, String language) {
//Method's variables
final int NO_INDEX = -1;
ArrayList<Post> 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<String> embededVideosUrls = new ArrayList<>();
@ -158,32 +156,20 @@ class TopicParser {
break;
p_post = p_post.replace(
p_post.substring(p_post.indexOf("<embed"), p_post.indexOf("/noembed>") + 9)
, "<iframe width=\"100%\" height=\"auto\" src=\""
+ "https://www.youtube.com/embed/"
+ embededVideosUrls.get(tmp_counter)
+ "\" frameborder=\"0\" allowfullscreen></iframe>"
);
, "<div class=\"embedded-video\">"
+ "<a href=\"https://www.youtube.com/"
+ embededVideosUrls.get(tmp_counter) + "\" target=\"_blank\">"
+ "<img src=\"https://img.youtube.com/vi/"
+ embededVideosUrls.get(tmp_counter) + "/default.jpg\" alt=\"\" border=\"0\">"
+ "</a>"
//+ "<img class=\"embedded-video-play\" src=\"http://www.youtube.com/yt/brand/media/image/YouTube_light_color_icon.png\">"
+ "</div>");
}
}
//Add stuff to make it work in WebView
p_post = ("<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />"
+ 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 = ("<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" + 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<attachedFiles.size(); ++i){
String[] attachedArray = new String[3];
//Find post's submit date
Element postDate = item.select("div.smalltext:matches(στις:)").first();
p_postDate = postDate.text();
p_postDate = p_postDate.substring(p_postDate.indexOf("στις:") + 6
, p_postDate.indexOf(" »"));
//Find post's number
Element postNum = item.select("div.smalltext:matches(Απάντηση #)").first();
if (postNum == null) { //Topic starter
p_postNum = 0;
} else {
String tmp_str = postNum.text().substring(12);
p_postNum = Integer.parseInt(tmp_str.substring(0, tmp_str.indexOf(" στις")));
}
//Get file's url and filename
Element tmpAttachedFileUrlAndName = attachedFiles.get(i);
attachedArray[0] = tmpAttachedFileUrlAndName.attr("href");
attachedArray[1] = tmpAttachedFileUrlAndName.text().substring(1);
//Get file's info (size and download count)
String postAttachmentsTextSbstr = postAttachmentsText.substring(
postAttachmentsText.indexOf(attachedArray[1]));
//Find attached file's urls, names and info, if present
Elements postAttachments = item.select("div:containsOwn(έγινε λήψη):containsOwn(φορές.)");
if (postAttachments != null) {
Elements attachedFiles = postAttachments.select("a");
String postAttachmentsText = postAttachments.text();
attachedArray[2] = postAttachmentsTextSbstr.substring(attachedArray[1].length()
, postAttachmentsTextSbstr.indexOf(postAttachedSubstr))
+ postAttachedSubstr + ")";
for (int i = 0; i < attachedFiles.size(); ++i) {
String[] attachedArray = new String[3];
p_attachedFiles.add(attachedArray);
//Get file's url and filename
Element tmpAttachedFileUrlAndName = attachedFiles.get(i);
attachedArray[0] = tmpAttachedFileUrlAndName.attr("href");
attachedArray[1] = tmpAttachedFileUrlAndName.text().substring(1);
//Get file's info (size and download count)
String postAttachmentsTextSbstr = postAttachmentsText.substring(
postAttachmentsText.indexOf(attachedArray[1]));
attachedArray[2] = postAttachmentsTextSbstr.substring(attachedArray[1]
.length(), postAttachmentsTextSbstr.indexOf("φορές.")) + "φορές.)";
p_attachedFiles.add(attachedArray);
}
}
} else {
//Find username
userName = item.select("a[title^=View the profile of]").first();
if (userName == null) { //Deleted profile
p_isDeleted = true;
p_userName = item
.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_BLACK;
} else {
p_userName = userName.html();
p_profileURL = userName.attr("href");
}
//Find post's submit date
Element postDate = item.select("div.smalltext:matches(on:)").first();
p_postDate = postDate.text();
p_postDate = p_postDate.substring(p_postDate.indexOf("on:") + 4
, p_postDate.indexOf(" »"));
//Find post's number
Element postNum = item.select("div.smalltext:matches(Reply #)").first();
if (postNum == null) { //Topic starter
p_postNum = 0;
} else {
String tmp_str = postNum.text().substring(9);
p_postNum = Integer.parseInt(tmp_str.substring(0, tmp_str.indexOf(" on")));
}
//Find attached file's urls, names and info, if present
Elements postAttachments = item.select("div:containsOwn(downloaded):containsOwn(times.)");
if (postAttachments != null) {
Elements attachedFiles = postAttachments.select("a");
String postAttachmentsText = postAttachments.text();
for (int i = 0; i < attachedFiles.size(); ++i) {
String[] attachedArray = new String[3];
//Get file's url and filename
Element tmpAttachedFileUrlAndName = attachedFiles.get(i);
attachedArray[0] = tmpAttachedFileUrlAndName.attr("href");
attachedArray[1] = tmpAttachedFileUrlAndName.text().substring(1);
//Get file's info (size and download count)
String postAttachmentsTextSbstr = postAttachmentsText.substring(
postAttachmentsText.indexOf(attachedArray[1]));
attachedArray[2] = postAttachmentsTextSbstr.substring(attachedArray[1]
.length(), postAttachmentsTextSbstr.indexOf("times.")) + "times.)";
p_attachedFiles.add(attachedArray);
}
}
}
@ -229,24 +302,47 @@ class TopicParser {
Element info = userName.parent().nextElementSibling(); //Get sibling "div"
List<String> infoList = Arrays.asList(info.html().split("<br>"));
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("<a href=")) {
p_personalText = thisLine; //Then this line has user's personal text
//Remove any line breaks and spaces on the start and end
p_personalText = p_personalText.replace("\n", "")
.replace("\r", "").trim();
p_personalText = p_personalText.replace("\n", "").replace("\r", "").trim();
}
}
//Add new post in postsList, extended information needed
returnList.add(new Post(p_thumbnailUrl, p_userName, p_subject, p_post
, p_postIndex, p_postNum, p_postDate, p_profileURL, p_rank
, p_specialRank, p_gender, p_numberOfPosts, p_personalText
, p_numberOfStars, p_userColor, p_attachedFiles));
returnList.add(new Post(p_thumbnailUrl, p_userName, p_subject, p_post, p_postIndex
, p_postNum, p_postDate, p_profileURL, p_rank, p_specialRank, p_gender
, p_numberOfPosts, p_personalText, p_numberOfStars, p_userColor
, p_attachedFiles));
} else { //Deleted user
//Add new post in postsList, only standard information needed
returnList.add(new Post(p_thumbnailUrl, p_userName, p_subject, p_post
, p_postIndex, p_postNum, p_postDate, p_userColor, p_attachedFiles));
returnList.add(new Post(p_thumbnailUrl, p_userName, p_subject, p_post, p_postIndex
, p_postNum, p_postDate, p_userColor, p_attachedFiles));
}
}
return returnList;
}
private static void defineLanguage(Document doc){
//English parsing variables
final String en_usersViewingTopic = "are viewing this topic";
final String en_currentPage = "Pages:";
final String en_postRowSelection = "on";
final String en_userNameSelection = "View the profile of";
final String en_guestSelection = "Guest";
final String en_postAttachedDiv = "downloaded";
final String en_postAttachedSubstr = "times.";
final String en_postsNumberSelection = "Reply #";
final String en_numberOfPostsSelection = "Posts:";
final String en_genderSelection = "Gender:";
final String en_genderAltMale = "Male";
final String en_genderAltFemale = "Female";
//Greek parsing variables
final String gr_usersViewingTopic = "διαβάζουν αυτό το θέμα";
final String gr_currentPage = "Σελίδες:";
final String gr_postRowSelection = "στις";
final String gr_userNameSelection = "Εμφάνιση προφίλ του μέλους";
final String gr_guestSelection = "Επισκέπτης";
final String gr_postAttachedDiv = "έγινε λήψη";
final String gr_postAttachedSubstr = "φορές.";
final String gr_postsNumberSelection = "Απάντηση #";
final String gr_numberOfPostsSelection = "Μηνύματα:";
final String gr_genderSelection = "Φύλο:";
final String gr_genderAltMale = "Άντρας";
final String gr_genderAltFemale = "Γυναίκα";
if(doc.select("h3").text().contains("Καλώς ορίσατε")){
usersViewingTopic = gr_usersViewingTopic;
currentPage = gr_currentPage;
postRowSelection = gr_postRowSelection;
userNameSelection = gr_userNameSelection;
guestSelection = gr_guestSelection;
postAttachedDiv = gr_postAttachedDiv;
postAttachedSubstr = gr_postAttachedSubstr;
postDateSubstrSelection = 6;
postNumberSelection = gr_postsNumberSelection;
postNumSubstrSelection = 12;
numberOfPostsSelection = gr_numberOfPostsSelection;
genderSelection = gr_genderSelection;
genderAltMale = gr_genderAltMale;
genderAltFemale = gr_genderAltFemale;
}
else{ //Default is english (eg. guest's language)
usersViewingTopic = en_usersViewingTopic;
currentPage = en_currentPage;
postRowSelection = en_postRowSelection;
userNameSelection = en_userNameSelection;
guestSelection = en_guestSelection;
postAttachedDiv = en_postAttachedDiv;
postAttachedSubstr = en_postAttachedSubstr;
postDateSubstrSelection = 4;
postNumberSelection = en_postsNumberSelection;
postNumSubstrSelection = 9;
numberOfPostsSelection = en_numberOfPostsSelection;
genderSelection = en_genderSelection;
genderAltMale = en_genderAltMale;
genderAltFemale = en_genderAltFemale;
static String defineLanguage(Document doc) {
if (doc.select("h3").text().contains("Καλώς ορίσατε")) {
return LANGUAGE_GREEK;
} else { //Default is english (eg. guest's language)
return LANGUAGE_ENGLISH;
}
}
private static int colorPicker(String starsUrl){
if(starsUrl.contains("/star.gif"))
private static int colorPicker(String starsUrl) {
if (starsUrl.contains("/star.gif"))
return USER_COLOR_YELLOW;
else if(starsUrl.contains("/starmod.gif"))
else if (starsUrl.contains("/starmod.gif"))
return USER_COLOR_GREEN;
else if(starsUrl.contains("/stargmod.gif"))
else if (starsUrl.contains("/stargmod.gif"))
return USER_COLOR_BLUE;
else if(starsUrl.contains("/staradmin.gif"))
else if (starsUrl.contains("/staradmin.gif"))
return USER_COLOR_RED;
else if(starsUrl.contains("/starweb.gif"))
else if (starsUrl.contains("/starweb.gif"))
return USER_COLOR_BLACK;
else if(starsUrl.contains("/oscar.gif"))
else if (starsUrl.contains("/oscar.gif"))
return USER_COLOR_PINK;
return USER_COLOR_YELLOW;
}

Loading…
Cancel
Save