From 0ce246d859c1ec699a3d8f3fa8e7269addb4e282 Mon Sep 17 00:00:00 2001 From: Floens Date: Sun, 11 Feb 2018 16:24:53 +0100 Subject: [PATCH] organize actions and the parser for vichan --- Clover/app/src/main/assets/icons/8chan.png | Bin 411 -> 0 bytes .../chan/core/site/common/CommonSite.java | 34 ++--- .../site/common/vichan/VichanActions.java | 119 ++++++++++++++++++ .../common/vichan/VichanCommentParser.java | 32 +++++ .../chan/core/site/sites/chan8/Chan8.java | 67 ++-------- .../core/site/sites/lainchan/Lainchan.java | 103 +-------------- 6 files changed, 184 insertions(+), 171 deletions(-) delete mode 100644 Clover/app/src/main/assets/icons/8chan.png create mode 100644 Clover/app/src/main/java/org/floens/chan/core/site/common/vichan/VichanActions.java create mode 100644 Clover/app/src/main/java/org/floens/chan/core/site/common/vichan/VichanCommentParser.java diff --git a/Clover/app/src/main/assets/icons/8chan.png b/Clover/app/src/main/assets/icons/8chan.png deleted file mode 100644 index cabdb1ba884d77c05b9f608906d6c73d4da40f40..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 411 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!63?wyl`GbKJOS+@4BLl<6e(pbstU$g(vPY0F z14ES>14Ba#1H&(%P{RubhEf9thF1v;3|2E37{m+a>cM?V0YC*^O8(udK|TNf|KGvC z(-UX}V@Z%-FoVOh8)+a;lDE5y$fWxxO@SQt5>H=O_J{0}BBH!^u9>$3g?v0+977~7 zul2PSYcSwp5?=d1Yw!Qqr7ss%ODa5MW%|N+vY(;(pnHPXjNrS|L$|!G^Ejuo$zqR9 z(DoCn4+_;~U-X!4vne|JF!)uFd>%zZZhlH;S|x4`-MY7nff^V*UHx3vIVCg! E06f@!3jhEB diff --git a/Clover/app/src/main/java/org/floens/chan/core/site/common/CommonSite.java b/Clover/app/src/main/java/org/floens/chan/core/site/common/CommonSite.java index 44966bf5..6a77386c 100644 --- a/Clover/app/src/main/java/org/floens/chan/core/site/common/CommonSite.java +++ b/Clover/app/src/main/java/org/floens/chan/core/site/common/CommonSite.java @@ -315,10 +315,10 @@ public abstract class CommonSite extends SiteBase { } public static abstract class CommonEndpoints implements SiteEndpoints { - private CommonSite commonSite; + protected CommonSite site; - public CommonEndpoints(CommonSite commonSite) { - this.commonSite = commonSite; + public CommonEndpoints(CommonSite site) { + this.site = site; } @NonNull @@ -407,7 +407,13 @@ public abstract class CommonSite extends SiteBase { } } - public abstract class CommonActions implements SiteActions { + public static abstract class CommonActions implements SiteActions { + protected CommonSite site; + + public CommonActions(CommonSite site) { + this.site = site; + } + public void setupPost(Reply reply, MultipartHttpCall call) { } @@ -416,8 +422,8 @@ public abstract class CommonSite extends SiteBase { @Override public void boards(BoardsListener boardsListener) { - if (!staticBoards.isEmpty()) { - boardsListener.onBoardsReceived(new Boards(staticBoards)); + if (!site.staticBoards.isEmpty()) { + boardsListener.onBoardsReceived(new Boards(site.staticBoards)); } } @@ -425,17 +431,17 @@ public abstract class CommonSite extends SiteBase { public void post(Reply reply, PostListener postListener) { ReplyResponse replyResponse = new ReplyResponse(); - reply.password = Long.toHexString(secureRandom.nextLong()); + reply.password = Long.toHexString(site.secureRandom.nextLong()); replyResponse.password = reply.password; - MultipartHttpCall call = new MultipartHttpCall(CommonSite.this) { + MultipartHttpCall call = new MultipartHttpCall(site) { @Override public void process(Response response, String result) throws IOException { handlePost(replyResponse, response, result); } }; - call.url(endpoints().reply(reply.loadable)); + call.url(site.endpoints().reply(reply.loadable)); if (requirePrepare()) { Handler handler = new Handler(Looper.getMainLooper()); @@ -453,7 +459,7 @@ public abstract class CommonSite extends SiteBase { } private void makePostCall(HttpCall call, ReplyResponse replyResponse, PostListener postListener) { - httpCallManager.makeHttpCall(call, new HttpCall.HttpCallback() { + site.httpCallManager.makeHttpCall(call, new HttpCall.HttpCallback() { @Override public void onHttpSuccess(HttpCall httpCall) { postListener.onPostComplete(httpCall, replyResponse); @@ -510,15 +516,15 @@ public abstract class CommonSite extends SiteBase { } public static abstract class CommonApi implements ChanReader { - private CommonSite commonSite; + protected CommonSite site; - public CommonApi(CommonSite commonSite) { - this.commonSite = commonSite; + public CommonApi(CommonSite site) { + this.site = site; } @Override public PostParser getParser() { - return commonSite.postParser; + return site.postParser; } } diff --git a/Clover/app/src/main/java/org/floens/chan/core/site/common/vichan/VichanActions.java b/Clover/app/src/main/java/org/floens/chan/core/site/common/vichan/VichanActions.java new file mode 100644 index 00000000..07224e38 --- /dev/null +++ b/Clover/app/src/main/java/org/floens/chan/core/site/common/vichan/VichanActions.java @@ -0,0 +1,119 @@ +/* + * Clover - 4chan browser https://github.com/Floens/Clover/ + * Copyright (C) 2014 Floens + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.floens.chan.core.site.common.vichan; + +import org.floens.chan.core.site.SiteAuthentication; +import org.floens.chan.core.site.common.CommonSite; +import org.floens.chan.core.site.common.MultipartHttpCall; +import org.floens.chan.core.site.http.Reply; +import org.floens.chan.core.site.http.ReplyResponse; +import org.jsoup.Jsoup; + +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import okhttp3.HttpUrl; +import okhttp3.Response; + +import static android.text.TextUtils.isEmpty; + +public class VichanActions extends CommonSite.CommonActions { + public VichanActions(CommonSite commonSite) { + super(commonSite); + } + + @Override + public void setupPost(Reply reply, MultipartHttpCall call) { + call.parameter("board", reply.loadable.board.code); + + if (reply.loadable.isThreadMode()) { + call.parameter("thread", String.valueOf(reply.loadable.no)); + } + + // Added with VichanAntispam. + // call.parameter("post", "Post"); + + call.parameter("password", reply.password); + call.parameter("name", reply.name); + call.parameter("email", reply.options); + + if (!isEmpty(reply.subject)) { + call.parameter("subject", reply.subject); + } + + call.parameter("body", reply.comment); + + if (reply.file != null) { + call.fileParameter("file", reply.fileName, reply.file); + } + + if (reply.spoilerImage) { + call.parameter("spoiler", "on"); + } + } + + @Override + public boolean requirePrepare() { + return true; + } + + @Override + public void prepare(MultipartHttpCall call, Reply reply, ReplyResponse replyResponse) { + VichanAntispam antispam = new VichanAntispam( + HttpUrl.parse(site.resolvable().desktopUrl(reply.loadable, null))); + antispam.addDefaultIgnoreFields(); + for (Map.Entry e : antispam.get().entrySet()) { + call.parameter(e.getKey(), e.getValue()); + } + } + + @Override + public void handlePost(ReplyResponse replyResponse, Response response, String result) { + Matcher auth = Pattern.compile(".*\"captcha\": ?true.*").matcher(result); + Matcher err = Pattern.compile(".*]*>Error.*]*>(.*?).*").matcher(result); + if (auth.find()) { + replyResponse.requireAuthentication = true; + replyResponse.errorMessage = result; + } else if (err.find()) { + replyResponse.errorMessage = Jsoup.parse(err.group(1)).body().text(); + } else { + HttpUrl url = response.request().url(); + Matcher m = Pattern.compile("/\\w+/\\w+/(\\d+).html").matcher(url.encodedPath()); + try { + if (m.find()) { + replyResponse.threadNo = Integer.parseInt(m.group(1)); + String fragment = url.encodedFragment(); + if (fragment != null) { + replyResponse.postNo = Integer.parseInt(fragment); + } else { + replyResponse.postNo = replyResponse.threadNo; + } + replyResponse.posted = true; + } + } catch (NumberFormatException ignored) { + replyResponse.errorMessage = "Error posting: could not find posted thread."; + } + } + } + + @Override + public SiteAuthentication postAuthenticate() { + return SiteAuthentication.fromNone(); + } +} diff --git a/Clover/app/src/main/java/org/floens/chan/core/site/common/vichan/VichanCommentParser.java b/Clover/app/src/main/java/org/floens/chan/core/site/common/vichan/VichanCommentParser.java new file mode 100644 index 00000000..cec0bd55 --- /dev/null +++ b/Clover/app/src/main/java/org/floens/chan/core/site/common/vichan/VichanCommentParser.java @@ -0,0 +1,32 @@ +/* + * Clover - 4chan browser https://github.com/Floens/Clover/ + * Copyright (C) 2014 Floens + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.floens.chan.core.site.common.vichan; + +import org.floens.chan.core.site.parser.CommentParser; +import org.floens.chan.core.site.parser.StyleRule; + +import java.util.regex.Pattern; + +public class VichanCommentParser extends CommentParser { + public VichanCommentParser() { + addDefaultRules(); + setQuotePattern(Pattern.compile(".*#(\\d+)")); + setFullQuotePattern(Pattern.compile("/(\\w+)/\\w+/(\\d+)\\.html#(\\d+)")); + rule(StyleRule.tagRule("p").cssClass("quote").color(StyleRule.Color.INLINE_QUOTE).linkify()); + } +} diff --git a/Clover/app/src/main/java/org/floens/chan/core/site/sites/chan8/Chan8.java b/Clover/app/src/main/java/org/floens/chan/core/site/sites/chan8/Chan8.java index af72393e..39a6afd5 100644 --- a/Clover/app/src/main/java/org/floens/chan/core/site/sites/chan8/Chan8.java +++ b/Clover/app/src/main/java/org/floens/chan/core/site/sites/chan8/Chan8.java @@ -9,22 +9,15 @@ import org.floens.chan.core.site.SiteAuthentication; import org.floens.chan.core.site.SiteIcon; import org.floens.chan.core.site.common.CommonSite; import org.floens.chan.core.site.common.MultipartHttpCall; +import org.floens.chan.core.site.common.vichan.VichanActions; import org.floens.chan.core.site.common.vichan.VichanApi; +import org.floens.chan.core.site.common.vichan.VichanCommentParser; import org.floens.chan.core.site.common.vichan.VichanEndpoints; import org.floens.chan.core.site.http.Reply; -import org.floens.chan.core.site.http.ReplyResponse; -import org.floens.chan.core.site.parser.CommentParser; -import org.floens.chan.core.site.parser.StyleRule; -import org.jsoup.Jsoup; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import okhttp3.HttpUrl; -import okhttp3.Response; - -import static android.text.TextUtils.isEmpty; public class Chan8 extends CommonSite { public static final CommonSiteUrlHandler URL_HANDLER = new CommonSiteUrlHandler() { @@ -100,60 +93,24 @@ public class Chan8 extends CommonSite { } }); - setActions(new CommonActions() { + setActions(new VichanActions(this) { @Override public void setupPost(Reply reply, MultipartHttpCall call) { - call.parameter("board", reply.loadable.board.code); + super.setupPost(reply, call); if (reply.loadable.isThreadMode()) { + // "thread" is already added in VichanActions. call.parameter("post", "New Reply"); - call.parameter("thread", String.valueOf(reply.loadable.no)); } else { call.parameter("post", "New Thread"); call.parameter("page", "1"); } - - call.parameter("pwd", reply.password); - call.parameter("name", reply.name); - call.parameter("email", reply.options); - - if (!reply.loadable.isThreadMode() && !isEmpty(reply.subject)) { - call.parameter("subject", reply.subject); - } - - call.parameter("body", reply.comment); - - if (reply.file != null) { - call.fileParameter("file", reply.fileName, reply.file); - } - - if (reply.spoilerImage) { - call.parameter("spoiler", "on"); - } } @Override - public void handlePost(ReplyResponse replyResponse, Response response, String result) { - Matcher auth = Pattern.compile(".*\"captcha\": ?true.*").matcher(result); - Matcher err = Pattern.compile(".*

Error

.*]*>(.*?).*").matcher(result); - if (auth.find()) { - replyResponse.requireAuthentication = true; - replyResponse.errorMessage = result; - } else if (err.find()) { - replyResponse.errorMessage = Jsoup.parse(err.group(1)).body().text(); - } else { - HttpUrl url = response.request().url(); - Matcher m = Pattern.compile("/\\w+/\\w+/(\\d+).html").matcher(url.encodedPath()); - try { - if (m.find()) { - replyResponse.threadNo = Integer.parseInt(m.group(1)); - replyResponse.postNo = Integer.parseInt(url.encodedFragment()); - replyResponse.posted = true; - } - } catch (NumberFormatException ignored) { - replyResponse.errorMessage = "Error posting: could not find posted thread."; - } - } + public boolean requirePrepare() { + // We don't need to check the antispam fields for 8chan. + return false; } @Override @@ -166,12 +123,6 @@ public class Chan8 extends CommonSite { setApi(new VichanApi(this)); - CommentParser commentParser = new CommentParser(); - commentParser.addDefaultRules(); - commentParser.setQuotePattern(Pattern.compile(".*#(\\d+)")); - commentParser.setFullQuotePattern(Pattern.compile("/(\\w+)/\\w+/(\\d+)\\.html#(\\d+)")); - commentParser.rule(StyleRule.tagRule("p").cssClass("quote").color(StyleRule.Color.INLINE_QUOTE).linkify()); - - setParser(commentParser); + setParser(new VichanCommentParser()); } } diff --git a/Clover/app/src/main/java/org/floens/chan/core/site/sites/lainchan/Lainchan.java b/Clover/app/src/main/java/org/floens/chan/core/site/sites/lainchan/Lainchan.java index 9100ec3d..42eab81a 100644 --- a/Clover/app/src/main/java/org/floens/chan/core/site/sites/lainchan/Lainchan.java +++ b/Clover/app/src/main/java/org/floens/chan/core/site/sites/lainchan/Lainchan.java @@ -23,27 +23,14 @@ import org.floens.chan.core.model.Post; import org.floens.chan.core.model.orm.Board; import org.floens.chan.core.model.orm.Loadable; import org.floens.chan.core.site.Site; -import org.floens.chan.core.site.SiteAuthentication; import org.floens.chan.core.site.SiteIcon; import org.floens.chan.core.site.common.CommonSite; -import org.floens.chan.core.site.common.MultipartHttpCall; -import org.floens.chan.core.site.common.vichan.VichanAntispam; +import org.floens.chan.core.site.common.vichan.VichanActions; import org.floens.chan.core.site.common.vichan.VichanApi; +import org.floens.chan.core.site.common.vichan.VichanCommentParser; import org.floens.chan.core.site.common.vichan.VichanEndpoints; -import org.floens.chan.core.site.http.Reply; -import org.floens.chan.core.site.http.ReplyResponse; -import org.floens.chan.core.site.parser.CommentParser; -import org.floens.chan.core.site.parser.StyleRule; -import org.jsoup.Jsoup; - -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import okhttp3.HttpUrl; -import okhttp3.Response; - -import static android.text.TextUtils.isEmpty; public class Lainchan extends CommonSite { public static final CommonSiteUrlHandler URL_HANDLER = new CommonSiteUrlHandler() { @@ -111,90 +98,8 @@ public class Lainchan extends CommonSite { setEndpoints(new VichanEndpoints(this, "https://lainchan.org", "https://lainchan.org")); - - setActions(new CommonActions() { - @Override - public void setupPost(Reply reply, MultipartHttpCall call) { - call.parameter("board", reply.loadable.board.code); - - if (reply.loadable.isThreadMode()) { - call.parameter("thread", String.valueOf(reply.loadable.no)); - } else { -// call.parameter("page", "1"); - } - - call.parameter("password", reply.password); - call.parameter("name", reply.name); - call.parameter("email", reply.options); - - if (!reply.loadable.isThreadMode() && !isEmpty(reply.subject)) { - call.parameter("subject", reply.subject); - } - - call.parameter("body", reply.comment); - - if (reply.file != null) { - call.fileParameter("file", reply.fileName, reply.file); - } - - if (reply.spoilerImage) { - call.parameter("spoiler", "on"); - } - } - - @Override - public boolean requirePrepare() { - return true; - } - - @Override - public void prepare(MultipartHttpCall call, Reply reply, ReplyResponse replyResponse) { - VichanAntispam antispam = new VichanAntispam( - HttpUrl.parse(resolvable().desktopUrl(reply.loadable, null))); - antispam.addDefaultIgnoreFields(); - for (Map.Entry e : antispam.get().entrySet()) { - call.parameter(e.getKey(), e.getValue()); - } - } - - @Override - public void handlePost(ReplyResponse replyResponse, Response response, String result) { - Matcher auth = Pattern.compile(".*\"captcha\": ?true.*").matcher(result); - Matcher err = Pattern.compile(".*]*>Error.*]*>(.*?).*").matcher(result); - if (auth.find()) { - replyResponse.requireAuthentication = true; - replyResponse.errorMessage = result; - } else if (err.find()) { - replyResponse.errorMessage = Jsoup.parse(err.group(1)).body().text(); - } else { - HttpUrl url = response.request().url(); - Matcher m = Pattern.compile("/\\w+/\\w+/(\\d+).html").matcher(url.encodedPath()); - try { - if (m.find()) { - replyResponse.threadNo = Integer.parseInt(m.group(1)); - replyResponse.postNo = Integer.parseInt(url.encodedFragment()); - replyResponse.posted = true; - } - } catch (NumberFormatException ignored) { - replyResponse.errorMessage = "Error posting: could not find posted thread."; - } - } - } - - @Override - public SiteAuthentication postAuthenticate() { - return SiteAuthentication.fromNone(); - } - }); - + setActions(new VichanActions(this)); setApi(new VichanApi(this)); - - CommentParser commentParser = new CommentParser(); - commentParser.addDefaultRules(); - commentParser.setQuotePattern(Pattern.compile(".*#(\\d+)")); - commentParser.setFullQuotePattern(Pattern.compile("/(\\w+)/\\w+/(\\d+)\\.html#(\\d+)")); - commentParser.rule(StyleRule.tagRule("p").cssClass("quote").color(StyleRule.Color.INLINE_QUOTE).linkify()); - - setParser(commentParser); + setParser(new VichanCommentParser()); } }