From 6ff9cd4f12ff116f9de4fdb214765816bb7ff7d3 Mon Sep 17 00:00:00 2001 From: FongMi Date: Thu, 23 May 2024 01:57:52 +0800 Subject: [PATCH] Support header for get live list --- .../java/com/fongmi/android/tv/api/LiveParser.java | 14 +++++++------- .../main/java/com/fongmi/android/tv/bean/Live.java | 11 +++++++++++ .../main/java/com/github/catvod/net/OkHttp.java | 9 +++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/fongmi/android/tv/api/LiveParser.java b/app/src/main/java/com/fongmi/android/tv/api/LiveParser.java index 84511f4a4..b9b6da6e3 100644 --- a/app/src/main/java/com/fongmi/android/tv/api/LiveParser.java +++ b/app/src/main/java/com/fongmi/android/tv/api/LiveParser.java @@ -33,9 +33,9 @@ public class LiveParser { public static void start(Live live) { if (live.getGroups().size() > 0) return; - if (live.getType() == 0) text(live, getText(live.getUrl())); - if (live.getType() == 1) json(live, getText(live.getUrl())); - if (live.getType() == 2) proxy(live, getText(live.getUrl())); + if (live.getType() == 0) text(live, getText(live.getUrl(), live.getHeaders())); + if (live.getType() == 1) json(live, getText(live.getUrl(), live.getHeaders())); + if (live.getType() == 2) proxy(live, getText(live.getUrl(), live.getHeaders())); } public static void text(Live live, String text) { @@ -110,11 +110,11 @@ public class LiveParser { } } - private static String getText(String url) { + private static String getText(String url, Map header) { if (url.startsWith("file")) return Path.read(url); - if (url.startsWith("http")) return OkHttp.string(url); - if (url.startsWith("assets") || url.startsWith("clan") || url.startsWith("proxy")) return getText(UrlUtil.convert(url)); - if (url.length() > 0 && url.length() % 4 == 0) return getText(new String(Base64.decode(url, Base64.DEFAULT))); + if (url.startsWith("http")) return OkHttp.string(url, header); + if (url.startsWith("assets") || url.startsWith("clan") || url.startsWith("proxy")) return getText(UrlUtil.convert(url), header); + if (url.length() > 0 && url.length() % 4 == 0) return getText(new String(Base64.decode(url, Base64.DEFAULT)), header); return ""; } diff --git a/app/src/main/java/com/fongmi/android/tv/bean/Live.java b/app/src/main/java/com/fongmi/android/tv/bean/Live.java index 7485cfd61..0ee51e711 100644 --- a/app/src/main/java/com/fongmi/android/tv/bean/Live.java +++ b/app/src/main/java/com/fongmi/android/tv/bean/Live.java @@ -12,6 +12,8 @@ import com.fongmi.android.tv.App; import com.fongmi.android.tv.Constant; import com.fongmi.android.tv.R; import com.fongmi.android.tv.db.AppDatabase; +import com.github.catvod.utils.Json; +import com.google.common.net.HttpHeaders; import com.google.gson.JsonElement; import com.google.gson.annotations.SerializedName; import com.google.gson.reflect.TypeToken; @@ -21,6 +23,7 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; @Entity public class Live { @@ -286,6 +289,14 @@ public class Live { return this; } + public Map getHeaders() { + Map headers = Json.toMap(getHeader()); + if (!getUa().isEmpty()) headers.put(HttpHeaders.USER_AGENT, getUa()); + if (!getOrigin().isEmpty()) headers.put(HttpHeaders.ORIGIN, getOrigin()); + if (!getReferer().isEmpty()) headers.put(HttpHeaders.REFERER, getReferer()); + return headers; + } + public static Live find(String name) { return AppDatabase.get().getLiveDao().find(name); } diff --git a/catvod/src/main/java/com/github/catvod/net/OkHttp.java b/catvod/src/main/java/com/github/catvod/net/OkHttp.java index d311c33ca..e20e6d9b4 100644 --- a/catvod/src/main/java/com/github/catvod/net/OkHttp.java +++ b/catvod/src/main/java/com/github/catvod/net/OkHttp.java @@ -96,6 +96,15 @@ public class OkHttp { } } + public static String string(String url, Map headers) { + try { + return newCall(url, Headers.of(headers)).execute().body().string(); + } catch (Exception e) { + e.printStackTrace(); + return ""; + } + } + public static Call newCall(String url) { return client().newCall(new Request.Builder().url(url).build()); }