revanced-integrations/app/src/main/java/app/revanced/integrations/sponsorblock/requests/SBRequester.java

232 lines
11 KiB
Java
Raw Normal View History

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;
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-11-05 03:19:09 +01:00
import app.revanced.integrations.requests.Requester;
import app.revanced.integrations.requests.Route;
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;
import app.revanced.integrations.utils.ReVancedUtils;
import app.revanced.integrations.utils.LogHelper;
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) {
// FIXME? should this use Requester#getJSONArray and not disconnect?
// HTTPURLConnection#disconnect says:
// disconnect if other requests to the server
// are unlikely in the near future.
JSONArray responseArray = Requester.parseJSONArrayAndDisconnect(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
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) {
LogHelper.printException(() -> "failed to get segments", ex);
2021-07-19 16:19:47 +02:00
}
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:
SponsorBlockUtils.messageToToast = str("submit_failed_forbidden", Requester.parseErrorJsonAndDisconnect(connection));
2021-07-19 16:19:47 +02:00
break;
case 429:
SponsorBlockUtils.messageToToast = str("submit_failed_rate_limit");
break;
case 400:
SponsorBlockUtils.messageToToast = str("submit_failed_invalid", Requester.parseErrorJsonAndDisconnect(connection));
break;
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) {
LogHelper.printException(() -> "failed to submit segments", ex);
2021-07-19 16:19:47 +02:00
}
}
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) {
LogHelper.printException(() -> "failed to send view count request", ex);
2021-07-19 16:19:47 +02:00
}
}
2022-01-24 13:45:07 +01:00
public static void voteForSegment(SponsorSegment segment, VoteOption voteOption, Context context, String... args) {
ReVancedUtils.runOnBackgroundThread(() -> {
2022-01-24 13:45:07 +01:00
try {
String segmentUuid = segment.UUID;
String uuid = SettingsEnum.SB_UUID.getString();
2022-01-24 13:45:07 +01:00
String vote = Integer.toString(voteOption == VoteOption.UPVOTE ? 1 : 0);
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:
SponsorBlockUtils.messageToToast = str("vote_failed_forbidden", Requester.parseErrorJsonAndDisconnect(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) {
LogHelper.printException(() -> "failed to vote for segment", ex);
2022-01-24 13:45:07 +01:00
}
});
2021-07-19 16:19:47 +02:00
}
2021-07-23 14:45:10 +02:00
public static void retrieveUserStats(PreferenceCategory category, Preference loadingPreference) {
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
ReVancedUtils.runOnBackgroundThread(() -> {
2021-07-23 14:45:10 +02:00
try {
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"));
runOnMainThread(() -> { // get back on main thread to modify UI elements
SponsorBlockUtils.addUserStats(category, loadingPreference, stats);
});
2022-06-24 00:16:32 +02:00
} catch (Exception ex) {
LogHelper.printException(() -> "failed to retrieve user stats", ex);
2021-07-23 14:45:10 +02:00
}
});
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) {
ReVancedUtils.runOnBackgroundThread(() -> {
2022-01-24 13:45:07 +01:00
try {
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) {
LogHelper.printException(() -> "failed to set username", ex);
}
});
2021-07-19 16:19:47 +02:00
}
2022-02-07 16:15:22 +01:00
public static void runVipCheck() {
long now = System.currentTimeMillis();
if (now < (SettingsEnum.SB_LAST_VIP_CHECK.getLong() + TimeUnit.DAYS.toMillis(3))) {
2022-02-07 16:15:22 +01:00
return;
}
try {
JSONObject json = getJSONObject(SBRoutes.IS_USER_VIP, SettingsEnum.SB_UUID.getString());
2022-02-07 16:15:22 +01:00
boolean vip = json.getBoolean("vip");
SettingsEnum.SB_IS_VIP.saveValue(vip);
SettingsEnum.SB_LAST_VIP_CHECK.saveValue(now);
2022-06-24 00:16:32 +02:00
} catch (Exception ex) {
LogHelper.printException(() -> "failed to check VIP", ex);
2022-02-07 16:15:22 +01:00
}
}
// helpers
2021-07-19 16:19:47 +02:00
private static HttpURLConnection getConnectionFromRoute(Route route, String... params) throws IOException {
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.parseJSONObjectAndDisconnect(getConnectionFromRoute(route, params));
2021-07-19 16:19:47 +02:00
}
}