Support xml.gz

pull/499/head
okjack 2 years ago
parent 88d2282981
commit 7d581fa4f5
  1. 47
      app/src/main/java/com/fongmi/android/tv/api/EpgParser.java
  2. 7
      app/src/main/java/com/fongmi/android/tv/bean/Tv.java
  3. 2
      app/src/main/java/com/fongmi/android/tv/db/AppDatabase.java
  4. 2
      app/src/main/java/com/fongmi/android/tv/server/process/Local.java
  5. 14
      app/src/main/java/com/fongmi/android/tv/utils/FileUtil.java
  6. 8
      catvod/src/main/java/com/github/catvod/utils/Path.java

@ -9,6 +9,7 @@ import com.fongmi.android.tv.bean.Group;
import com.fongmi.android.tv.bean.Live;
import com.fongmi.android.tv.bean.Tv;
import com.fongmi.android.tv.utils.Download;
import com.fongmi.android.tv.utils.FileUtil;
import com.github.catvod.utils.Path;
import com.github.catvod.utils.Trans;
@ -20,64 +21,68 @@ import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
public class EpgParser {
private static final SimpleDateFormat formatFull = new SimpleDateFormat("yyyyMMddHHmmss");
private static final SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
private static final SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm");
public static void start(Live live) {
try {
if (!live.getEpg().contains(".xml") || live.getEpg().contains("{")) return;
File file = Path.cache(Uri.parse(live.getEpg()).getLastPathSegment());
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();
readXml(live, Path.read(file));
if (file.getName().endsWith(".gz")) readGzip(live, file);
else readXml(live, file);
} catch (Exception e) {
e.printStackTrace();
}
}
private static boolean shouldDownload(File file) {
return !file.exists() || !equalToday(file);
return !file.exists() || !isToday(file.lastModified());
}
private static boolean isToday(Date date) {
return isToday(date.getTime());
}
private static boolean equalToday(File file) {
private static boolean isToday(long millis) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(file.lastModified());
calendar.setTimeInMillis(millis);
return calendar.get(Calendar.DAY_OF_MONTH) == Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
}
private static Date parseDateTime(String text) throws Exception {
return formatFull.parse(text.substring(0, 14));
private static void readGzip(Live live, File file) throws Exception {
File xml = Path.epg(file.getName().replace(".gz", ""));
if (!xml.exists()) FileUtil.extractGzip(file, xml);
readXml(live, xml);
}
private static void readXml(Live live, String xml) throws Exception {
private static void readXml(Live live, File file) throws Exception {
Set<String> exist = new HashSet<>();
Map<String, Epg> epgMap = new HashMap<>();
Map<String, String> mapping = new HashMap<>();
SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm", Locale.getDefault());
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
SimpleDateFormat formatFull = new SimpleDateFormat("yyyyMMddHHmmss Z", Locale.getDefault());
String today = formatDate.format(new Date());
Tv tv = new Persister().read(Tv.class, xml);
Tv tv = new Persister().read(Tv.class, Path.read(file));
for (Group group : live.getGroups()) for (Channel channel : group.getChannel()) exist.add(channel.getTvgName());
for (Tv.Channel channel : tv.getChannel()) mapping.put(channel.getId(), channel.getDisplayName());
for (Tv.Programme programme : tv.getProgramme()) {
String key = mapping.get(programme.getChannel());
Date startDate = formatFull.parse(programme.getStart());
Date endDate = formatFull.parse(programme.getStop());
if (!exist.contains(key)) continue;
if (!programme.equals(today)) continue;
if (!isToday(startDate) && !isToday(endDate)) continue;
if (!epgMap.containsKey(key)) epgMap.put(key, Epg.create(key, today));
String title = programme.getTitle();
String start = programme.getStart();
String stop = programme.getStop();
Date startDate = parseDateTime(start);
Date endDate = parseDateTime(stop);
EpgData epgData = new EpgData();
epgData.setTitle(Trans.s2t(programme.getTitle()));
epgData.setStart(formatTime.format(startDate));
epgData.setEnd(formatTime.format(endDate));
epgData.setStartTime(startDate.getTime());
epgData.setEndTime(endDate.getTime());
epgData.setTitle(Trans.s2t(title));
epgMap.get(key).getList().add(epgData);
}
for (Group group : live.getGroups()) {

@ -63,6 +63,9 @@ public class Tv {
@Element(name = "date", required = false)
private String date;
@Element(name = "desc", required = false)
private String desc;
public String getStart() {
return TextUtils.isEmpty(start) ? "" : start;
}
@ -83,8 +86,8 @@ public class Tv {
return TextUtils.isEmpty(date) ? "" : date;
}
public boolean equals(String date) {
return getDate().isEmpty() || getDate().equals(date);
public String getDesc() {
return TextUtils.isEmpty(desc) ? "" : desc;
}
}
}

@ -75,7 +75,7 @@ public abstract class AppDatabase extends RoomDatabase {
App.execute(() -> {
File restore = Path.restore();
if (!restore.exists()) return;
FileUtil.unzip(file, restore);
FileUtil.extractZip(file, restore);
File db = new File(restore, NAME);
File wal = new File(restore, NAME + "-wal");
File shm = new File(restore, NAME + "-shm");

@ -56,7 +56,7 @@ public class Local implements Process {
for (String k : files.keySet()) {
String fn = params.get(k);
File temp = new File(files.get(k));
if (fn.toLowerCase().endsWith(".zip")) FileUtil.unzip(temp, Path.root(path));
if (fn.toLowerCase().endsWith(".zip")) FileUtil.extractZip(temp, Path.root(path));
else Path.copy(temp, Path.root(path, fn));
}
return Nano.success();

@ -11,12 +11,15 @@ import com.fongmi.android.tv.App;
import com.fongmi.android.tv.impl.Callback;
import com.github.catvod.utils.Path;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.Enumeration;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
@ -63,8 +66,17 @@ public class FileUtil {
in.close();
}
}
public static void extractGzip(File target, File path) {
byte[] buffer = new byte[1024];
try (GZIPInputStream is = new GZIPInputStream(new BufferedInputStream(new FileInputStream(target))); BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(path))) {
int read;
while ((read = is.read(buffer)) != -1) os.write(buffer, 0, read);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void unzip(File target, File path) {
public static void extractZip(File target, File path) {
try (ZipFile zip = new ZipFile(target)) {
Enumeration<?> entries = zip.entries();
while (entries.hasMoreElements()) {

@ -89,6 +89,10 @@ public class Path {
return mkdir(new File(cache() + File.separator + "exo"));
}
public static File epg() {
return mkdir(new File(cache() + File.separator + "epg"));
}
public static File jpa() {
return mkdir(new File(cache() + File.separator + "jpa"));
}
@ -117,6 +121,10 @@ public class Path {
return new File(files(), name);
}
public static File epg(String name) {
return new File(epg(), name);
}
public static File js(String name) {
return new File(js(), name);
}

Loading…
Cancel
Save