Clean code

This commit is contained in:
FongMi 2022-12-06 15:14:06 +08:00
parent 970aa5ab3f
commit 2e1a802dfd
4 changed files with 47 additions and 24 deletions

View File

@ -8,7 +8,9 @@ import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class Drive { public class Drive {
@ -84,6 +86,22 @@ public class Drive {
return this; return this;
} }
public String params(String keyword) {
if (isNew()) {
Map<String, Object> params = new HashMap<>();
params.put("keywords", keyword);
params.put("page", 1);
params.put("parent", "/");
params.put("per_page", 100);
return new Gson().toJson(params);
} else {
Map<String, Object> params = new HashMap<>();
params.put("keyword", keyword);
params.put("path", "/");
return new Gson().toJson(params);
}
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) return true; if (this == obj) return true;

View File

@ -14,7 +14,6 @@ import com.github.catvod.crawler.Spider;
import com.github.catvod.net.OkHttpUtil; import com.github.catvod.net.OkHttpUtil;
import com.github.catvod.utils.Misc; import com.github.catvod.utils.Misc;
import com.github.catvod.utils.Trans; import com.github.catvod.utils.Trans;
import com.google.gson.Gson;
import org.json.JSONObject; import org.json.JSONObject;
@ -25,7 +24,6 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
public class AList extends Spider { public class AList extends Spider {
@ -138,8 +136,7 @@ public class AList extends Spider {
params.put("path", path); params.put("path", path);
params.put("password", drive.getPassword()); params.put("password", drive.getPassword());
String response = OkHttpUtil.postJson(drive.getApi(), params.toString()); String response = OkHttpUtil.postJson(drive.getApi(), params.toString());
String json = drive.isNew() ? new JSONObject(response).getJSONObject("data").toString() : new JSONObject(response).getJSONObject("data").getJSONArray("files").getJSONObject(0).toString(); return Item.objectFrom(getDetailJson(drive.isNew(), response));
return Item.objectFrom(json);
} catch (Exception e) { } catch (Exception e) {
return new Item(); return new Item();
} }
@ -154,8 +151,7 @@ public class AList extends Spider {
params.put("path", path); params.put("path", path);
params.put("password", drive.getPassword()); params.put("password", drive.getPassword());
String response = OkHttpUtil.postJson(drive.listApi(), params.toString()); String response = OkHttpUtil.postJson(drive.listApi(), params.toString());
String json = new JSONObject(response).getJSONObject("data").getJSONArray(drive.isNew() ? "content" : "files").toString(); List<Item> items = Item.arrayFrom(getListJson(drive.isNew(), response));
List<Item> items = Item.arrayFrom(json);
Iterator<Item> iterator = items.iterator(); Iterator<Item> iterator = items.iterator();
if (filter) while (iterator.hasNext()) if (iterator.next().ignore(drive.isNew())) iterator.remove(); if (filter) while (iterator.hasNext()) if (iterator.next().ignore(drive.isNew())) iterator.remove();
return items; return items;
@ -164,25 +160,10 @@ public class AList extends Spider {
} }
} }
private String getParams(boolean isNew, String keyword) {
Map<String, Object> params = new HashMap<>();
if (isNew) {
params.put("keywords", keyword);
params.put("page", 1);
params.put("parent", "/");
params.put("per_page", 100);
} else {
params.put("keyword", keyword);
params.put("path", "/");
}
return new Gson().toJson(params);
}
private void search(CountDownLatch cd, List<Vod> list, Drive drive, String keyword) { private void search(CountDownLatch cd, List<Vod> list, Drive drive, String keyword) {
try { try {
String response = OkHttpUtil.postJson(drive.searchApi(), getParams(drive.isNew(), keyword)); String response = OkHttpUtil.postJson(drive.searchApi(), drive.params(keyword));
String json = drive.isNew() ? new JSONObject(response).getJSONObject("data").getJSONArray("content").toString() : new JSONObject(response).getJSONArray("data").toString(); List<Item> items = Item.arrayFrom(getSearchJson(drive.isNew(), response));
List<Item> items = Item.arrayFrom(json);
for (Item item : items) if (!item.ignore(drive.isNew())) list.add(item.getVod(drive.getName())); for (Item item : items) if (!item.ignore(drive.isNew())) list.add(item.getVod(drive.getName()));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -191,6 +172,30 @@ public class AList extends Spider {
} }
} }
private String getListJson(boolean isNew, String response) throws Exception {
if (isNew) {
return new JSONObject(response).getJSONObject("data").getJSONArray("content").toString();
} else {
return new JSONObject(response).getJSONObject("data").getJSONArray("files").toString();
}
}
private String getDetailJson(boolean isNew, String response) throws Exception {
if (isNew) {
return new JSONObject(response).getJSONObject("data").toString();
} else {
return new JSONObject(response).getJSONObject("data").getJSONArray("files").getJSONObject(0).toString();
}
}
private String getSearchJson(boolean isNew, String response) throws Exception {
if (isNew) {
return new JSONObject(response).getJSONObject("data").getJSONArray("content").toString();
} else {
return new JSONObject(response).getJSONArray("data").toString();
}
}
private String findSubs(String path, List<Item> items) { private String findSubs(String path, List<Item> items) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (Item item : items) if (Misc.isSub(item.getExt())) sb.append("~~~").append(Trans.get(item.getName())).append("@").append(Misc.getSubMimeType(item.getExt())).append("@").append(item.getVodId(path)); for (Item item : items) if (Misc.isSub(item.getExt())) sb.append("~~~").append(Trans.get(item.getName())).append("@").append(Misc.getSubMimeType(item.getExt())).append("@").append(item.getVodId(path));

Binary file not shown.

View File

@ -1 +1 @@
10acad2429c1de8501f2b52a85730bcf 906b337d4b4baef13292d004e5fd8f9b