From 303a2d01709c228e707f597643efb725caeaf370 Mon Sep 17 00:00:00 2001 From: FongMi Date: Wed, 12 Jun 2024 18:45:55 +0800 Subject: [PATCH] Optimize fetch xml epg --- .../android/tv/ui/activity/LiveActivity.java | 6 ++++++ .../java/com/fongmi/android/tv/Constant.java | 2 ++ .../com/fongmi/android/tv/api/EpgParser.java | 17 +++++++---------- .../fongmi/android/tv/model/LiveViewModel.java | 17 ++++++++++++++++- .../android/tv/ui/activity/LiveActivity.java | 6 ++++++ 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/app/src/leanback/java/com/fongmi/android/tv/ui/activity/LiveActivity.java b/app/src/leanback/java/com/fongmi/android/tv/ui/activity/LiveActivity.java index ddbcd7c13..c49971471 100644 --- a/app/src/leanback/java/com/fongmi/android/tv/ui/activity/LiveActivity.java +++ b/app/src/leanback/java/com/fongmi/android/tv/ui/activity/LiveActivity.java @@ -213,8 +213,10 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick private void setViewModel() { mViewModel = new ViewModelProvider(this).get(LiveViewModel.class); mViewModel.url.observe(this, result -> mPlayers.start(result, getTimeout())); + mViewModel.xml.observe(this, this::setEpg); mViewModel.epg.observe(this, this::setEpg); mViewModel.live.observe(this, live -> { + mViewModel.getXml(live); hideProgress(); setGroup(live); setWidth(live); @@ -605,6 +607,10 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick setMetadata(); } + private void setEpg(boolean success) { + if (mChannel != null && success) mViewModel.getEpg(mChannel); + } + private void setEpg(Epg epg) { if (mChannel != null && mChannel.getTvgName().equals(epg.getKey())) setEpg(); } diff --git a/app/src/main/java/com/fongmi/android/tv/Constant.java b/app/src/main/java/com/fongmi/android/tv/Constant.java index 76e913d8b..5bf1ce193 100644 --- a/app/src/main/java/com/fongmi/android/tv/Constant.java +++ b/app/src/main/java/com/fongmi/android/tv/Constant.java @@ -13,6 +13,8 @@ public class Constant { public static final int TIMEOUT_LIVE = 30 * 1000; //節目爬蟲時間 public static final int TIMEOUT_EPG = 5 * 1000; + //節目爬蟲時間 + public static final int TIMEOUT_XML = 15 * 1000; //播放超時時間 public static final int TIMEOUT_PLAY = 15 * 1000; //解析預設時間 diff --git a/app/src/main/java/com/fongmi/android/tv/api/EpgParser.java b/app/src/main/java/com/fongmi/android/tv/api/EpgParser.java index 5bef7c6dd..f79fdca4b 100644 --- a/app/src/main/java/com/fongmi/android/tv/api/EpgParser.java +++ b/app/src/main/java/com/fongmi/android/tv/api/EpgParser.java @@ -27,16 +27,13 @@ import java.util.Set; public class EpgParser { - public static void start(Live live) { - try { - if (!live.getEpg().endsWith(".xml") && !live.getEpg().endsWith(".gz")) return; - File file = Path.epg(Uri.parse(live.getEpg()).getLastPathSegment()); - if (shouldDownload(file)) Download.create(live.getEpg(), file).start(); - if (file.getName().endsWith(".gz")) readGzip(live, file); - else readXml(live, file); - } catch (Exception e) { - e.printStackTrace(); - } + public static boolean start(Live live) throws Exception { + if (!live.getEpg().endsWith(".xml") && !live.getEpg().endsWith(".gz")) return false; + File file = Path.epg(Uri.parse(live.getEpg()).getLastPathSegment()); + if (shouldDownload(file)) Download.create(live.getEpg(), file).start(); + if (file.getName().endsWith(".gz")) readGzip(live, file); + else readXml(live, file); + return true; } private static boolean shouldDownload(File file) { diff --git a/app/src/main/java/com/fongmi/android/tv/model/LiveViewModel.java b/app/src/main/java/com/fongmi/android/tv/model/LiveViewModel.java index 8a0910d81..7eac5e29c 100644 --- a/app/src/main/java/com/fongmi/android/tv/model/LiveViewModel.java +++ b/app/src/main/java/com/fongmi/android/tv/model/LiveViewModel.java @@ -30,17 +30,20 @@ public class LiveViewModel extends ViewModel { private static final int LIVE = 0; private static final int EPG = 1; private static final int URL = 2; + private static final int XML = 3; private final SimpleDateFormat formatDate; private final SimpleDateFormat formatTime; public MutableLiveData url; + public MutableLiveData xml; public MutableLiveData live; public MutableLiveData epg; private ExecutorService executor1; private ExecutorService executor2; private ExecutorService executor3; + private ExecutorService executor4; public LiveViewModel() { this.formatTime = new SimpleDateFormat("yyyy-MM-ddHH:mm", Locale.getDefault()); @@ -48,18 +51,22 @@ public class LiveViewModel extends ViewModel { this.live = new MutableLiveData<>(); this.epg = new MutableLiveData<>(); this.url = new MutableLiveData<>(); + this.xml = new MutableLiveData<>(); } public void getLive(Live item) { execute(LIVE, () -> { VodConfig.get().setRecent(item.getJar()); LiveParser.start(item); - EpgParser.start(item); verify(item); return item; }); } + public void getXml(Live item) { + execute(XML, () -> EpgParser.start(item)); + } + public void getEpg(Channel item) { String date = formatDate.format(new Date()); String url = item.getEpg().replace("{date}", date); @@ -109,6 +116,11 @@ public class LiveViewModel extends ViewModel { executor3 = Executors.newFixedThreadPool(2); executor3.execute(runnable(type, callable, executor3)); break; + case XML: + if (executor4 != null) executor4.shutdownNow(); + executor4 = Executors.newFixedThreadPool(2); + executor4.execute(runnable(type, callable, executor4)); + break; } } @@ -118,6 +130,7 @@ public class LiveViewModel extends ViewModel { if (Thread.interrupted()) return; if (type == EPG) epg.postValue((Epg) executor.submit(callable).get(Constant.TIMEOUT_EPG, TimeUnit.MILLISECONDS)); if (type == LIVE) live.postValue((Live) executor.submit(callable).get(Constant.TIMEOUT_LIVE, TimeUnit.MILLISECONDS)); + if (type == XML) xml.postValue((Boolean) executor.submit(callable).get(Constant.TIMEOUT_XML, TimeUnit.MILLISECONDS)); if (type == URL) url.postValue((Channel) executor.submit(callable).get(Constant.TIMEOUT_PARSE_LIVE, TimeUnit.MILLISECONDS)); } catch (Throwable e) { if (e instanceof InterruptedException || Thread.interrupted()) return; @@ -125,6 +138,7 @@ public class LiveViewModel extends ViewModel { else if (type == URL) url.postValue(new Channel()); if (type == LIVE) live.postValue(new Live()); if (type == EPG) epg.postValue(new Epg()); + if (type == XML) xml.postValue(false); e.printStackTrace(); } }; @@ -135,5 +149,6 @@ public class LiveViewModel extends ViewModel { if (executor1 != null) executor1.shutdownNow(); if (executor2 != null) executor2.shutdownNow(); if (executor3 != null) executor3.shutdownNow(); + if (executor4 != null) executor4.shutdownNow(); } } diff --git a/app/src/mobile/java/com/fongmi/android/tv/ui/activity/LiveActivity.java b/app/src/mobile/java/com/fongmi/android/tv/ui/activity/LiveActivity.java index f0bd071fa..452d90f2c 100644 --- a/app/src/mobile/java/com/fongmi/android/tv/ui/activity/LiveActivity.java +++ b/app/src/mobile/java/com/fongmi/android/tv/ui/activity/LiveActivity.java @@ -236,8 +236,10 @@ public class LiveActivity extends BaseActivity implements CustomKeyDownLive.List private void setViewModel() { mViewModel = new ViewModelProvider(this).get(LiveViewModel.class); mViewModel.url.observeForever(mObserveUrl); + mViewModel.xml.observe(this, this::setEpg); mViewModel.epg.observeForever(mObserveEpg); mViewModel.live.observe(this, live -> { + mViewModel.getXml(live); hideProgress(); setGroup(live); setWidth(live); @@ -646,6 +648,10 @@ public class LiveActivity extends BaseActivity implements CustomKeyDownLive.List setMetadata(); } + private void setEpg(boolean success) { + if (mChannel != null && success) mViewModel.getEpg(mChannel); + } + private void setEpg(Epg epg) { if (mChannel != null && mChannel.getTvgName().equals(epg.getKey())) setEpg(); }