Clean code

This commit is contained in:
FongMi 2025-02-03 15:08:25 +08:00
parent 069b641b04
commit 0de123b8a1
15 changed files with 3 additions and 2969 deletions

View File

@ -43,8 +43,8 @@ dependencies {
implementation 'com.squareup.okhttp3:okhttp:' + okhttpVersion
implementation 'com.github.thegrizzlylabs:sardine-android:0.9'
implementation 'wang.harlon.quickjs:wrapper-android:2.4.3'
implementation 'com.google.code.gson:gson:2.12.1'
implementation 'com.google.code.gson:gson:2.11.0'
implementation 'cn.wanghaomiao:JsoupXpath:2.5.1'
implementation 'com.orhanobut:logger:2.2.0'
implementation 'org.jsoup:jsoup:1.17.2'
implementation 'org.jsoup:jsoup:1.18.3'
}

Binary file not shown.

View File

@ -1 +1 @@
3cbe660633a395f10a0ae0ee543e935a
45b44b65267c0c737bd436140ff9dcb9

View File

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

1
tools/.gitignore vendored
View File

@ -1 +0,0 @@
/build

View File

@ -1,14 +0,0 @@
plugins {
id 'java-library'
}
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
dependencies {
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
implementation 'com.google.guava:guava:32.0.1-jre'
implementation 'com.google.code.gson:gson:2.11.0'
}

Binary file not shown.

View File

@ -1,6 +0,0 @@
#Mon Nov 18 21:36:17 CST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -1,101 +0,0 @@
package com.fongmi.tools;
import com.fongmi.tools.bean.Channel;
import com.fongmi.tools.bean.Data;
import com.fongmi.tools.bean.Group;
import com.google.gson.Gson;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class Live {
private final List<Group> groups;
private final List<Data> data;
private final Gson gson;
public static void main(String[] args) throws IOException {
new Live().start("http://home.jundie.top:81/Cat/tv/live.txt");
}
public Live() {
groups = new ArrayList<>();
data = Data.arrayFrom(Utils.getFile(getClass(), "data.json"));
gson = new Gson().newBuilder().disableHtmlEscaping().setPrettyPrinting().create();
}
private void start(String text) throws IOException {
//parseTxt(Util.getFile(getClass(), "test.txt"));
parse(Utils.call(text));
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#")) groups.add(Group.create(split[0]));
if (split[1].contains("://")) {
Group group = groups.get(groups.size() - 1);
String name = split[0];
String url = split[1].trim();
group.find(Channel.create().name(name).epg(name)).addUrls(url.split("#"));
}
}
int number = 0;
for (Group group : groups) {
for (Channel channel : group.getChannel()) {
channel.number(String.format(Locale.getDefault(), "%03d", ++number));
channel.logo("https://raw.githubusercontent.com/FongMi/TV/release/app/src/main/res/drawable-xxhdpi/ic_img_empty.png");
combine(channel);
}
}
}
private void parseTxt(String text) {
for (String line : text.split("\n")) {
String[] split = line.split(",");
if (split.length < 2) continue;
if (line.contains("#genre#")) groups.add(Group.create(split[0]));
if (!line.contains("://")) continue;
Group group = groups.get(groups.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 void combine(Channel channel) {
for (Data item : data) {
if (item.getName().contains(channel.getName())) {
channel.logo(item.getLogo());
channel.epg(item.getEpg());
break;
}
}
}
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(groups));
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -1,39 +0,0 @@
package com.fongmi.tools;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class Utils {
public static String call(String url) throws IOException {
return new OkHttpClient().newCall(new Request.Builder().url(url).build()).execute().body().string();
}
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 "";
}
}
public static String getFile(String fileName) {
try {
return Files.readString(Path.of(fileName));
} catch (Exception e) {
return "";
}
}
}

View File

@ -1,85 +0,0 @@
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().isEmpty() && 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

@ -1,36 +0,0 @@
package com.fongmi.tools.bean;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class Data {
@SerializedName("name")
private String name;
@SerializedName("epg")
private String epg;
@SerializedName("logo")
private String logo;
public static List<Data> arrayFrom(String str) {
Type listType = new TypeToken<ArrayList<Data>>() {}.getType();
return new Gson().fromJson(str, listType);
}
public String getName() {
return name;
}
public String getEpg() {
return epg;
}
public String getLogo() {
return logo;
}
}

View File

@ -1,65 +0,0 @@
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();
}
}

File diff suppressed because it is too large Load Diff