Optimize fetch xml epg

pull/586/head
FongMi 2 years ago
parent 44e2523239
commit 303a2d0170
  1. 6
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/LiveActivity.java
  2. 2
      app/src/main/java/com/fongmi/android/tv/Constant.java
  3. 17
      app/src/main/java/com/fongmi/android/tv/api/EpgParser.java
  4. 17
      app/src/main/java/com/fongmi/android/tv/model/LiveViewModel.java
  5. 6
      app/src/mobile/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();
}

@ -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;
//解析預設時間

@ -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) {

@ -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<Channel> url;
public MutableLiveData<Boolean> xml;
public MutableLiveData<Live> live;
public MutableLiveData<Epg> 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();
}
}

@ -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();
}

Loading…
Cancel
Save