Update tools

This commit is contained in:
FongMi 2023-01-03 13:55:03 +08:00
parent cfa8d12e83
commit 0d61a541ee
12 changed files with 151 additions and 162 deletions

View File

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

View File

@ -1,10 +1,18 @@
plugins { plugins {
id 'java-library' id 'java'
} }
java { java {
sourceCompatibility = JavaVersion.VERSION_1_8 sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_11
}
repositories {
mavenCentral()
}
compileJava {
options.encoding = 'UTF-8'
} }
dependencies { dependencies {

BIN
tools/gradle/wrapper/gradle-wrapper.jar vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

4
tools/release/README.md Normal file
View File

@ -0,0 +1,4 @@
data.json 為對應表,可依據需求新增修改 epg、name、logo。
1. 執行 run.bat
2. 輸入直播文本網址或檔名
3. 自動產出 live.json

1
tools/release/run.bat Normal file
View File

@ -0,0 +1 @@
java -jar run.jar

BIN
tools/release/run.jar Normal file

Binary file not shown.

1
tools/settings.gradle Normal file
View File

@ -0,0 +1 @@
rootProject.name = 'tools'

View File

@ -0,0 +1,2 @@
Manifest-Version: 1.0
Main-Class: com.fongmi.tools.Run

View File

@ -14,96 +14,94 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Scanner; import java.util.Scanner;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class Run { public class Run {
private final List<Group> groups; private final List<Group> groups;
private final List<Data> data; private final List<Data> data;
private final Gson gson; private final Gson gson;
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
new Run().start(); if (args.length > 0) {
} new Run().start(args[0]);
} else {
System.out.print("請輸入網址或檔名:");
new Run().start(new Scanner(System.in).nextLine());
}
}
public Run() { public Run() {
groups = new ArrayList<>(); groups = new ArrayList<>();
data = Data.arrayFrom(Util.getFile(getClass(), "data.json")); data = Data.arrayFrom(Util.getFile("data.json"));
gson = new Gson().newBuilder().disableHtmlEscaping().setPrettyPrinting().create(); gson = new Gson().newBuilder().disableHtmlEscaping().setPrettyPrinting().create();
} }
private void start() throws IOException { private void start(String text) throws IOException {
Scanner scanner = new Scanner(System.in); if (text.startsWith("http")) parse(Util.call(text));
System.out.print("請輸入網址或檔名:"); else parse(Util.getFile(text));
String text = scanner.nextLine(); //parseTxt(Util.getFile(getClass(), "live.txt"));
if (text.startsWith("http")) parse(new OkHttpClient().newCall(new Request.Builder().url(text).build()).execute().body().string()); writeFile();
else parse(Util.getFile(getClass(), text)); }
//parseTxt(Util.getFile(getClass(), "live.txt"));
System.out.println(gson.toJson(groups));
writeFile();
}
private void parse(String text) { private void parse(String text) {
for (String line : text.split("\n")) { for (String line : text.split("\n")) {
String[] split = line.split(","); String[] split = line.split(",");
if (split.length < 2) continue; if (split.length < 2) continue;
if (line.contains("#genre#")) groups.add(Group.create(split[0])); if (line.contains("#genre#")) groups.add(Group.create(split[0]));
if (split[1].contains("://")) { if (split[1].contains("://")) {
Group group = groups.get(groups.size() - 1); Group group = groups.get(groups.size() - 1);
String name = split[0]; String name = split[0];
String url = split[1].trim(); String url = split[1].trim();
group.find(Channel.create().name(name).epg(name)).addUrls(url.split("#")); group.find(Channel.create().name(name).epg(name)).addUrls(url.split("#"));
} }
} }
int number = 0; int number = 0;
for (Group group : groups) { for (Group group : groups) {
for (Channel channel : group.getChannel()) { for (Channel channel : group.getChannel()) {
channel.number(String.format(Locale.getDefault(), "%03d", ++number)); channel.number(String.format(Locale.getDefault(), "%03d", ++number));
combine(channel); combine(channel);
} }
} }
} }
private void parseTxt(String text) { private void parseTxt(String text) {
for (String line : text.split("\n")) { for (String line : text.split("\n")) {
String[] split = line.split(","); String[] split = line.split(",");
if (split.length < 2) continue; if (split.length < 2) continue;
if (line.contains("#genre#")) groups.add(Group.create(split[0])); if (line.contains("#genre#")) groups.add(Group.create(split[0]));
if (!line.contains("://")) continue; if (!line.contains("://")) continue;
Group group = groups.get(groups.size() - 1); Group group = groups.get(groups.size() - 1);
String number = split[0]; String number = split[0];
String epg = split[1]; String epg = split[1];
String logo = split[2]; String logo = split[2];
String name = split[3]; String name = split[3];
String url = split[4]; String url = split[4];
group.find(Channel.create().number(number).epg(epg).logo(logo).name(name).ua(getUa(url))).addUrls(url.split("#")); group.find(Channel.create().number(number).epg(epg).logo(logo).name(name).ua(getUa(url))).addUrls(url.split("#"));
} }
} }
private void combine(Channel channel) { private void combine(Channel channel) {
for (Data item : data) { for (Data item : data) {
if (item.getName().contains(channel.getName())) { if (item.getName().contains(channel.getName())) {
channel.epg(item.getEpgid()); channel.logo(item.getLogo());
channel.logo(item.getLogo()); channel.epg(item.getEpg());
break; break;
} }
} }
} }
private String getUa(String url) { private String getUa(String url) {
if (url.contains("play-live.ifeng")) return "okhttp/3.15"; if (url.contains("play-live.ifeng")) return "okhttp/3.15";
return null; return null;
} }
private void writeFile() { private void writeFile() {
try { try {
File file = new File("json", "live.json"); File file = new File("live.json");
PrintWriter writer = new PrintWriter(file, String.valueOf(StandardCharsets.UTF_8)); PrintWriter writer = new PrintWriter(file, String.valueOf(StandardCharsets.UTF_8));
writer.println(gson.toJson(groups)); writer.println(gson.toJson(groups));
writer.close(); writer.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

View File

@ -1,22 +1,39 @@
package com.fongmi.tools; package com.fongmi.tools;
import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.stream.Stream; import java.util.stream.Stream;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class Util { public class Util {
public static String getFile(Class<?> clz, String fileName) { public static String call(String url) throws IOException {
try { return new OkHttpClient().newCall(new Request.Builder().url(url).build()).execute().body().string();
StringBuilder sb = new StringBuilder(); }
URI uri = clz.getClassLoader().getResource(fileName).toURI();
Stream<String> stream = Files.lines(Paths.get(uri), StandardCharsets.UTF_8); public static String getFile(Class<?> clz, String fileName) {
stream.forEach(s -> sb.append(s).append("\n")); try {
return sb.toString(); StringBuilder sb = new StringBuilder();
} catch (Exception e) { URI uri = clz.getClassLoader().getResource(fileName).toURI();
return ""; 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 "";
}
}
public static String getFile(String fileName) {
try {
return Files.readString(Path.of(fileName));
} catch (Exception e) {
return "";
}
}
} }

View File

@ -10,74 +10,28 @@ import java.util.List;
public class Data { public class Data {
@SerializedName("tvid") @SerializedName("name")
private String tvid; private String name;
@SerializedName("epgid") @SerializedName("epg")
private String epgid; private String epg;
@SerializedName("name")
private String name;
@SerializedName("status")
private String status;
@SerializedName("note")
private String note;
@SerializedName("logo")
private String logo;
public static Data objectFrom(String str) { @SerializedName("logo")
return new Gson().fromJson(str, Data.class); private String logo;
}
public static List<Data> arrayFrom(String str) { public static List<Data> arrayFrom(String str) {
Type listType = new TypeToken<ArrayList<Data>>() { Type listType = new TypeToken<ArrayList<Data>>() {}.getType();
}.getType(); return new Gson().fromJson(str, listType);
return new Gson().fromJson(str, listType); }
}
public String getTvid() { public String getName() {
return tvid; return name;
} }
public void setTvid(String tvid) { public String getEpg() {
this.tvid = tvid; return epg;
} }
public String getEpgid() { public String getLogo() {
return epgid; return logo;
} }
public void setEpgid(String epgid) {
this.epgid = epgid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
} }