- Update all channel URLs and event endpoint to streamtp10.com - Create NetworkUtils for centralized OkHttpClient configuration - Implement DNS fallback: Google (Primary) -> AdGuard (Secondary) -> System (Tertiary) - Migrate EventRepository to use NetworkUtils client instead of HttpURLConnection - Fix Referer header in StreamUrlResolver
172 lines
7.6 KiB
Diff
172 lines
7.6 KiB
Diff
diff --git a/app/src/main/java/com/streamplayer/EventRepository.java b/app/src/main/java/com/streamplayer/EventRepository.java
|
|
index f9340c7..7b3f662 100644
|
|
--- a/app/src/main/java/com/streamplayer/EventRepository.java
|
|
+++ b/app/src/main/java/com/streamplayer/EventRepository.java
|
|
@@ -29,8 +29,17 @@ public class EventRepository {
|
|
private static final String PREFS_NAME = "events_cache";
|
|
private static final String KEY_JSON = "json";
|
|
private static final String KEY_TIMESTAMP = "timestamp";
|
|
+ private static final String KEY_WORKING_URL = "working_url";
|
|
private static final long CACHE_DURATION = 24L * 60 * 60 * 1000; // 24 horas
|
|
- private static final String EVENTS_URL = "https://streamtpcloud.com/eventos.json";
|
|
+
|
|
+ // Lista de URLs a intentar en orden (con sistema de fallback)
|
|
+ private static final String[] EVENT_URLS = {
|
|
+ "https://streamtpcloud.com/eventos.json", // URL original
|
|
+ "https://streamtp10.com/eventos.json", // URL actual
|
|
+ "https://streamtpmedia.com/eventos.json" // URL anterior
|
|
+ };
|
|
+
|
|
+ private static final String DEFAULT_EVENTS_URL = "https://streamtpcloud.com/eventos.json";
|
|
|
|
public interface Callback {
|
|
void onSuccess(List<EventItem> events);
|
|
@@ -55,7 +64,7 @@ public class EventRepository {
|
|
|
|
new Thread(() -> {
|
|
try {
|
|
- String json = downloadJson();
|
|
+ String json = downloadJson(context);
|
|
List<EventItem> events = parseEvents(json);
|
|
prefs.edit().putString(KEY_JSON, json).putLong(KEY_TIMESTAMP, System.currentTimeMillis()).apply();
|
|
callback.onSuccess(events);
|
|
@@ -73,27 +82,103 @@ public class EventRepository {
|
|
}).start();
|
|
}
|
|
|
|
- private String downloadJson() throws IOException {
|
|
- URL url = new URL(EVENTS_URL);
|
|
+ private String downloadJson(Context context) throws IOException {
|
|
+ SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
|
+ String savedWorkingUrl = prefs.getString(KEY_WORKING_URL, null);
|
|
+
|
|
+ // Construir lista de URLs a intentar
|
|
+ // Primero la URL que funcionó la última vez, luego el resto
|
|
+ List<String> urlsToTry = new ArrayList<>();
|
|
+ if (savedWorkingUrl != null && !savedWorkingUrl.isEmpty()) {
|
|
+ urlsToTry.add(savedWorkingUrl);
|
|
+ }
|
|
+ for (String url : EVENT_URLS) {
|
|
+ if (!urlsToTry.contains(url)) {
|
|
+ urlsToTry.add(url);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ IOException lastException = null;
|
|
+
|
|
+ // Intentar cada URL en orden
|
|
+ for (String urlString : urlsToTry) {
|
|
+ try {
|
|
+ String json = downloadFromUrl(urlString);
|
|
+ // Guardar la URL que funcionó
|
|
+ prefs.edit().putString(KEY_WORKING_URL, urlString).apply();
|
|
+ return json;
|
|
+ } catch (IOException e) {
|
|
+ lastException = e;
|
|
+ // Continuar con la siguiente URL
|
|
+ }
|
|
+ }
|
|
+
|
|
+ // Si todas fallaron, lanzar la última excepción
|
|
+ throw new IOException("No se pudo conectar a ninguna de las URLs disponibles. Último error: " +
|
|
+ (lastException != null ? lastException.getMessage() : "Error desconocido"));
|
|
+ }
|
|
+
|
|
+ private String downloadFromUrl(String urlString) throws IOException {
|
|
+ URL url = new URL(urlString);
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
connection.setConnectTimeout(15000);
|
|
connection.setReadTimeout(15000);
|
|
connection.setRequestMethod("GET");
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
connection.setRequestProperty("User-Agent", "StreamPlayer/1.0");
|
|
-
|
|
+
|
|
+ // Habilitar seguimiento de redirecciones automáticamente
|
|
+ connection.setInstanceFollowRedirects(true);
|
|
+
|
|
+ String currentUrl = urlString;
|
|
+ int redirectCount = 0;
|
|
+ final int MAX_REDIRECTS = 5;
|
|
+
|
|
try {
|
|
int responseCode = connection.getResponseCode();
|
|
+
|
|
+ // Seguir redirecciones manualmente si es necesario
|
|
+ while (isRedirect(responseCode) && redirectCount < MAX_REDIRECTS) {
|
|
+ redirectCount++;
|
|
+ String newUrl = connection.getHeaderField("Location");
|
|
+
|
|
+ if (newUrl == null) {
|
|
+ throw new IOException("Redirección sin cabecera Location");
|
|
+ }
|
|
+
|
|
+ // Manejar URLs relativas
|
|
+ if (newUrl.startsWith("/")) {
|
|
+ newUrl = url.getProtocol() + "://" + url.getHost() + newUrl;
|
|
+ } else if (!newUrl.startsWith("http")) {
|
|
+ newUrl = url.getProtocol() + "://" + url.getHost() +
|
|
+ (url.getPort() > 0 ? ":" + url.getPort() : "") + "/" + newUrl;
|
|
+ }
|
|
+
|
|
+ currentUrl = newUrl;
|
|
+ url = new URL(currentUrl);
|
|
+ connection.disconnect();
|
|
+
|
|
+ connection = (HttpURLConnection) url.openConnection();
|
|
+ connection.setConnectTimeout(15000);
|
|
+ connection.setReadTimeout(15000);
|
|
+ connection.setRequestMethod("GET");
|
|
+ connection.setRequestProperty("Accept", "application/json");
|
|
+ connection.setRequestProperty("User-Agent", "StreamPlayer/1.0");
|
|
+ connection.setInstanceFollowRedirects(true);
|
|
+
|
|
+ responseCode = connection.getResponseCode();
|
|
+ }
|
|
+
|
|
if (responseCode != HttpURLConnection.HTTP_OK) {
|
|
throw new IOException("Error HTTP " + responseCode + ": " + connection.getResponseMessage());
|
|
}
|
|
-
|
|
+
|
|
String contentType = connection.getContentType();
|
|
// Permitir json o text/plain (Raw de Gitea a veces es text/plain)
|
|
if (contentType != null && !contentType.contains("json") && !contentType.contains("text/plain")) {
|
|
throw new IOException("El servidor devolvió " + contentType + " en lugar de JSON. Verifica que la URL sea correcta.");
|
|
}
|
|
-
|
|
+
|
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
|
StringBuilder builder = new StringBuilder();
|
|
String line;
|
|
@@ -101,12 +186,12 @@ public class EventRepository {
|
|
builder.append(line);
|
|
}
|
|
String response = builder.toString();
|
|
-
|
|
+
|
|
// Validar que no sea HTML
|
|
if (response.trim().startsWith("<!") || response.trim().startsWith("<html")) {
|
|
throw new IOException("El servidor devolvió HTML en lugar de JSON. La URL del endpoint puede estar incorrecta o el servidor tiene problemas.");
|
|
}
|
|
-
|
|
+
|
|
return response;
|
|
}
|
|
} finally {
|
|
@@ -114,6 +199,14 @@ public class EventRepository {
|
|
}
|
|
}
|
|
|
|
+ private boolean isRedirect(int statusCode) {
|
|
+ return statusCode == HttpURLConnection.HTTP_MOVED_PERM ||
|
|
+ statusCode == HttpURLConnection.HTTP_MOVED_TEMP ||
|
|
+ statusCode == HttpURLConnection.HTTP_SEE_OTHER ||
|
|
+ statusCode == 307 || // Temporary Redirect
|
|
+ statusCode == 308; // Permanent Redirect
|
|
+ }
|
|
+
|
|
private List<EventItem> parseEvents(String json) throws JSONException {
|
|
if (json == null || json.trim().isEmpty()) {
|
|
throw new JSONException("La respuesta está vacía");
|