From 600a9da95358f564f288ec6aa299734612746c26 Mon Sep 17 00:00:00 2001 From: gdlbo <41114720+gdlbo@users.noreply.github.com> Date: Fri, 22 Jul 2022 14:42:25 +0300 Subject: [PATCH] speedup adblock --- .../ru/vtosters/lite/hooks/JsonInjectors.java | 126 ++++++++++++------ .../java/ru/vtosters/lite/utils/Newsfeed.java | 41 +++--- 2 files changed, 103 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/ru/vtosters/lite/hooks/JsonInjectors.java b/app/src/main/java/ru/vtosters/lite/hooks/JsonInjectors.java index 1e6d9dd70..1a4288063 100644 --- a/app/src/main/java/ru/vtosters/lite/hooks/JsonInjectors.java +++ b/app/src/main/java/ru/vtosters/lite/hooks/JsonInjectors.java @@ -11,8 +11,8 @@ import static ru.vtosters.lite.utils.Globals.getPrefsValue; import static ru.vtosters.lite.utils.Globals.getUserToken; import static ru.vtosters.lite.utils.Newsfeed.checkCaption; import static ru.vtosters.lite.utils.Newsfeed.checkCopyright; -import static ru.vtosters.lite.utils.Newsfeed.injectFiltersReposts; import static ru.vtosters.lite.utils.Newsfeed.isBadNews; +import static ru.vtosters.lite.utils.Newsfeed.mFiltersLinks; import static ru.vtosters.lite.utils.Preferences.ads; import static ru.vtosters.lite.utils.Preferences.adsgroup; import static ru.vtosters.lite.utils.Preferences.adsstories; @@ -231,13 +231,70 @@ public class JsonInjectors{ if (!story.optBoolean("is_ads") && !story.optBoolean("is_promo")) { newStories.put(story); } else { - if (dev()) Log.d("StoriesAds", "Fetched ad, owner id " + story.optString("owner_id") + ", caption " + story.optString("caption")); + if (dev()) + Log.d("StoriesAds", "Fetched ad, owner id " + story.optString("owner_id") + ", caption " + story.optString("caption")); } } item.put("stories", newStories); } + public static boolean parseRepostItem(JSONObject list) throws JSONException{ + var item = list.optJSONArray("copy_history"); + + if (item == null || !getBoolValue("cringerepost", false)) return false; + + for (int j = 0; j < item.length(); j++) { + var items = item.optJSONObject(j); + var text = items.optString("text"); + var type = items.optString("post_type"); + + for (String linkfilters : mFiltersLinks) { + if (text.contains(linkfilters)) { + if (dev()) + Log.d("RepostFilter", "Fetched repost ad, owner id " + items.optString("owner_id") + ", text: " + text); + return true; + } else if (isBadNews(text)) { + if (dev()) + Log.d("RepostFilter", "Fetched repost ad (isBadNews), owner id " + items.optString("owner_id") + ", text: " + text); + return true; + } else if (isAds(items, type)) { + if (dev()) + Log.d("RepostFilter", "Fetched repost ad (ads), owner id " + items.optString("owner_id") + ", text: " + text); + return true; + } + } + } + + return false; + } + + public static Boolean isAds(JSONObject list, String type) throws JSONException{ + if (list == null || type == null || !ads()) return false; + + if (list.has("ads") + || type.equals("ads") + || type.equals("carousel") + || type.equals("html5_ad") + || type.equals("ads_easy_promote")) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: ads"); + return true; + } + + if (type.equals("promo_button") + || type.equals("app_widget") + || type.equals("app_video") + || type.equals("app_slider") + || type.equals("tags_suggestions")) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: promo"); + return true; + } + + return false; + } + public static JSONArray newsfeedlist(JSONArray items) throws JSONException{ for (int j = 0; j < items.length(); j++) { var list = items.optJSONObject(j); @@ -260,69 +317,55 @@ public class JsonInjectors{ var list = items.optJSONObject(j); var type = list.optString("type"); - if (ads()) { - if (list.has("ads") - || type.equals("ads") - || type.equals("carousel") - || type.equals("html5_ad") - || type.equals("ads_easy_promote")) { - if (dev()) Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: ads"); - continue; - } - - if (type.equals("promo_button") - || type.equals("app_widget") - || type.equals("app_video") - || type.equals("app_slider") - || type.equals("tags_suggestions")) { - if (dev()) Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: promo"); - continue; - } + if (isAds(list, type)) { + continue; } - if (authorsrecomm()) { - if (type.equals("authors_rec") || type.startsWith("recommended_") && (type.endsWith("audios") || type.endsWith("artists") || type.endsWith("playlists") || type.endsWith("groups"))) { - if (dev()) Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: authorsrecomm"); - continue; - } + if (authorsrecomm() && (type.equals("authors_rec") || type.startsWith("recommended_") && (type.endsWith("audios") || type.endsWith("artists") || type.endsWith("playlists") || type.endsWith("groups")))) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: authorsrecomm"); + continue; } - if (postsrecomm()) { - if (type.equals("inline_user_rec") || type.equals("live_recommended")) { - if (dev()) Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: postsrecomm"); - continue; - } + if (postsrecomm() && (type.equals("inline_user_rec") || type.equals("live_recommended"))) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: postsrecomm"); + continue; } - if (friendsrecomm()) { - if (type.equals("user_rec") || type.equals("friends_recomm")) { - if (dev()) Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: friendsrecomm"); - continue; - } + if (friendsrecomm() && (type.equals("user_rec") || type.equals("friends_recomm"))) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: friendsrecomm"); + continue; } if (adsgroup() && list.optInt("marked_as_ads") == 1) { - if (dev()) Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: marked_as_ads is true"); + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: marked_as_ads is true"); continue; } if (isBadNews(list.optString("text"))) { - if (dev()) Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: text filters"); + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: text filters"); continue; } if (checkCopyright(list)) { - if (dev()) Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: copyright filters"); + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: copyright filters"); continue; } if (checkCaption(list)) { - if (dev()) Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: caption filters"); + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: caption filters"); continue; } - if (injectFiltersReposts(list)) { - if (dev()) Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: repost ad"); + if (parseRepostItem(list)) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: repost ad"); continue; } @@ -399,6 +442,7 @@ public class JsonInjectors{ return json; } + public static JSONObject fetchCatalogId(String section){ if (section == null) return null; diff --git a/app/src/main/java/ru/vtosters/lite/utils/Newsfeed.java b/app/src/main/java/ru/vtosters/lite/utils/Newsfeed.java index f15802e43..95acdedd6 100644 --- a/app/src/main/java/ru/vtosters/lite/utils/Newsfeed.java +++ b/app/src/main/java/ru/vtosters/lite/utils/Newsfeed.java @@ -2,9 +2,10 @@ package ru.vtosters.lite.utils; import static ru.vtosters.lite.utils.Globals.getContext; import static ru.vtosters.lite.utils.Globals.getPrefsValue; -import static ru.vtosters.lite.utils.Preferences.*; +import static ru.vtosters.lite.utils.Preferences.ads; import static ru.vtosters.lite.utils.Preferences.adsgroup; import static ru.vtosters.lite.utils.Preferences.authorsrecomm; +import static ru.vtosters.lite.utils.Preferences.captions; import static ru.vtosters.lite.utils.Preferences.copyright_post; import static ru.vtosters.lite.utils.Preferences.dev; import static ru.vtosters.lite.utils.Preferences.friendsrecomm; @@ -16,15 +17,15 @@ import android.util.Log; import org.json.JSONException; import org.json.JSONObject; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.Scanner; +import ru.vtosters.lite.hooks.JsonInjectors; + public class Newsfeed{ public static List mFilters; public static List mFiltersLinks; @@ -54,7 +55,7 @@ public class Newsfeed{ if (getBoolValue(boolname, true)) { try { var scanner = new Scanner(getContext().getAssets().open(filename)); - while (scanner.hasNextLine()) { + while(scanner.hasNextLine()) { var line = scanner.nextLine(); if (!line.isEmpty()) list.add(line.toLowerCase()); @@ -65,25 +66,17 @@ public class Newsfeed{ } } // Get needed filter list from assets - public static boolean injectFiltersReposts(JSONObject obj){ - + public static boolean injectFiltersReposts(JSONObject obj) throws JSONException{ if (getBoolValue("cringerepost", false) && obj.has("copy_history")) { - var Array = Objects.requireNonNull(obj.optJSONArray("copy_history")).toString(); - if (Array.isEmpty()) return false; - Array = Array.toLowerCase(); - for (String linkfilters : mFiltersLinks) { - if (Array.contains(linkfilters)) { - return true; - } - } - return isBadNews(Array); + return JsonInjectors.parseRepostItem(obj); } - return false; - } // get repost information and inject our text filters + } + + public static boolean checkCopyright(JSONObject json) throws JSONException{ + if (json.has("copyright")) { + if (copyright_post()) return true; - public static boolean checkCopyright(JSONObject json) throws JSONException { - if (copyright_post() && json.has("copyright")) { var copyright = json.optJSONObject("copyright"); if (copyright != null) { var copyrightlink = copyright.getString("link").toLowerCase(); @@ -127,9 +120,10 @@ public class Newsfeed{ public static boolean checkCaption(JSONObject postJson){ try { - if (captions() && postsrecomm()) { - var caption = postJson.optJSONObject("caption"); - if (caption != null) { + var caption = postJson.optJSONObject("caption"); + if (caption != null) { + if (captions()) return true; + if (postsrecomm()) { var type = caption.getString("type"); return type.equals("explorebait") || // Может быть интересно type.equals("shared") || // Поделился записью @@ -139,8 +133,9 @@ public class Newsfeed{ } } } catch (JSONException e) { - e.printStackTrace(); + Log.d("checkCaption", "Caption error"); } + return false; }