mirror of
https://github.com/revanced/revanced-patches.git
synced 2025-05-03 16:14:28 +02:00
chore(YouTube - Announcements): Use API v4 to get announcements (#3869)
This commit is contained in:
parent
ae160a3798
commit
2ace07d7f8
@ -2,7 +2,8 @@ package app.revanced.extension.youtube.patches.announcements;
|
|||||||
|
|
||||||
import static android.text.Html.FROM_HTML_MODE_COMPACT;
|
import static android.text.Html.FROM_HTML_MODE_COMPACT;
|
||||||
import static app.revanced.extension.shared.StringRef.str;
|
import static app.revanced.extension.shared.StringRef.str;
|
||||||
import static app.revanced.extension.youtube.patches.announcements.requests.AnnouncementsRoutes.GET_LATEST_ANNOUNCEMENT;
|
import static app.revanced.extension.youtube.patches.announcements.requests.AnnouncementsRoutes.GET_LATEST_ANNOUNCEMENTS;
|
||||||
|
import static app.revanced.extension.youtube.patches.announcements.requests.AnnouncementsRoutes.GET_LATEST_ANNOUNCEMENT_IDS;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
@ -13,11 +14,14 @@ import android.widget.TextView;
|
|||||||
|
|
||||||
import androidx.annotation.RequiresApi;
|
import androidx.annotation.RequiresApi;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import app.revanced.extension.shared.Logger;
|
import app.revanced.extension.shared.Logger;
|
||||||
import app.revanced.extension.shared.Utils;
|
import app.revanced.extension.shared.Utils;
|
||||||
@ -30,6 +34,45 @@ public final class AnnouncementsPatch {
|
|||||||
private AnnouncementsPatch() {
|
private AnnouncementsPatch() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||||
|
private static boolean isLatestAlready() throws IOException {
|
||||||
|
HttpURLConnection connection =
|
||||||
|
AnnouncementsRoutes.getAnnouncementsConnectionFromRoute(GET_LATEST_ANNOUNCEMENT_IDS);
|
||||||
|
|
||||||
|
Logger.printDebug(() -> "Get latest announcement IDs route connection url: " + connection.getURL());
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Do not show the announcement if the request failed.
|
||||||
|
if (connection.getResponseCode() != 200) {
|
||||||
|
if (Settings.ANNOUNCEMENT_LAST_ID.isSetToDefault())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
Settings.ANNOUNCEMENT_LAST_ID.resetToDefault();
|
||||||
|
Utils.showToastLong(str("revanced_announcements_connection_failed"));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} catch (IOException ex) {
|
||||||
|
Logger.printException(() -> "Could not connect to announcements provider", ex);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var jsonString = Requester.parseStringAndDisconnect(connection);
|
||||||
|
|
||||||
|
// Parse the ID. Fall-back to raw string if it fails.
|
||||||
|
int id = Settings.ANNOUNCEMENT_LAST_ID.defaultValue;
|
||||||
|
try {
|
||||||
|
final var announcementIds = new JSONArray(jsonString);
|
||||||
|
id = announcementIds.getJSONObject(0).getInt("id");
|
||||||
|
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
Logger.printException(() -> "Failed to parse announcement IDs", ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do not show the announcement, if the last announcement id is the same as the current one.
|
||||||
|
return Settings.ANNOUNCEMENT_LAST_ID.get() == id;
|
||||||
|
}
|
||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||||
public static void showAnnouncement(final Activity context) {
|
public static void showAnnouncement(final Activity context) {
|
||||||
if (!Settings.ANNOUNCEMENTS.get()) return;
|
if (!Settings.ANNOUNCEMENTS.get()) return;
|
||||||
@ -39,26 +82,12 @@ public final class AnnouncementsPatch {
|
|||||||
|
|
||||||
Utils.runOnBackgroundThread(() -> {
|
Utils.runOnBackgroundThread(() -> {
|
||||||
try {
|
try {
|
||||||
|
if (isLatestAlready()) return;
|
||||||
|
|
||||||
HttpURLConnection connection = AnnouncementsRoutes.getAnnouncementsConnectionFromRoute(
|
HttpURLConnection connection = AnnouncementsRoutes.getAnnouncementsConnectionFromRoute(
|
||||||
GET_LATEST_ANNOUNCEMENT, Locale.getDefault().toLanguageTag());
|
GET_LATEST_ANNOUNCEMENTS, Locale.getDefault().toLanguageTag());
|
||||||
|
|
||||||
Logger.printDebug(() -> "Get latest announcement route connection url: " + connection.getURL());
|
Logger.printDebug(() -> "Get latest announcements route connection url: " + connection.getURL());
|
||||||
|
|
||||||
try {
|
|
||||||
// Do not show the announcement if the request failed.
|
|
||||||
if (connection.getResponseCode() != 200) {
|
|
||||||
if (Settings.ANNOUNCEMENT_LAST_ID.isSetToDefault())
|
|
||||||
return;
|
|
||||||
|
|
||||||
Settings.ANNOUNCEMENT_LAST_ID.resetToDefault();
|
|
||||||
Utils.showToastLong(str("revanced_announcements_connection_failed"));
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch (IOException ex) {
|
|
||||||
Logger.printException(() -> "Could not connect to announcements provider", ex);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var jsonString = Requester.parseStringAndDisconnect(connection);
|
var jsonString = Requester.parseStringAndDisconnect(connection);
|
||||||
|
|
||||||
@ -66,6 +95,7 @@ public final class AnnouncementsPatch {
|
|||||||
int id = Settings.ANNOUNCEMENT_LAST_ID.defaultValue;
|
int id = Settings.ANNOUNCEMENT_LAST_ID.defaultValue;
|
||||||
String title;
|
String title;
|
||||||
String message;
|
String message;
|
||||||
|
LocalDateTime archivedAt = LocalDateTime.MAX;
|
||||||
Level level = Level.INFO;
|
Level level = Level.INFO;
|
||||||
try {
|
try {
|
||||||
final var announcement = new JSONObject(jsonString);
|
final var announcement = new JSONObject(jsonString);
|
||||||
@ -73,8 +103,12 @@ public final class AnnouncementsPatch {
|
|||||||
id = announcement.getInt("id");
|
id = announcement.getInt("id");
|
||||||
title = announcement.getString("title");
|
title = announcement.getString("title");
|
||||||
message = announcement.getJSONObject("content").getString("message");
|
message = announcement.getJSONObject("content").getString("message");
|
||||||
if (!announcement.isNull("level")) level = Level.fromInt(announcement.getInt("level"));
|
if (!announcement.isNull("archived_at")) {
|
||||||
|
archivedAt = LocalDateTime.parse(announcement.getString("archived_at"));
|
||||||
|
}
|
||||||
|
if (!announcement.isNull("level")) {
|
||||||
|
level = Level.fromInt(announcement.getInt("level"));
|
||||||
|
}
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
Logger.printException(() -> "Failed to parse announcement. Fall-backing to raw string", ex);
|
Logger.printException(() -> "Failed to parse announcement. Fall-backing to raw string", ex);
|
||||||
|
|
||||||
@ -82,8 +116,11 @@ public final class AnnouncementsPatch {
|
|||||||
message = jsonString;
|
message = jsonString;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do not show the announcement, if the last announcement id is the same as the current one.
|
// If the announcement is archived, do not show it.
|
||||||
if (Settings.ANNOUNCEMENT_LAST_ID.get() == id) return;
|
if (archivedAt.isBefore(LocalDateTime.now())) {
|
||||||
|
Settings.ANNOUNCEMENT_LAST_ID.save(id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
int finalId = id;
|
int finalId = id;
|
||||||
final var finalTitle = title;
|
final var finalTitle = title;
|
||||||
|
@ -9,12 +9,9 @@ import java.net.HttpURLConnection;
|
|||||||
import static app.revanced.extension.youtube.requests.Route.Method.GET;
|
import static app.revanced.extension.youtube.requests.Route.Method.GET;
|
||||||
|
|
||||||
public class AnnouncementsRoutes {
|
public class AnnouncementsRoutes {
|
||||||
private static final String ANNOUNCEMENTS_PROVIDER = "https://api.revanced.app/v2";
|
public static final Route GET_LATEST_ANNOUNCEMENTS = new Route(GET, "/announcements/latest?tag=youtube");
|
||||||
|
public static final Route GET_LATEST_ANNOUNCEMENT_IDS = new Route(GET, "/announcements/latest/id?tag=youtube");
|
||||||
/**
|
private static final String ANNOUNCEMENTS_PROVIDER = "https://api.revanced.app/v4";
|
||||||
* 'language' parameter is IETF format (for USA it would be 'en-us').
|
|
||||||
*/
|
|
||||||
public static final Route GET_LATEST_ANNOUNCEMENT = new Route(GET, "/announcements/youtube/latest?language={language}");
|
|
||||||
|
|
||||||
private AnnouncementsRoutes() {
|
private AnnouncementsRoutes() {
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user