parent
155066d132
commit
92c50f9780
@ -0,0 +1,107 @@ |
||||
package com.github.catvod.bean.webdav; |
||||
|
||||
import android.net.Uri; |
||||
import android.text.TextUtils; |
||||
|
||||
import com.github.catvod.bean.Class; |
||||
import com.github.catvod.bean.Vod; |
||||
import com.github.catvod.utils.Utils; |
||||
import com.google.gson.Gson; |
||||
import com.google.gson.annotations.SerializedName; |
||||
import com.thegrizzlylabs.sardineandroid.DavResource; |
||||
import com.thegrizzlylabs.sardineandroid.Sardine; |
||||
import com.thegrizzlylabs.sardineandroid.impl.OkHttpSardine; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class Drive { |
||||
|
||||
@SerializedName("vodPic") |
||||
private String vodPic; |
||||
@SerializedName("drives") |
||||
private List<Drive> drives; |
||||
@SerializedName("name") |
||||
private String name; |
||||
@SerializedName("server") |
||||
private String server; |
||||
@SerializedName("user") |
||||
private String user; |
||||
@SerializedName("pass") |
||||
private String pass; |
||||
@SerializedName("path") |
||||
private String path; |
||||
@SerializedName("webdav") |
||||
private Sardine webdav; |
||||
|
||||
public static Drive objectFrom(String str) { |
||||
return new Gson().fromJson(str, Drive.class); |
||||
} |
||||
|
||||
public Drive(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public List<Drive> getDrives() { |
||||
return drives == null ? new ArrayList<>() : drives; |
||||
} |
||||
|
||||
public String getVodPic() { |
||||
return TextUtils.isEmpty(vodPic) ? "" : vodPic; |
||||
} |
||||
|
||||
public String getName() { |
||||
return TextUtils.isEmpty(name) ? "" : name; |
||||
} |
||||
|
||||
public String getServer() { |
||||
return TextUtils.isEmpty(server) ? "" : server; |
||||
} |
||||
|
||||
public String getUser() { |
||||
return TextUtils.isEmpty(user) ? "" : user; |
||||
} |
||||
|
||||
public String getPass() { |
||||
return TextUtils.isEmpty(pass) ? "" : pass; |
||||
} |
||||
|
||||
public String getPath() { |
||||
return TextUtils.isEmpty(path) ? "" : path; |
||||
} |
||||
|
||||
public void setPath(String path) { |
||||
this.path = TextUtils.isEmpty(path) ? "" : path; |
||||
} |
||||
|
||||
public String getHost() { |
||||
return getServer().replace(getPath(), ""); |
||||
} |
||||
|
||||
public Sardine getWebdav() { |
||||
return webdav; |
||||
} |
||||
|
||||
public Class toType() { |
||||
return new Class(getName(), getName(), "1"); |
||||
} |
||||
|
||||
public Drive init() { |
||||
webdav = new OkHttpSardine(); |
||||
webdav.setCredentials(getUser(), getPass()); |
||||
setPath(Uri.parse(getServer()).getPath()); |
||||
return this; |
||||
} |
||||
|
||||
public Vod vod(DavResource item, String vodPic) { |
||||
return new Vod(getName() + item.getPath(), item.getName(), vodPic, Utils.getSize(item.getContentLength()), item.isDirectory()); |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (this == obj) return true; |
||||
if (!(obj instanceof Drive)) return false; |
||||
Drive it = (Drive) obj; |
||||
return getName().equals(it.getName()); |
||||
} |
||||
} |
||||
@ -0,0 +1,37 @@ |
||||
package com.github.catvod.bean.webdav; |
||||
|
||||
import com.thegrizzlylabs.sardineandroid.DavResource; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.Comparator; |
||||
import java.util.List; |
||||
|
||||
public class Sorter implements Comparator<DavResource> { |
||||
|
||||
private final String type; |
||||
private final String order; |
||||
|
||||
public static void sort(String type, String order, List<DavResource> items) { |
||||
Collections.sort(items, new Sorter(type, order)); |
||||
} |
||||
|
||||
public Sorter(String type, String order) { |
||||
this.type = type; |
||||
this.order = order; |
||||
} |
||||
|
||||
@Override |
||||
public int compare(DavResource o1, DavResource o2) { |
||||
boolean asc = order.equals("asc"); |
||||
switch (type) { |
||||
case "name": |
||||
return asc ? o1.getName().compareTo(o2.getName()) : o2.getName().compareTo(o1.getName()); |
||||
case "size": |
||||
return asc ? Long.compare(o1.getContentLength(), o2.getContentLength()) : Long.compare(o2.getContentLength(), o1.getContentLength()); |
||||
case "date": |
||||
return asc ? o1.getModified().compareTo(o2.getModified()) : o2.getModified().compareTo(o1.getModified()); |
||||
default: |
||||
return -1; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,197 @@ |
||||
package com.github.catvod.spider; |
||||
|
||||
import android.content.Context; |
||||
import android.text.TextUtils; |
||||
|
||||
import com.github.catvod.bean.Class; |
||||
import com.github.catvod.bean.Filter; |
||||
import com.github.catvod.bean.Result; |
||||
import com.github.catvod.bean.Sub; |
||||
import com.github.catvod.bean.Vod; |
||||
import com.github.catvod.bean.webdav.Drive; |
||||
import com.github.catvod.bean.webdav.Sorter; |
||||
import com.github.catvod.crawler.Spider; |
||||
import com.github.catvod.net.OkHttp; |
||||
import com.github.catvod.utils.Utils; |
||||
import com.thegrizzlylabs.sardineandroid.DavResource; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.HashMap; |
||||
import java.util.Iterator; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
public class WebDAV extends Spider { |
||||
|
||||
private static List<Drive> drives; |
||||
private List<String> playExt; |
||||
private List<String> allExt; |
||||
private String vodPic; |
||||
private String ext; |
||||
|
||||
private List<Filter> getFilter() { |
||||
List<Filter> items = new ArrayList<>(); |
||||
items.add(new Filter("type", "排序類型", Arrays.asList(new Filter.Value("預設", ""), new Filter.Value("名稱", "name"), new Filter.Value("大小", "size"), new Filter.Value("修改時間", "date")))); |
||||
items.add(new Filter("order", "排序方式", Arrays.asList(new Filter.Value("預設", ""), new Filter.Value("⬆", "asc"), new Filter.Value("⬇", "desc")))); |
||||
return items; |
||||
} |
||||
|
||||
private void fetchRule() { |
||||
if (drives != null && !drives.isEmpty()) return; |
||||
if (ext.startsWith("http")) ext = OkHttp.string(ext); |
||||
Drive drive = Drive.objectFrom(ext); |
||||
drives = drive.getDrives(); |
||||
vodPic = drive.getVodPic(); |
||||
} |
||||
|
||||
private String getExt(DavResource item) { |
||||
return item.getName().substring(item.getName().lastIndexOf(".") + 1); |
||||
} |
||||
|
||||
private String removeExt(DavResource item) { |
||||
return item.getName().indexOf(".") > 0 ? item.getName().substring(0, item.getName().lastIndexOf(".")) : item.getName(); |
||||
} |
||||
|
||||
private static Drive getDrive(String name) { |
||||
return drives.get(drives.indexOf(new Drive(name))); |
||||
} |
||||
|
||||
@Override |
||||
public void init(Context context, String extend) { |
||||
playExt = Arrays.asList("mp4", "mkv", "flv", "avi", "mp3", "aac", "flac", "m4a"); |
||||
allExt = new ArrayList<>(Arrays.asList("ass", "ssa", "srt")); |
||||
allExt.addAll(playExt); |
||||
ext = extend; |
||||
fetchRule(); |
||||
} |
||||
|
||||
@Override |
||||
public String homeContent(boolean filter) throws Exception { |
||||
fetchRule(); |
||||
List<Class> classes = new ArrayList<>(); |
||||
LinkedHashMap<String, List<Filter>> filters = new LinkedHashMap<>(); |
||||
for (Drive drive : drives) classes.add(drive.init().toType()); |
||||
for (Class item : classes) filters.put(item.getTypeId(), getFilter()); |
||||
return Result.string(classes, filters); |
||||
} |
||||
|
||||
@Override |
||||
public String categoryContent(String tid, String pg, boolean filter, HashMap<String, String> extend) throws Exception { |
||||
String key = tid.contains("/") ? tid.substring(0, tid.indexOf("/")) : tid; |
||||
String path = tid.contains("/") ? tid.substring(tid.indexOf("/")) : ""; |
||||
String order = extend.containsKey("order") ? extend.get("order") : ""; |
||||
String type = extend.containsKey("type") ? extend.get("type") : ""; |
||||
List<DavResource> folders = new ArrayList<>(); |
||||
List<DavResource> files = new ArrayList<>(); |
||||
List<Vod> list = new ArrayList<>(); |
||||
Drive drive = getDrive(key); |
||||
for (DavResource item : getList(drive, path, playExt)) { |
||||
if (item.isDirectory()) folders.add(item); |
||||
else files.add(item); |
||||
} |
||||
if (!TextUtils.isEmpty(type) && !TextUtils.isEmpty(order)) { |
||||
Sorter.sort(type, order, folders); |
||||
Sorter.sort(type, order, files); |
||||
} |
||||
for (DavResource item : folders) list.add(drive.vod(item, vodPic)); |
||||
for (DavResource item : files) list.add(drive.vod(item, vodPic)); |
||||
return Result.get().vod(list).page().string(); |
||||
} |
||||
|
||||
@Override |
||||
public String detailContent(List<String> ids) throws Exception { |
||||
String id = ids.get(0); |
||||
String key = id.contains("/") ? id.substring(0, id.indexOf("/")) : id; |
||||
String name = id.substring(id.lastIndexOf("/") + 1); |
||||
String parent = id.substring(0, id.lastIndexOf("/")); |
||||
String path = parent.contains("/") ? parent.substring(parent.indexOf("/")) + "/" : ""; |
||||
Drive drive = getDrive(key); |
||||
List<DavResource> parents = getList(drive, path, allExt); |
||||
List<DavResource> subs = getSubs(parents); |
||||
Sorter.sort("name", "asc", parents); |
||||
List<String> playUrls = new ArrayList<>(); |
||||
for (DavResource item : parents) { |
||||
if (playExt.contains(getExt(item))) { |
||||
playUrls.add(item.getName() + "$" + drive.getName() + item.getPath() + findSubs(drive, item, subs)); |
||||
} |
||||
} |
||||
Vod vod = new Vod(); |
||||
vod.setVodId(id); |
||||
//TODO 資料夾名稱
|
||||
vod.setVodName(name); |
||||
vod.setVodPic(vodPic); |
||||
vod.setVodPlayFrom(key); |
||||
vod.setVodPlayUrl(TextUtils.join("#", playUrls)); |
||||
return Result.string(vod); |
||||
} |
||||
|
||||
@Override |
||||
public String playerContent(String flag, String id, List<String> vipFlags) throws Exception { |
||||
String[] ids = id.split("~~~"); |
||||
String key = ids[0].contains("/") ? ids[0].substring(0, ids[0].indexOf("/")) : ids[0]; |
||||
return Result.get().url(getProxyUrl(key, ids[0])).subs(getSub(ids)).string(); |
||||
} |
||||
|
||||
private List<DavResource> getList(Drive drive, String path, List<String> ext) throws Exception { |
||||
path = drive.getHost() + (path.startsWith(drive.getPath()) ? path : drive.getPath() + path); |
||||
List<DavResource> items = drive.getWebdav().list(path); |
||||
items.remove(0); //Remove parent
|
||||
Iterator<DavResource> iterator = items.iterator(); |
||||
while (iterator.hasNext()) { |
||||
DavResource item = iterator.next(); |
||||
if (!item.isDirectory() && !item.getName().contains(".")) iterator.remove(); |
||||
if (!item.isDirectory() && !ext.contains(getExt(item))) iterator.remove(); |
||||
} |
||||
return items; |
||||
} |
||||
|
||||
private List<DavResource> getSubs(List<DavResource> items) { |
||||
List<DavResource> subs = new ArrayList<>(); |
||||
for (DavResource item : items) if (Utils.isSub(getExt(item))) subs.add(item); |
||||
return subs; |
||||
} |
||||
|
||||
private String findSubs(Drive drive, DavResource res, List<DavResource> items) { |
||||
StringBuilder sb = new StringBuilder(); |
||||
for (DavResource item : items) if (removeExt(item).equals(removeExt(res))) sb.append("~~~").append(item.getName()).append("@@@").append(getExt(item)).append("@@@").append(drive.getName() + item.getPath()); |
||||
return sb.length() > 0 ? sb.toString() : findSubs(drive, items); |
||||
} |
||||
|
||||
private String findSubs(Drive drive, List<DavResource> items) { |
||||
StringBuilder sb = new StringBuilder(); |
||||
for (DavResource item : items) sb.append("~~~").append(item.getName()).append("@@@").append(getExt(item)).append("@@@").append(drive.getName() + item.getPath()); |
||||
return sb.toString(); |
||||
} |
||||
|
||||
private List<Sub> getSub(String[] ids) { |
||||
List<Sub> sub = new ArrayList<>(); |
||||
for (String text : ids) { |
||||
if (!text.contains("@@@")) continue; |
||||
String[] split = text.split("@@@"); |
||||
String name = split[0]; |
||||
String ext = split[1]; |
||||
String key = split[2].contains("/") ? split[2].substring(0, split[2].indexOf("/")) : split[2]; |
||||
String url = getProxyUrl(key, split[2]); |
||||
sub.add(Sub.create().name(name).ext(ext).url(url)); |
||||
} |
||||
return sub; |
||||
} |
||||
|
||||
private String getProxyUrl(String key, String path) { |
||||
return Proxy.getUrl() + "?do=webdav" + "&key=" + key + "&url=" + getDrive(key).getHost() + path.replace(key, ""); |
||||
} |
||||
|
||||
public static Object[] vod(Map<String, String> params) throws IOException { |
||||
String key = params.get("key"); |
||||
String url = params.get("url"); |
||||
Drive drive = getDrive(key); |
||||
Object[] result = new Object[3]; |
||||
result[0] = 200; |
||||
result[1] = "application/octet-stream"; |
||||
result[2] = drive.getWebdav().get(url); |
||||
return result; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue