Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

过滤垃圾评论 相关 #282

Merged
merged 2 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions app/src/main/java/ceui/lisa/helper/CommentFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.text.TextUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -10,33 +11,70 @@
import java.util.stream.Collectors;

import ceui.lisa.activities.Shaft;
import ceui.lisa.http.NullCtrl;
import ceui.lisa.http.Retro;
import ceui.lisa.models.CommentsBean;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

public class CommentFilter {

private static final List<CommentFilterRule> rules = getRules();
private static List<CommentFilterRule> rules = new ArrayList<>();

static {
updateRules();
}

public static boolean judge(CommentsBean commentsBean) {
return rules.stream()
.anyMatch(rule -> rule.judge(commentsBean.getComment()));
}

public static List<CommentFilterRule> getRules() {
List<CommentFilterRule> rules = new ArrayList<>();
private static void updateRules() {
updateRulesFromLocal();
updateRulesFromRemote();
}

private static void updateRulesFromLocal() {
try {
InputStream inputStream = Shaft.getContext().getAssets().open("comment.filter.rule.txt");
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
String content = new String(buffer);
return Arrays.stream(content.split("\\r?\\n", -1))
rules = Arrays.stream(content.split("\\r?\\n", -1))
.filter(string -> !TextUtils.isEmpty(string))
.map(CommentFilterRule::new)
.collect(Collectors.toList());
} catch (Exception e) {
e.printStackTrace();
}
return rules;
}

private static void updateRulesFromRemote() {

Retro.getResourceApi().getCommentFilterRule()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new NullCtrl<ResponseBody>() {
@Override
public void success(ResponseBody responseBody) {
try {
String content = responseBody.string();
if (TextUtils.isEmpty(content)) {
return;
}

rules = Arrays.stream(content.split("\\r?\\n", -1))
.filter(string -> !TextUtils.isEmpty(string))
.map(CommentFilterRule::new)
.collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}

private static class CommentFilterRule {
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/ceui/lisa/http/ResourceApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ceui.lisa.http;

import io.reactivex.Observable;
import okhttp3.ResponseBody;
import retrofit2.http.GET;

public interface ResourceApi {
String JSDELIVR_BASE_URL = "https://cdn.jsdelivr.net/";
// String JSDELIVR_BASE_URL = "https://raw.githubusercontent.com/";

@GET("gh/CeuiLiSA/Pixiv-Shaft/app/src/main/assets/comment.filter.rule.txt")
// @GET("CeuiLiSA/Pixiv-Shaft/master/app/src/main/assets/comment.filter.rule.txt")
Observable<ResponseBody> getCommentFilterRule();
}
16 changes: 15 additions & 1 deletion app/src/main/java/ceui/lisa/http/Retro.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.blankj.utilcode.util.DeviceUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.safframework.http.interceptor.LoggingInterceptor;

import java.security.cert.X509Certificate;
import java.util.Collections;
Expand All @@ -23,6 +22,7 @@

import static ceui.lisa.http.AccountApi.ACCOUNT_BASE_URL;
import static ceui.lisa.http.AppApi.API_BASE_URL;
import static ceui.lisa.http.ResourceApi.JSDELIVR_BASE_URL;
import static ceui.lisa.http.SignApi.SIGN_API;

public class Retro {
Expand All @@ -39,6 +39,10 @@ public static AccountApi getAccountApi() {
return buildRetrofit(ACCOUNT_BASE_URL).create(AccountApi.class);
}

public static ResourceApi getResourceApi(){
return buildPlainRetrofit(JSDELIVR_BASE_URL).create(ResourceApi.class);
}

private static Request.Builder addHeader(Request.Builder before) {
PixivHeaders pixivHeaders = new PixivHeaders();
String osVersion = DeviceUtils.getSDKVersionName();
Expand Down Expand Up @@ -73,6 +77,16 @@ private static Retrofit buildRetrofit(String baseUrl) {
.build();
}

private static Retrofit buildPlainRetrofit(String baseUrl){
OkHttpClient.Builder builder = getLogClient();
OkHttpClient client = builder.build();
return new Retrofit.Builder()
.client(client)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(baseUrl)
.build();
}

public static <T> T create(String baseUrl, final Class<T> service) {
Gson gson = new GsonBuilder().setLenient().create();
Retrofit retrofit = new Retrofit.Builder()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/ceui/lisa/utils/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void setCurrentProgress(long currentProgress) {

private int downloadWay = 0; //0传统模式,保存到Pictures目录下。 1 SAF模式保存到自选目录下

private boolean filterComment = true;
private boolean filterComment = false; // 过滤垃圾评论,默认不开启

public String getAppLanguage() {
if(!TextUtils.isEmpty(appLanguage)){
Expand Down