0
0
Fork 0
mirror of https://github.com/YTVanced/Integrations synced 2024-11-27 05:33:00 +00:00

add base for local segments

This commit is contained in:
caneleex 2021-08-19 14:08:28 +02:00
parent c8899e6aa5
commit 0988bd7afb
4 changed files with 58 additions and 29 deletions

View file

@ -45,7 +45,6 @@ public class PlayerController {
private static long currentVideoLength = 1L;
private static long lastKnownVideoTime = -1L;
private static final Runnable findAndSkipSegmentRunnable = () -> {
// Log.d(TAG, "findAndSkipSegmentRunnable");
findAndSkipSegment(false);
};
private static float sponsorBarLeft = 1f;
@ -89,7 +88,7 @@ public class PlayerController {
sponsorTimer.schedule(new TimerTask() {
@Override
public void run() {
executeDownloadSegments(currentVideoId);
executeDownloadSegments(currentVideoId, context);
}
}, 0);
}
@ -124,8 +123,8 @@ public class PlayerController {
}
}
public static void executeDownloadSegments(String videoId) {
SponsorSegment[] segments = Requester.getSegments(videoId);
public static void executeDownloadSegments(String videoId, Context context) {
SponsorSegment[] segments = Requester.getSegments(videoId, context);
Arrays.sort(segments);
if (VERBOSE)

View file

@ -29,6 +29,7 @@ public class SponsorBlockSettings {
public static final String PREFERENCES_KEY_SKIPPED_SEGMENTS_TIME = "sb-skipped-segments-time";
public static final String PREFERENCES_KEY_SHOW_TIME_WITHOUT_SEGMENTS = "sb-length-without-segments";
public static final String PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX = "_color";
public static final String PREFERENCES_KEY_VIDEO_SEGMENTS_PREFIX = "video_";
public static final SegmentBehaviour DEFAULT_BEHAVIOR = SegmentBehaviour.SKIP_AUTOMATICALLY;

View file

@ -71,7 +71,6 @@ public abstract class SponsorBlockUtils {
public static final SimpleDateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT);
public static boolean videoHasSegments = false;
public static String timeWithoutSegments = "";
private static final int sponsorBtnId = 1234;
public static final View.OnClickListener sponsorBlockBtnListener = v -> {
if (debug) {
Log.d(TAG, "Shield button clicked");
@ -153,7 +152,7 @@ public abstract class SponsorBlockUtils {
}
if (videoId != null)
PlayerController.executeDownloadSegments(videoId);
PlayerController.executeDownloadSegments(videoId, appContext.get());
};
private static final DialogInterface.OnClickListener segmentCategorySelectedDialogListener = (dialog, which) -> {
dialog.dismiss();
@ -440,7 +439,7 @@ public abstract class SponsorBlockUtils {
return totalTime;
}
public static String getTimeWithoutSegments(SponsorSegment[] sponsorSegmentsOfCurrentVideo) {
private static String getTimeWithoutSegments(SponsorSegment[] sponsorSegmentsOfCurrentVideo) {
long currentVideoLength = getCurrentVideoLength();
if (!isSettingEnabled(showTimeWithoutSegments) || sponsorSegmentsOfCurrentVideo == null || currentVideoLength <= 1) {
return "";
@ -629,6 +628,34 @@ public abstract class SponsorBlockUtils {
}
}
public static void parseAndInsertSegments(JSONArray segmentArrayJson, List<SponsorSegment> segmentList) {
int length = segmentArrayJson.length();
try {
for (int i = 0; i < length; i++) {
JSONObject obj = segmentArrayJson.getJSONObject(i);
JSONArray segment = obj.getJSONArray("segment");
long start = (long) (segment.getDouble(0) * 1000);
long end = (long) (segment.getDouble(1) * 1000);
String category = obj.getString("category");
String uuid = obj.getString("UUID");
SponsorBlockSettings.SegmentCategory segmentCategory = SponsorBlockSettings.SegmentCategory.byCategoryKey(category);
if (segmentCategory != null) {
SponsorSegment sponsorSegment = new SponsorSegment(start, end, segmentCategory, uuid);
segmentList.add(sponsorSegment);
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void setTimeWithoutSegments(SponsorSegment[] segments) {
videoHasSegments = true;
timeWithoutSegments = getTimeWithoutSegments(segments);
}
public static boolean isSettingEnabled(boolean setting) {
return isSponsorBlockEnabled && setting;
}

View file

@ -5,6 +5,7 @@ import static pl.jakubweg.SponsorBlockUtils.videoHasSegments;
import static pl.jakubweg.StringRef.str;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.preference.Preference;
@ -36,40 +37,41 @@ public class Requester {
private Requester() {}
public static synchronized SponsorSegment[] getSegments(String videoId) {
List<SponsorSegment> segments = new ArrayList<>();
try {
HttpURLConnection connection = getConnectionFromRoute(Route.GET_SEGMENTS, videoId, SponsorBlockSettings.sponsorBlockUrlCategories);
int responseCode = connection.getResponseCode();
public static synchronized SponsorSegment[] getSegments(String videoId, Context context) {
videoHasSegments = false;
timeWithoutSegments = "";
List<SponsorSegment> segmentList = new ArrayList<>();
try {
HttpURLConnection connection = getConnectionFromRoute(Route.GET_SEGMENTS, videoId, SponsorBlockSettings.sponsorBlockUrlCategories);
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
JSONArray responseArray = new JSONArray(parseJson(connection));
int length = responseArray.length();
for (int i = 0; i < length; i++) {
JSONObject obj = (JSONObject) responseArray.get(i);
JSONArray segment = obj.getJSONArray("segment");
long start = (long) (segment.getDouble(0) * 1000);
long end = (long) (segment.getDouble(1) * 1000);
String category = obj.getString("category");
String uuid = obj.getString("UUID");
SponsorBlockSettings.SegmentCategory segmentCategory = SponsorBlockSettings.SegmentCategory.byCategoryKey(category);
if (segmentCategory != null) {
SponsorSegment sponsorSegment = new SponsorSegment(start, end, segmentCategory, uuid);
segments.add(sponsorSegment);
}
}
videoHasSegments = true;
timeWithoutSegments = SponsorBlockUtils.getTimeWithoutSegments(segments.toArray(new SponsorSegment[0]));
SponsorBlockUtils.parseAndInsertSegments(responseArray, segmentList);
}
connection.disconnect();
}
catch (Exception ex) {
ex.printStackTrace();
}
return segments.toArray(new SponsorSegment[0]);
SharedPreferences preferences = SponsorBlockSettings.getPreferences(context);
String localSegmentsJson = preferences.getString(SponsorBlockSettings.PREFERENCES_KEY_VIDEO_SEGMENTS_PREFIX + videoId, null);
if (localSegmentsJson != null) {
try {
SponsorBlockUtils.parseAndInsertSegments(new JSONArray(localSegmentsJson), segmentList);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
SponsorSegment[] segments = segmentList.toArray(new SponsorSegment[0]);
if (!segmentList.isEmpty()) {
SponsorBlockUtils.setTimeWithoutSegments(segments);
}
return segments;
}
public static void submitSegments(String videoId, String uuid, float startTime, float endTime, String category, Runnable toastRunnable) {