|
| 1 | +package app.revanced.integrations.twitter.patches.tweet; |
| 2 | + |
| 3 | +import org.json.JSONObject; |
| 4 | + |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.InputStreamReader; |
| 7 | +import java.net.HttpURLConnection; |
| 8 | +import java.net.URL; |
| 9 | +import app.revanced.integrations.twitter.Utils; |
| 10 | + |
| 11 | +public class TweetInfoAPI { |
| 12 | + private static JSONObject responseObj = null; |
| 13 | + private static JSONObject cache = new JSONObject(); |
| 14 | + // Lock is used so that getTweetSource waits till request is fetched. |
| 15 | + private static final Object lock = new Object(); |
| 16 | + |
| 17 | + private static JSONObject fetchStatusById(String id) { |
| 18 | + HttpURLConnection connection = null; |
| 19 | + BufferedReader reader = null; |
| 20 | + if(!app.revanced.integrations.shared.Utils.isNetworkConnected()){ |
| 21 | + return null; |
| 22 | + } |
| 23 | + try { |
| 24 | + String apiUrl = "https://api.fxtwitter.com/x/status/" + id; |
| 25 | + URL url = new URL(apiUrl); |
| 26 | + connection = (HttpURLConnection) url.openConnection(); |
| 27 | + |
| 28 | + connection.setRequestMethod("GET"); |
| 29 | + connection.setConnectTimeout(5000); // 5 seconds |
| 30 | + connection.setReadTimeout(5000); // 5 seconds |
| 31 | + |
| 32 | + int responseCode = connection.getResponseCode(); |
| 33 | + if (responseCode != HttpURLConnection.HTTP_OK) { |
| 34 | + throw new Exception("HTTP error code: " + responseCode); |
| 35 | + } |
| 36 | + |
| 37 | + reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
| 38 | + StringBuilder responseBuilder = new StringBuilder(); |
| 39 | + String line; |
| 40 | + |
| 41 | + while ((line = reader.readLine()) != null) { |
| 42 | + responseBuilder.append(line); |
| 43 | + } |
| 44 | + return new JSONObject(responseBuilder.toString()); |
| 45 | + |
| 46 | + } catch (Exception e) { |
| 47 | + Utils.logger(e); |
| 48 | + return null; |
| 49 | + |
| 50 | + } finally { |
| 51 | + try { |
| 52 | + if (reader != null) reader.close(); |
| 53 | + if (connection != null) connection.disconnect(); |
| 54 | + } catch (Exception ignored) { |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static void sendRequest(long id) { |
| 60 | + String tweetId = String.valueOf(id); |
| 61 | + |
| 62 | + synchronized (lock) { |
| 63 | + if (!cache.has(tweetId)) { |
| 64 | + new Thread(() -> { |
| 65 | + synchronized (lock) { |
| 66 | + responseObj = fetchStatusById(tweetId); |
| 67 | + lock.notifyAll(); |
| 68 | + } |
| 69 | + }).start(); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static String getTweetSource(long id) { |
| 75 | + String src = null; |
| 76 | + String tweetId = String.valueOf(id); |
| 77 | + try { |
| 78 | + if(app.revanced.integrations.shared.Utils.isNetworkConnected()){ |
| 79 | + synchronized (lock) { |
| 80 | + if (cache.has(tweetId)) { |
| 81 | + src = cache.optString(tweetId); |
| 82 | + } else { |
| 83 | + while (responseObj == null) { |
| 84 | + try { |
| 85 | + lock.wait(); |
| 86 | + }catch (InterruptedException e){ |
| 87 | + Thread.currentThread().interrupt(); |
| 88 | + Utils.logger("Interrupted while waiting: " + e.getMessage()); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (responseObj != null && responseObj.has("tweet")) { |
| 93 | + src = responseObj.optJSONObject("tweet").optString("source"); |
| 94 | + if(src!=null) cache.put(tweetId, src); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + }catch(Exception e){ |
| 100 | + Utils.logger(e); |
| 101 | + } |
| 102 | + return src; |
| 103 | + } |
| 104 | +} |
0 commit comments