New live format

This commit is contained in:
FongMi 2022-12-28 12:55:36 +08:00
parent c2bc8bf95f
commit cf62736e6e
7 changed files with 246 additions and 0 deletions

View File

@ -15,3 +15,4 @@ dependencyResolutionManagement {
} }
rootProject.name = "CatVodSpider" rootProject.name = "CatVodSpider"
include ':app' include ':app'
include ':tools'

1
tools/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build

13
tools/build.gradle Normal file
View File

@ -0,0 +1,13 @@
plugins {
id 'java-library'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
dependencies {
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
implementation 'com.google.code.gson:gson:2.8.6'
}

View File

@ -0,0 +1,59 @@
package com.fongmi.tools;
import com.fongmi.tools.bean.Channel;
import com.fongmi.tools.bean.Group;
import com.google.gson.Gson;
import java.io.File;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class Run {
private final List<Group> items = new ArrayList<>();
private final Gson gson = new Gson().newBuilder().disableHtmlEscaping().setPrettyPrinting().create();
public static void main(String[] args) {
new Run().start();
}
private void start() {
parse(Util.getFile(getClass(), "live.txt"));
System.out.println(gson.toJson(items));
writeFile();
}
private void parse(String text) {
for (String line : text.split("\n")) {
String[] split = line.split(",");
if (split.length < 2) continue;
if (line.contains("#genre#")) items.add(Group.create(split[0]));
if (!line.contains("://")) continue;
Group group = items.get(items.size() - 1);
String number = split[0];
String epg = split[1];
String logo = split[2];
String name = split[3];
String url = split[4];
group.find(Channel.create().number(number).epg(epg).logo(logo).name(name).ua(getUa(url))).addUrls(url.split("#"));
}
}
private String getUa(String url) {
if (url.contains("play-live.ifeng")) return "okhttp/3.15";
return null;
}
private void writeFile() {
try {
File file = new File("json", "live.json");
PrintWriter writer = new PrintWriter(file, String.valueOf(StandardCharsets.UTF_8));
writer.println(gson.toJson(items));
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,22 @@
package com.fongmi.tools;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class Util {
public static String getFile(Class<?> clz, String fileName) {
try {
StringBuilder sb = new StringBuilder();
URI uri = clz.getClassLoader().getResource(fileName).toURI();
Stream<String> stream = Files.lines(Paths.get(uri), StandardCharsets.UTF_8);
stream.forEach(s -> sb.append(s).append("\n"));
return sb.toString();
} catch (Exception e) {
return "";
}
}
}

View File

@ -0,0 +1,85 @@
package com.fongmi.tools.bean;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Channel {
@SerializedName("urls")
private List<String> urls;
@SerializedName("number")
private String number;
@SerializedName("logo")
private String logo;
@SerializedName("epg")
private String epg;
@SerializedName("name")
private String name;
@SerializedName("ua")
private String ua;
public static Channel create() {
return new Channel();
}
public List<String> getUrls() {
return urls = urls == null ? new ArrayList<>() : urls;
}
public String getNumber() {
return number == null ? "" : number;
}
public String getName() {
return name == null ? "" : name;
}
public Channel number(String number) {
this.number = number;
return this;
}
public Channel logo(String logo) {
this.logo = logo;
return this;
}
public Channel epg(String epg) {
this.epg = epg;
return this;
}
public Channel name(String name) {
this.name = name;
return this;
}
public Channel ua(String ua) {
this.ua = ua;
return this;
}
public void addUrls(String... urls) {
getUrls().addAll(new ArrayList<>(Arrays.asList(urls)));
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Channel)) return false;
Channel it = (Channel) obj;
return getName().equals(it.getName()) || getNumber().equals(it.getNumber());
}
public static class Sorter implements Comparator<Channel> {
@Override
public int compare(Channel c1, Channel c2) {
return Integer.compare(Integer.parseInt(c1.getNumber()), Integer.parseInt(c2.getNumber()));
}
}
}

View File

@ -0,0 +1,65 @@
package com.fongmi.tools.bean;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
public class Group {
@SerializedName("channel")
private List<Channel> channel;
@SerializedName("logo")
private String logo;
@SerializedName("name")
private String name;
@SerializedName("pass")
private String pass;
public static Group create(String name) {
return new Group(name);
}
public Group(String name) {
this.name = name;
if (!name.contains("_")) return;
setName(name.split("_")[0]);
setPass(name.split("_")[1]);
}
public List<Channel> getChannel() {
return channel = channel == null ? new ArrayList<>() : channel;
}
public void setLogo(String logo) {
this.logo = logo;
}
public String getName() {
return name == null ? "" : name;
}
public void setName(String name) {
this.name = name;
}
public void setPass(String pass) {
this.pass = pass;
}
public Channel find(Channel channel) {
int index = getChannel().indexOf(channel);
if (index != -1) return getChannel().get(index);
getChannel().add(channel);
return channel;
}
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (this == obj) return true;
if (!(obj instanceof Group)) return false;
Group it = (Group) obj;
return getName().equals(it.getName()) && getChannel().size() == it.getChannel().size();
}
}