0
0
Fork 0
mirror of https://github.com/YTVanced/Integrations synced 2024-11-27 21:53:01 +00:00

Removed useless caching functionality

This commit is contained in:
jakweg 2020-08-25 13:46:20 +02:00
parent 999a8c481a
commit 062b50f924
6 changed files with 8 additions and 157 deletions

View file

@ -75,7 +75,7 @@ public class PlayerController {
sponsorTimer.schedule(new TimerTask() {
@Override
public void run() {
executeDownloadSegments(currentVideoId, false);
executeDownloadSegments(currentVideoId);
}
}, 0);
}
@ -114,8 +114,8 @@ public class PlayerController {
}
}
public static void executeDownloadSegments(String videoId, boolean ignoreCache) {
SponsorSegment[] segments = SponsorBlockUtils.getSegmentsForVideo(videoId, ignoreCache);
public static void executeDownloadSegments(String videoId) {
SponsorSegment[] segments = SponsorBlockUtils.getSegmentsForVideo(videoId);
Arrays.sort(segments);
if (VERBOSE)

View file

@ -21,7 +21,6 @@ import java.util.ArrayList;
import static pl.jakubweg.SponsorBlockSettings.DefaultBehaviour;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_CACHE_SEGMENTS;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_COUNT_SKIPS;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_NEW_SEGMENT_ENABLED;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_SHOW_TOAST_WHEN_SKIP;
@ -29,7 +28,6 @@ import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_SPONSOR_BLOCK_ENA
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_UUID;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_NAME;
import static pl.jakubweg.SponsorBlockSettings.adjustNewSegmentMillis;
import static pl.jakubweg.SponsorBlockSettings.cacheEnabled;
import static pl.jakubweg.SponsorBlockSettings.countSkips;
import static pl.jakubweg.SponsorBlockSettings.showToastWhenSkippedAutomatically;
import static pl.jakubweg.SponsorBlockSettings.uuid;
@ -210,38 +208,6 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
screen.addPreference(preference);
preferencesToDisableWhenSBDisabled.add(preference);
}
{
Preference preference = new SwitchPreference(context);
preference.setTitle(str("general_cache"));
preference.setSummary(str("general_cache_sum"));
preference.setKey(PREFERENCES_KEY_CACHE_SEGMENTS);
preference.setDefaultValue(cacheEnabled);
screen.addPreference(preference);
preferencesToDisableWhenSBDisabled.add(preference);
}
{
Preference preference = new Preference(context);
preference.setTitle(str("general_cache_clear"));
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
File cacheDirectory = SponsorBlockSettings.cacheDirectory;
if (cacheDirectory != null) {
for (File file : cacheDirectory.listFiles()) {
if (!file.delete())
return false;
}
Toast.makeText(getActivity(), str("done"), Toast.LENGTH_SHORT).show();
}
return false;
}
});
preferencesToDisableWhenSBDisabled.add(preference);
screen.addPreference(preference);
}
}
@Override

View file

@ -22,7 +22,6 @@ public class SponsorBlockSettings {
public static final String PREFERENCES_KEY_SHOW_TOAST_WHEN_SKIP = "show-toast";
public static final String PREFERENCES_KEY_COUNT_SKIPS = "count-skips";
public static final String PREFERENCES_KEY_UUID = "uuid";
public static final String PREFERENCES_KEY_CACHE_SEGMENTS = "cache-enabled";
public static final String PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP = "new-segment-step-accuracy";
public static final String PREFERENCES_KEY_SPONSOR_BLOCK_ENABLED = "sb-enabled";
public static final String PREFERENCES_KEY_NEW_SEGMENT_ENABLED = "sb-new-segment-enabled";
@ -36,10 +35,8 @@ public class SponsorBlockSettings {
public static boolean isAddNewSegmentEnabled = false;
public static boolean showToastWhenSkippedAutomatically = true;
public static boolean countSkips = true;
public static boolean cacheEnabled = true;
public static int adjustNewSegmentMillis = 150;
public static String uuid = "<invalid>";
public static File cacheDirectory;
private static String sponsorBlockUrlCategories = "[]";
@SuppressWarnings("unused")
@ -58,11 +55,6 @@ public class SponsorBlockSettings {
public static void update(Context context) {
if (context == null) return;
File directory = cacheDirectory = new File(context.getCacheDir(), CACHE_DIRECTORY_NAME);
if (!directory.mkdirs() && !directory.exists()) {
Log.e("jakubweg.Settings", "Unable to create cache directory");
cacheDirectory = null;
}
SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
isSponsorBlockEnabled = preferences.getBoolean(PREFERENCES_KEY_SPONSOR_BLOCK_ENABLED, isSponsorBlockEnabled);
@ -114,10 +106,9 @@ public class SponsorBlockSettings {
showToastWhenSkippedAutomatically = preferences.getBoolean(PREFERENCES_KEY_SHOW_TOAST_WHEN_SKIP, showToastWhenSkippedAutomatically);
cacheEnabled = preferences.getBoolean(PREFERENCES_KEY_CACHE_SEGMENTS, true);
adjustNewSegmentMillis = Integer.parseInt(preferences
.getString(PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP,
String.valueOf(adjustNewSegmentMillis)));
String tmp1 = preferences.getString(PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP, null);
if (tmp1 != null)
adjustNewSegmentMillis = Integer.parseInt(tmp1);
uuid = preferences.getString(PREFERENCES_KEY_UUID, null);

View file

@ -250,7 +250,7 @@ public abstract class SponsorBlockUtils {
}
if (videoId != null)
PlayerController.executeDownloadSegments(videoId, true);
PlayerController.executeDownloadSegments(videoId);
}
};
@ -453,70 +453,9 @@ public abstract class SponsorBlockUtils {
}
}
public synchronized static SponsorSegment[] getSegmentsForVideo(String videoId, boolean ignoreCache) {
public synchronized static SponsorSegment[] getSegmentsForVideo(String videoId) {
newSponsorSegmentEndMillis = newSponsorSegmentStartMillis = -1;
int usageCounter = 0;
if (!ignoreCache && SponsorBlockSettings.cacheEnabled) {
File cacheDirectory = SponsorBlockSettings.cacheDirectory;
if (cacheDirectory == null) {
Log.w(TAG, "Cache directory is null, cannot read");
} else {
File file = new File(cacheDirectory, videoId);
try {
RandomAccessFile rwd = new RandomAccessFile(file, "rw");
rwd.seek(0);
usageCounter = rwd.readInt();
long now = System.currentTimeMillis();
long savedTimestamp = rwd.readLong();
int segmentsSize = rwd.readInt();
byte maxDaysCache;
if (usageCounter < 2)
maxDaysCache = 0;
else if (usageCounter < 5 || segmentsSize == 0)
maxDaysCache = 2;
else if (usageCounter < 10)
maxDaysCache = 5;
else
maxDaysCache = 10;
if (VERBOSE)
Log.d(TAG, String.format("Read cache data about segments, counter=%d, timestamp=%d, now=%d, maxCacheDays=%s, segmentsSize=%d",
usageCounter, savedTimestamp, now, maxDaysCache, segmentsSize));
if (savedTimestamp + (((long) maxDaysCache) * 24 * 60 * 60 * 1000) > now) {
if (VERBOSE)
Log.d(TAG, "getSegmentsForVideo: cacheHonored videoId=" + videoId);
SponsorSegment[] segments = new SponsorSegment[segmentsSize];
for (int i = 0; i < segmentsSize; i++) {
segments[i] = SponsorSegment.readFrom(rwd);
}
rwd.seek(0);
rwd.writeInt(usageCounter + 1);
rwd.close();
if (VERBOSE)
Log.d(TAG, "getSegmentsForVideo: reading from cache and updating usageCounter finished");
return segments;
} else {
if (VERBOSE)
Log.d(TAG, "getSegmentsForVideo: cache of video " + videoId + " was not honored, fallback to downloading...");
}
} catch (FileNotFoundException | EOFException ignored) {
if (VERBOSE)
Log.e(TAG, "FileNotFoundException | EOFException ignored");
} catch (Exception e) {
//noinspection ResultOfMethodCallIgnored
file.delete();
Log.e(TAG, "Error while reading cached segments", e);
}
}
}
ArrayList<SponsorSegment> sponsorSegments = new ArrayList<>();
try {
if (VERBOSE)
@ -569,29 +508,6 @@ public abstract class SponsorBlockUtils {
connection.disconnect();
if (SponsorBlockSettings.cacheEnabled) {
File cacheDirectory = SponsorBlockSettings.cacheDirectory;
if (cacheDirectory == null) {
Log.w(TAG, "Cache directory is null");
} else {
File file = new File(cacheDirectory, videoId);
try {
DataOutputStream stream = new DataOutputStream(new FileOutputStream(file));
stream.writeInt(usageCounter + 1);
stream.writeLong(System.currentTimeMillis());
stream.writeInt(sponsorSegments.size());
for (SponsorSegment segment : sponsorSegments) {
segment.writeTo(stream);
}
stream.close();
} catch (Exception e) {
//noinspection ResultOfMethodCallIgnored
file.delete();
Log.e(TAG, "Unable to write segments to file", e);
}
}
}
} catch (Exception e) {
Log.e(TAG, "download segments failed", e);
}

View file

@ -1,9 +1,5 @@
package pl.jakubweg;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
public class SponsorSegment implements Comparable<SponsorSegment> {
public final long start;
public final long end;
@ -17,15 +13,6 @@ public class SponsorSegment implements Comparable<SponsorSegment> {
this.UUID = UUID;
}
public static SponsorSegment readFrom(RandomAccessFile stream) throws IOException {
long start = stream.readLong();
long end = stream.readLong();
String categoryName = stream.readUTF();
String UUID = stream.readUTF();
SponsorBlockSettings.SegmentInfo category = SponsorBlockSettings.SegmentInfo.valueOf(categoryName);
return new SponsorSegment(start, end, category, UUID);
}
@Override
public String toString() {
return "SegmentInfo{" +
@ -40,10 +27,4 @@ public class SponsorSegment implements Comparable<SponsorSegment> {
return (int) (this.start - o.start);
}
public void writeTo(DataOutputStream stream) throws IOException {
stream.writeLong(start);
stream.writeLong(end);
stream.writeUTF(category.name());
stream.writeUTF(UUID);
}
}

View file

@ -153,9 +153,6 @@
<string name="general_adjusting_sum">This is the number of milliseconds you can move when you use the time adjustment buttons while adding new segment</string>
<string name="general_uuid">Your unique user id</string>
<string name="general_uuid_sum">This should be kept private. This is like a password and should not be shared with anyone. If someone has this, they can impersonate you</string>
<string name="general_cache">Cache segments locally</string>
<string name="general_cache_sum">Frequently watched videos (eg. music videos) may store segments in app cache to make skipping segments faster</string>
<string name="general_cache_clear">Clear sponsor block segments cache</string>
<string name="segments_sponsor">Sponsor</string>
<string name="segments_sponsor_sum">Paid promotion, paid referrals and direct advertisements</string>
<string name="segments_intermission">Intermission/Intro Animation</string>