VancedIntegrations/app/src/main/java/fi/vanced/libraries/youtube/whitelisting/requests/WhitelistRequester.java

103 lines
4.5 KiB
Java
Raw Normal View History

package fi.vanced.libraries.youtube.whitelisting.requests;
2022-01-17 13:54:11 +00:00
import static fi.razerman.youtube.XGlobals.debug;
import static fi.vanced.libraries.youtube.player.VideoInformation.currentVideoId;
2022-01-17 19:15:43 +00:00
import static fi.vanced.libraries.youtube.ui.AdButton.TAG;
2022-01-17 22:38:25 +00:00
import static pl.jakubweg.StringRef.str;
2022-01-17 13:54:11 +00:00
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import org.json.JSONObject;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import fi.vanced.libraries.youtube.player.ChannelModel;
import fi.vanced.libraries.youtube.whitelisting.Whitelist;
import fi.vanced.libraries.youtube.whitelisting.WhitelistType;
2022-01-17 13:54:11 +00:00
import fi.vanced.utils.requests.Requester;
import fi.vanced.utils.requests.Route;
public class WhitelistRequester {
2022-01-17 13:56:13 +00:00
private static final String YT_API_URL = "https://www.youtube.com/youtubei/v1/";
2022-01-17 13:54:11 +00:00
2022-01-17 22:38:25 +00:00
private WhitelistRequester() {}
public static void addChannelToWhitelist(WhitelistType whitelistType, View view, ImageView buttonIcon, Context context) {
2022-01-17 13:54:11 +00:00
try {
2022-01-24 11:47:32 +00:00
HttpURLConnection connection = getConnectionFromRoute(WhitelistRoutes.GET_CHANNEL_DETAILS, "replaceMeWithTheYouTubeAPIKey");
2022-01-17 13:54:11 +00:00
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
connection.setConnectTimeout(2 * 1000);
// TODO: Actually fetch the version
String jsonInputString = "{\"context\": {\"client\": { \"clientName\": \"Android\", \"clientVersion\": \"16.49.37\" } }, \"videoId\": \"" + currentVideoId + "\"}";
try(OutputStream os = connection.getOutputStream()) {
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
2022-01-23 21:36:01 +00:00
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
2022-01-17 13:54:11 +00:00
JSONObject json = getJSONObject(connection);
JSONObject videoInfo = json.getJSONObject("videoDetails");
ChannelModel channelModel = new ChannelModel(videoInfo.getString("author"), videoInfo.getString("channelId"));
2022-01-17 22:38:25 +00:00
String author = channelModel.getAuthor();
2022-01-17 13:54:11 +00:00
if (debug) {
2022-01-17 22:38:25 +00:00
Log.d(TAG, "channelId " + channelModel.getChannelId() + " fetched for author " + author);
2022-01-17 13:54:11 +00:00
}
boolean success = Whitelist.addToWhitelist(whitelistType, context, channelModel);
2022-01-17 22:38:25 +00:00
String whitelistTypeName = whitelistType.getFriendlyName();
2022-01-23 21:36:01 +00:00
runOnMainThread(() -> {
2022-01-17 13:54:11 +00:00
if (success) {
2022-01-23 21:36:01 +00:00
buttonIcon.setEnabled(whitelistType != WhitelistType.SPONSORBLOCK);
2022-01-17 22:38:25 +00:00
Toast.makeText(context, str("vanced_whitelisting_added", author, whitelistTypeName), Toast.LENGTH_SHORT).show();
2022-01-17 13:54:11 +00:00
}
else {
2022-01-23 21:36:01 +00:00
buttonIcon.setEnabled(whitelistType == WhitelistType.SPONSORBLOCK);
2022-01-17 22:38:25 +00:00
Toast.makeText(context, str("vanced_whitelisting_add_failed", author, whitelistTypeName), Toast.LENGTH_SHORT).show();
2022-01-17 13:54:11 +00:00
}
view.setEnabled(true);
});
}
else {
if (debug) {
2022-01-23 21:36:01 +00:00
Log.d(TAG, "player fetch response was " + responseCode);
2022-01-17 13:54:11 +00:00
}
2022-01-23 21:36:01 +00:00
runOnMainThread(() -> {
Toast.makeText(context, str("vanced_whitelisting_fetch_failed", responseCode), Toast.LENGTH_SHORT).show();
buttonIcon.setEnabled(true);
view.setEnabled(true);
});
2022-01-17 13:54:11 +00:00
}
2022-01-23 21:56:16 +00:00
connection.disconnect();
2022-01-17 13:54:11 +00:00
}
catch (Exception ex) {
Log.e(TAG, "Failed to fetch channelId", ex);
2022-01-23 21:36:01 +00:00
runOnMainThread(() -> view.setEnabled(true));
2022-01-17 13:54:11 +00:00
}
}
// helpers
2022-01-23 21:36:01 +00:00
private static void runOnMainThread(Runnable runnable) {
new Handler(Looper.getMainLooper()).post(runnable);
}
2022-01-17 13:54:11 +00:00
private static HttpURLConnection getConnectionFromRoute(Route route, String... params) throws IOException {
return Requester.getConnectionFromRoute(YT_API_URL, route, params);
}
private static JSONObject getJSONObject(HttpURLConnection connection) throws Exception {
return Requester.getJSONObject(connection);
}
}