2022-06-24 00:16:32 +02:00
|
|
|
package app.revanced.integrations.sponsorblock.requests;
|
2021-07-19 16:19:47 +02:00
|
|
|
|
2022-01-24 13:45:07 +01:00
|
|
|
import static android.text.Html.fromHtml;
|
2022-06-24 00:16:32 +02:00
|
|
|
import static app.revanced.integrations.sponsorblock.SponsorBlockUtils.timeWithoutSegments;
|
|
|
|
import static app.revanced.integrations.sponsorblock.SponsorBlockUtils.videoHasSegments;
|
|
|
|
import static app.revanced.integrations.sponsorblock.StringRef.str;
|
2022-07-12 03:17:03 +02:00
|
|
|
import static app.revanced.integrations.utils.ReVancedUtils.runOnMainThread;
|
2021-08-10 13:55:50 +02:00
|
|
|
|
2021-07-19 16:19:47 +02:00
|
|
|
import android.content.Context;
|
2022-01-24 13:45:07 +01:00
|
|
|
import android.preference.EditTextPreference;
|
2021-07-23 14:45:10 +02:00
|
|
|
import android.preference.Preference;
|
|
|
|
import android.preference.PreferenceCategory;
|
2021-07-19 16:19:47 +02:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2021-08-10 13:55:50 +02:00
|
|
|
import java.util.Locale;
|
2022-02-07 16:15:22 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2021-07-19 16:19:47 +02:00
|
|
|
|
2022-07-14 18:42:43 +02:00
|
|
|
import app.revanced.integrations.whitelist.requests.Requester;
|
|
|
|
import app.revanced.integrations.whitelist.requests.Route;
|
2022-07-05 22:31:13 +02:00
|
|
|
import app.revanced.integrations.settings.SettingsEnum;
|
2022-06-24 00:16:32 +02:00
|
|
|
import app.revanced.integrations.sponsorblock.PlayerController;
|
|
|
|
import app.revanced.integrations.sponsorblock.SponsorBlockSettings;
|
|
|
|
import app.revanced.integrations.sponsorblock.SponsorBlockUtils;
|
|
|
|
import app.revanced.integrations.sponsorblock.SponsorBlockUtils.VoteOption;
|
|
|
|
import app.revanced.integrations.sponsorblock.objects.SponsorSegment;
|
|
|
|
import app.revanced.integrations.sponsorblock.objects.UserStats;
|
2021-07-19 16:19:47 +02:00
|
|
|
|
2022-01-17 14:54:11 +01:00
|
|
|
public class SBRequester {
|
2021-07-19 16:19:47 +02:00
|
|
|
private static final String TIME_TEMPLATE = "%.3f";
|
|
|
|
|
2022-06-24 00:16:32 +02:00
|
|
|
private SBRequester() {
|
|
|
|
}
|
2021-07-19 16:19:47 +02:00
|
|
|
|
|
|
|
public static synchronized SponsorSegment[] getSegments(String videoId) {
|
|
|
|
List<SponsorSegment> segments = new ArrayList<>();
|
|
|
|
try {
|
2022-01-17 14:54:11 +01:00
|
|
|
HttpURLConnection connection = getConnectionFromRoute(SBRoutes.GET_SEGMENTS, videoId, SponsorBlockSettings.sponsorBlockUrlCategories);
|
2021-07-19 16:19:47 +02:00
|
|
|
int responseCode = connection.getResponseCode();
|
2022-02-07 16:15:22 +01:00
|
|
|
runVipCheck();
|
2021-07-19 16:19:47 +02:00
|
|
|
|
2021-07-26 00:13:33 +02:00
|
|
|
if (responseCode == 200) {
|
2022-01-17 14:54:11 +01:00
|
|
|
JSONArray responseArray = Requester.getJSONArray(connection);
|
2021-07-26 00:13:33 +02:00
|
|
|
int length = responseArray.length();
|
|
|
|
for (int i = 0; i < length; i++) {
|
2022-01-17 14:54:11 +01:00
|
|
|
JSONObject obj = (JSONObject) responseArray.get(i);
|
2021-07-26 00:13:33 +02:00
|
|
|
JSONArray segment = obj.getJSONArray("segment");
|
|
|
|
long start = (long) (segment.getDouble(0) * 1000);
|
|
|
|
long end = (long) (segment.getDouble(1) * 1000);
|
2022-01-24 17:22:21 +01:00
|
|
|
|
2022-07-14 18:42:43 +02:00
|
|
|
long minDuration = (long) (SettingsEnum.SB_MIN_DURATION.getFloat() * 1000);
|
2022-01-24 17:22:21 +01:00
|
|
|
if ((end - start) < minDuration)
|
|
|
|
continue;
|
|
|
|
|
2021-07-26 00:13:33 +02:00
|
|
|
String category = obj.getString("category");
|
|
|
|
String uuid = obj.getString("UUID");
|
2022-02-07 16:15:22 +01:00
|
|
|
boolean locked = obj.getInt("locked") == 1;
|
2021-07-19 16:19:47 +02:00
|
|
|
|
2021-07-26 00:13:33 +02:00
|
|
|
SponsorBlockSettings.SegmentInfo segmentCategory = SponsorBlockSettings.SegmentInfo.byCategoryKey(category);
|
|
|
|
if (segmentCategory != null && segmentCategory.behaviour.showOnTimeBar) {
|
2022-02-07 16:15:22 +01:00
|
|
|
SponsorSegment sponsorSegment = new SponsorSegment(start, end, segmentCategory, uuid, locked);
|
2021-07-26 00:13:33 +02:00
|
|
|
segments.add(sponsorSegment);
|
2021-07-19 16:19:47 +02:00
|
|
|
}
|
2021-07-26 00:13:33 +02:00
|
|
|
}
|
2022-01-24 17:22:21 +01:00
|
|
|
if (!segments.isEmpty()) {
|
|
|
|
videoHasSegments = true;
|
|
|
|
timeWithoutSegments = SponsorBlockUtils.getTimeWithoutSegments(segments.toArray(new SponsorSegment[0]));
|
|
|
|
}
|
2021-07-19 16:19:47 +02:00
|
|
|
}
|
2022-01-23 22:46:12 +01:00
|
|
|
connection.disconnect();
|
2022-06-24 00:16:32 +02:00
|
|
|
} catch (Exception ex) {
|
2021-07-19 16:19:47 +02:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return segments.toArray(new SponsorSegment[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void submitSegments(String videoId, String uuid, float startTime, float endTime, String category, Runnable toastRunnable) {
|
|
|
|
try {
|
2021-08-10 13:55:50 +02:00
|
|
|
String start = String.format(Locale.US, TIME_TEMPLATE, startTime);
|
|
|
|
String end = String.format(Locale.US, TIME_TEMPLATE, endTime);
|
2022-01-28 15:47:58 +01:00
|
|
|
String duration = String.valueOf(PlayerController.getCurrentVideoLength() / 1000);
|
|
|
|
HttpURLConnection connection = getConnectionFromRoute(SBRoutes.SUBMIT_SEGMENTS, videoId, uuid, start, end, category, duration);
|
2021-07-19 16:19:47 +02:00
|
|
|
int responseCode = connection.getResponseCode();
|
|
|
|
|
|
|
|
switch (responseCode) {
|
|
|
|
case 200:
|
|
|
|
SponsorBlockUtils.messageToToast = str("submit_succeeded");
|
|
|
|
break;
|
|
|
|
case 409:
|
|
|
|
SponsorBlockUtils.messageToToast = str("submit_failed_duplicate");
|
|
|
|
break;
|
|
|
|
case 403:
|
2022-01-26 14:43:52 +01:00
|
|
|
SponsorBlockUtils.messageToToast = str("submit_failed_forbidden", Requester.parseErrorJson(connection));
|
2021-07-19 16:19:47 +02:00
|
|
|
break;
|
|
|
|
case 429:
|
|
|
|
SponsorBlockUtils.messageToToast = str("submit_failed_rate_limit");
|
|
|
|
break;
|
2022-09-19 00:04:39 +02:00
|
|
|
case 400:
|
2022-10-04 20:43:29 +02:00
|
|
|
SponsorBlockUtils.messageToToast = str("submit_failed_invalid", Requester.parseErrorJson(connection));
|
2022-09-19 00:04:39 +02:00
|
|
|
break;
|
2021-07-26 00:15:02 +02:00
|
|
|
default:
|
|
|
|
SponsorBlockUtils.messageToToast = str("submit_failed_unknown_error", responseCode, connection.getResponseMessage());
|
|
|
|
break;
|
2021-07-19 16:19:47 +02:00
|
|
|
}
|
2022-01-24 13:45:07 +01:00
|
|
|
runOnMainThread(toastRunnable);
|
2021-07-19 16:19:47 +02:00
|
|
|
connection.disconnect();
|
2022-06-24 00:16:32 +02:00
|
|
|
} catch (Exception ex) {
|
2021-07-19 16:19:47 +02:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void sendViewCountRequest(SponsorSegment segment) {
|
|
|
|
try {
|
2022-01-17 14:54:11 +01:00
|
|
|
HttpURLConnection connection = getConnectionFromRoute(SBRoutes.VIEWED_SEGMENT, segment.UUID);
|
2021-07-19 16:19:47 +02:00
|
|
|
connection.disconnect();
|
2022-06-24 00:16:32 +02:00
|
|
|
} catch (Exception ex) {
|
2021-07-19 16:19:47 +02:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 13:45:07 +01:00
|
|
|
public static void voteForSegment(SponsorSegment segment, VoteOption voteOption, Context context, String... args) {
|
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
|
|
|
String segmentUuid = segment.UUID;
|
2022-07-14 18:42:43 +02:00
|
|
|
String uuid = SettingsEnum.SB_UUID.getString();
|
2022-01-24 13:45:07 +01:00
|
|
|
String vote = Integer.toString(voteOption == VoteOption.UPVOTE ? 1 : 0);
|
|
|
|
|
|
|
|
runOnMainThread(() -> Toast.makeText(context, str("vote_started"), Toast.LENGTH_SHORT).show());
|
|
|
|
|
|
|
|
HttpURLConnection connection = voteOption == VoteOption.CATEGORY_CHANGE
|
|
|
|
? getConnectionFromRoute(SBRoutes.VOTE_ON_SEGMENT_CATEGORY, segmentUuid, uuid, args[0])
|
|
|
|
: getConnectionFromRoute(SBRoutes.VOTE_ON_SEGMENT_QUALITY, segmentUuid, uuid, vote);
|
|
|
|
int responseCode = connection.getResponseCode();
|
|
|
|
|
|
|
|
switch (responseCode) {
|
|
|
|
case 200:
|
|
|
|
SponsorBlockUtils.messageToToast = str("vote_succeeded");
|
|
|
|
break;
|
|
|
|
case 403:
|
2022-01-26 14:43:52 +01:00
|
|
|
SponsorBlockUtils.messageToToast = str("vote_failed_forbidden", Requester.parseErrorJson(connection));
|
2022-01-24 13:45:07 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SponsorBlockUtils.messageToToast = str("vote_failed_unknown_error", responseCode, connection.getResponseMessage());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
runOnMainThread(() -> Toast.makeText(context, SponsorBlockUtils.messageToToast, Toast.LENGTH_LONG).show());
|
|
|
|
connection.disconnect();
|
2022-06-24 00:16:32 +02:00
|
|
|
} catch (Exception ex) {
|
2022-01-24 13:45:07 +01:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}).start();
|
2021-07-19 16:19:47 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 14:45:10 +02:00
|
|
|
public static void retrieveUserStats(PreferenceCategory category, Preference loadingPreference) {
|
2022-07-14 18:42:43 +02:00
|
|
|
if (!SettingsEnum.SB_ENABLED.getBoolean()) {
|
2021-07-23 14:45:10 +02:00
|
|
|
loadingPreference.setTitle(str("stats_sb_disabled"));
|
|
|
|
return;
|
2021-07-19 16:19:47 +02:00
|
|
|
}
|
2021-07-23 14:45:10 +02:00
|
|
|
|
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
2022-07-14 18:42:43 +02:00
|
|
|
JSONObject json = getJSONObject(SBRoutes.GET_USER_STATS, SettingsEnum.SB_UUID.getString());
|
2021-07-23 14:45:10 +02:00
|
|
|
UserStats stats = new UserStats(json.getString("userName"), json.getDouble("minutesSaved"), json.getInt("segmentCount"),
|
|
|
|
json.getInt("viewCount"));
|
2021-07-23 14:56:54 +02:00
|
|
|
SponsorBlockUtils.addUserStats(category, loadingPreference, stats);
|
2022-06-24 00:16:32 +02:00
|
|
|
} catch (Exception ex) {
|
2021-07-23 14:45:10 +02:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}).start();
|
2021-07-19 18:54:26 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:45:07 +01:00
|
|
|
public static void setUsername(String username, EditTextPreference preference, Runnable toastRunnable) {
|
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
2022-07-14 18:42:43 +02:00
|
|
|
HttpURLConnection connection = getConnectionFromRoute(SBRoutes.CHANGE_USERNAME, SettingsEnum.SB_UUID.getString(), username);
|
2022-01-24 13:45:07 +01:00
|
|
|
int responseCode = connection.getResponseCode();
|
|
|
|
|
|
|
|
if (responseCode == 200) {
|
|
|
|
SponsorBlockUtils.messageToToast = str("stats_username_changed");
|
|
|
|
runOnMainThread(() -> {
|
|
|
|
preference.setTitle(fromHtml(str("stats_username", username)));
|
|
|
|
preference.setText(username);
|
|
|
|
});
|
2022-06-24 00:16:32 +02:00
|
|
|
} else {
|
2022-01-24 13:45:07 +01:00
|
|
|
SponsorBlockUtils.messageToToast = str("stats_username_change_unknown_error", responseCode, connection.getResponseMessage());
|
|
|
|
}
|
|
|
|
runOnMainThread(toastRunnable);
|
|
|
|
connection.disconnect();
|
2022-06-24 00:16:32 +02:00
|
|
|
} catch (Exception ex) {
|
2022-01-24 13:45:07 +01:00
|
|
|
ex.printStackTrace();
|
2021-07-24 18:53:59 +02:00
|
|
|
}
|
2022-01-24 13:45:07 +01:00
|
|
|
}).start();
|
2021-07-19 16:19:47 +02:00
|
|
|
}
|
|
|
|
|
2022-02-07 16:15:22 +01:00
|
|
|
public static void runVipCheck() {
|
|
|
|
long now = System.currentTimeMillis();
|
2022-07-14 18:42:43 +02:00
|
|
|
if (now < (SettingsEnum.SB_LAST_VIP_CHECK.getLong() + TimeUnit.DAYS.toMillis(3))) {
|
2022-02-07 16:15:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2022-07-14 18:42:43 +02:00
|
|
|
JSONObject json = getJSONObject(SBRoutes.IS_USER_VIP, SettingsEnum.SB_UUID.getString());
|
2022-02-07 16:15:22 +01:00
|
|
|
boolean vip = json.getBoolean("vip");
|
2022-07-14 18:42:43 +02:00
|
|
|
SettingsEnum.SB_IS_VIP.saveValue(vip);
|
|
|
|
SettingsEnum.SB_LAST_VIP_CHECK.saveValue(now);
|
2022-06-24 00:16:32 +02:00
|
|
|
} catch (Exception ex) {
|
2022-02-07 16:15:22 +01:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 15:14:56 +01:00
|
|
|
// helpers
|
|
|
|
|
2021-07-19 16:19:47 +02:00
|
|
|
private static HttpURLConnection getConnectionFromRoute(Route route, String... params) throws IOException {
|
2022-07-14 18:42:43 +02:00
|
|
|
return Requester.getConnectionFromRoute(SettingsEnum.SB_API_URL.getString(), route, params);
|
2021-07-19 16:19:47 +02:00
|
|
|
}
|
|
|
|
|
2022-01-17 14:54:11 +01:00
|
|
|
private static JSONObject getJSONObject(Route route, String... params) throws Exception {
|
|
|
|
return Requester.getJSONObject(getConnectionFromRoute(route, params));
|
2021-07-19 16:19:47 +02:00
|
|
|
}
|
2022-10-04 20:43:29 +02:00
|
|
|
}
|