Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
feat(YouTube - Hide feed components): Remove Duration filter settin…
Browse files Browse the repository at this point in the history
…g as it no longer works due to server side changes
  • Loading branch information
inotia00 authored and anddea committed Sep 16, 2024
1 parent 995bfc6 commit 3f0f0da
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@
import app.revanced.integrations.shared.patches.components.StringFilterGroup;
import app.revanced.integrations.youtube.settings.Settings;

/**
* @noinspection ALL
*/
@SuppressWarnings("all")
public final class FeedVideoViewsFilter extends Filter {
private final StringFilterGroup feedVideoFilter;

public FeedVideoViewsFilter() {
feedVideoFilter = new StringFilterGroup(
null, // Multiple settings are used and must be individually checked if active.
final StringFilterGroup feedVideoFilter = new StringFilterGroup(
Settings.HIDE_VIDEO_BY_VIEW_COUNTS,
"video_with_context.eml",
"video_lockup_with_attachment.eml"
);
Expand All @@ -31,7 +27,7 @@ public FeedVideoViewsFilter() {
@Override
public boolean isFiltered(String path, @Nullable String identifier, String allValue, byte[] protobufBufferArray,
StringFilterGroup matchedGroup, FilterContentType contentType, int contentIndex) {
if (filterVideos(protobufBufferArray)) {
if (filterByViews(protobufBufferArray)) {
return super.isFiltered(path, identifier, allValue, protobufBufferArray, matchedGroup, contentType, contentIndex);
}

Expand All @@ -43,78 +39,11 @@ public boolean isFiltered(String path, @Nullable String identifier, String allVa
private final String[] parts = Settings.HIDE_VIDEO_VIEW_COUNTS_MULTIPLIER.get().split("\\n");
private Pattern[] viewCountPatterns = null;

private boolean filterVideos(byte[] protobufBufferArray) {
final String protobufString = new String(protobufBufferArray);

boolean hideBasedOnDuration = false;
boolean hideBasedOnViews = false;

if (Settings.HIDE_VIDEO_BY_DURATION.get())
hideBasedOnDuration = filterByDuration(protobufString);

if (Settings.HIDE_VIDEO_BY_VIEW_COUNTS.get())
hideBasedOnViews = filterByViews(protobufString);

return hideBasedOnDuration || hideBasedOnViews;
}

/**
* Hide videos based on duration
*/
private boolean filterByDuration(String protobufString) {
Pattern durationPattern = getDurationPattern();

String shorterThanStr = Settings.HIDE_VIDEO_BY_DURATION_SHORTER_THAN.get();
String longerThanStr = Settings.HIDE_VIDEO_BY_DURATION_LONGER_THAN.get();

long shorterThan = parseDuration(shorterThanStr);
long longerThan = parseDuration(longerThanStr);

Matcher matcher = durationPattern.matcher(protobufString);
if (matcher.find()) {
String durationString = Objects.requireNonNull(matcher.group());
long durationInSeconds = convertToSeconds(durationString);
return checkDuration(durationInSeconds, shorterThan, longerThan);
}

return false;
}

private long parseDuration(String durationString) {
if (durationString.contains(":")) {
return convertToSeconds(durationString);
} else {
return Long.parseLong(durationString);
}
}

private Pattern getDurationPattern() {
// Pattern: hours? : minutes : seconds
return Pattern.compile("(?:(\\d+):)?(\\d+):(\\d+)");
}

private long convertToSeconds(String durationString) {
String[] parts = durationString.split(":");
int length = parts.length;
long seconds = 0;
for (int i = length - 1; i >= 0; i--) {
long value = Long.parseLong(parts[i]);
seconds += (long) (value * Math.pow(60, length - 1 - i));
}
return seconds;
}

private boolean checkDuration(long durationInSeconds, long shorterThan, long longerThan) {
if (shorterThan < 0 || longerThan < 0)
throw new IllegalArgumentException("Duration cannot be negative.");

return durationInSeconds < shorterThan || durationInSeconds > longerThan;
}

/**
* Hide videos badsed on views count
* Hide videos based on views count
*/
private synchronized boolean filterByViews(String protobufString) {
private synchronized boolean filterByViews(byte[] protobufBufferArray) {
final String protobufString = new String(protobufBufferArray);
final long lessThan = Settings.HIDE_VIDEO_VIEW_COUNTS_LESS_THAN.get();
final long greaterThan = Settings.HIDE_VIDEO_VIEW_COUNTS_GREATER_THAN.get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ public class Settings extends BaseSettings {
public static final BooleanSetting HIDE_RECOMMENDED_VIDEO = new BooleanSetting("revanced_hide_recommended_video", FALSE);
public static final BooleanSetting HIDE_LOW_VIEWS_VIDEO = new BooleanSetting("revanced_hide_low_views_video", FALSE);

public static final BooleanSetting HIDE_VIDEO_BY_DURATION = new BooleanSetting("revanced_hide_video_by_duration", FALSE);
public static final StringSetting HIDE_VIDEO_BY_DURATION_SHORTER_THAN = new StringSetting("revanced_hide_video_by_duration_shorter_than", "0:10", parent(HIDE_VIDEO_BY_DURATION));
public static final StringSetting HIDE_VIDEO_BY_DURATION_LONGER_THAN = new StringSetting("revanced_hide_video_by_duration_longer_than", "10:10:10:10", parent(HIDE_VIDEO_BY_DURATION));

public static final BooleanSetting HIDE_VIDEO_BY_VIEW_COUNTS = new BooleanSetting("revanced_hide_video_by_view_counts", FALSE);
public static final LongSetting HIDE_VIDEO_VIEW_COUNTS_LESS_THAN = new LongSetting("revanced_hide_video_view_counts_less_than", 1000L, parent(HIDE_VIDEO_BY_VIEW_COUNTS));
public static final LongSetting HIDE_VIDEO_VIEW_COUNTS_GREATER_THAN = new LongSetting("revanced_hide_video_view_counts_greater_than", 1_000_000_000_000L, parent(HIDE_VIDEO_BY_VIEW_COUNTS));
Expand Down

0 comments on commit 3f0f0da

Please sign in to comment.