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:
parent
c8899e6aa5
commit
0988bd7afb
4 changed files with 58 additions and 29 deletions
|
@ -45,7 +45,6 @@ public class PlayerController {
|
||||||
private static long currentVideoLength = 1L;
|
private static long currentVideoLength = 1L;
|
||||||
private static long lastKnownVideoTime = -1L;
|
private static long lastKnownVideoTime = -1L;
|
||||||
private static final Runnable findAndSkipSegmentRunnable = () -> {
|
private static final Runnable findAndSkipSegmentRunnable = () -> {
|
||||||
// Log.d(TAG, "findAndSkipSegmentRunnable");
|
|
||||||
findAndSkipSegment(false);
|
findAndSkipSegment(false);
|
||||||
};
|
};
|
||||||
private static float sponsorBarLeft = 1f;
|
private static float sponsorBarLeft = 1f;
|
||||||
|
@ -89,7 +88,7 @@ public class PlayerController {
|
||||||
sponsorTimer.schedule(new TimerTask() {
|
sponsorTimer.schedule(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
executeDownloadSegments(currentVideoId);
|
executeDownloadSegments(currentVideoId, context);
|
||||||
}
|
}
|
||||||
}, 0);
|
}, 0);
|
||||||
}
|
}
|
||||||
|
@ -124,8 +123,8 @@ public class PlayerController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void executeDownloadSegments(String videoId) {
|
public static void executeDownloadSegments(String videoId, Context context) {
|
||||||
SponsorSegment[] segments = Requester.getSegments(videoId);
|
SponsorSegment[] segments = Requester.getSegments(videoId, context);
|
||||||
Arrays.sort(segments);
|
Arrays.sort(segments);
|
||||||
|
|
||||||
if (VERBOSE)
|
if (VERBOSE)
|
||||||
|
|
|
@ -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_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_SHOW_TIME_WITHOUT_SEGMENTS = "sb-length-without-segments";
|
||||||
public static final String PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX = "_color";
|
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;
|
public static final SegmentBehaviour DEFAULT_BEHAVIOR = SegmentBehaviour.SKIP_AUTOMATICALLY;
|
||||||
|
|
||||||
|
|
|
@ -71,7 +71,6 @@ public abstract class SponsorBlockUtils {
|
||||||
public static final SimpleDateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT);
|
public static final SimpleDateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT);
|
||||||
public static boolean videoHasSegments = false;
|
public static boolean videoHasSegments = false;
|
||||||
public static String timeWithoutSegments = "";
|
public static String timeWithoutSegments = "";
|
||||||
private static final int sponsorBtnId = 1234;
|
|
||||||
public static final View.OnClickListener sponsorBlockBtnListener = v -> {
|
public static final View.OnClickListener sponsorBlockBtnListener = v -> {
|
||||||
if (debug) {
|
if (debug) {
|
||||||
Log.d(TAG, "Shield button clicked");
|
Log.d(TAG, "Shield button clicked");
|
||||||
|
@ -153,7 +152,7 @@ public abstract class SponsorBlockUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (videoId != null)
|
if (videoId != null)
|
||||||
PlayerController.executeDownloadSegments(videoId);
|
PlayerController.executeDownloadSegments(videoId, appContext.get());
|
||||||
};
|
};
|
||||||
private static final DialogInterface.OnClickListener segmentCategorySelectedDialogListener = (dialog, which) -> {
|
private static final DialogInterface.OnClickListener segmentCategorySelectedDialogListener = (dialog, which) -> {
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
|
@ -440,7 +439,7 @@ public abstract class SponsorBlockUtils {
|
||||||
return totalTime;
|
return totalTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getTimeWithoutSegments(SponsorSegment[] sponsorSegmentsOfCurrentVideo) {
|
private static String getTimeWithoutSegments(SponsorSegment[] sponsorSegmentsOfCurrentVideo) {
|
||||||
long currentVideoLength = getCurrentVideoLength();
|
long currentVideoLength = getCurrentVideoLength();
|
||||||
if (!isSettingEnabled(showTimeWithoutSegments) || sponsorSegmentsOfCurrentVideo == null || currentVideoLength <= 1) {
|
if (!isSettingEnabled(showTimeWithoutSegments) || sponsorSegmentsOfCurrentVideo == null || currentVideoLength <= 1) {
|
||||||
return "";
|
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) {
|
public static boolean isSettingEnabled(boolean setting) {
|
||||||
return isSponsorBlockEnabled && setting;
|
return isSponsorBlockEnabled && setting;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import static pl.jakubweg.SponsorBlockUtils.videoHasSegments;
|
||||||
import static pl.jakubweg.StringRef.str;
|
import static pl.jakubweg.StringRef.str;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.preference.Preference;
|
import android.preference.Preference;
|
||||||
|
@ -36,40 +37,41 @@ public class Requester {
|
||||||
|
|
||||||
private Requester() {}
|
private Requester() {}
|
||||||
|
|
||||||
public static synchronized SponsorSegment[] getSegments(String videoId) {
|
public static synchronized SponsorSegment[] getSegments(String videoId, Context context) {
|
||||||
List<SponsorSegment> segments = new ArrayList<>();
|
videoHasSegments = false;
|
||||||
|
timeWithoutSegments = "";
|
||||||
|
|
||||||
|
List<SponsorSegment> segmentList = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
HttpURLConnection connection = getConnectionFromRoute(Route.GET_SEGMENTS, videoId, SponsorBlockSettings.sponsorBlockUrlCategories);
|
HttpURLConnection connection = getConnectionFromRoute(Route.GET_SEGMENTS, videoId, SponsorBlockSettings.sponsorBlockUrlCategories);
|
||||||
int responseCode = connection.getResponseCode();
|
int responseCode = connection.getResponseCode();
|
||||||
videoHasSegments = false;
|
|
||||||
timeWithoutSegments = "";
|
|
||||||
|
|
||||||
if (responseCode == 200) {
|
if (responseCode == 200) {
|
||||||
JSONArray responseArray = new JSONArray(parseJson(connection));
|
JSONArray responseArray = new JSONArray(parseJson(connection));
|
||||||
int length = responseArray.length();
|
SponsorBlockUtils.parseAndInsertSegments(responseArray, segmentList);
|
||||||
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]));
|
|
||||||
}
|
}
|
||||||
connection.disconnect();
|
connection.disconnect();
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
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) {
|
public static void submitSegments(String videoId, String uuid, float startTime, float endTime, String category, Runnable toastRunnable) {
|
||||||
|
|
Loading…
Reference in a new issue