Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完善3.0书源转换 #552

Merged
merged 1 commit into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 53 additions & 22 deletions app/src/main/java/com/kunfei/bookshelf/bean/BookSource3Bean.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import kotlin.jvm.Transient;
import kotlinx.android.parcel.Parcelize;

Expand Down Expand Up @@ -104,6 +110,27 @@ public Object clone() {
return this;
}*/

// 给书源增加一个标签
public BookSource3Bean addGroupTag(String tag) {
if (this.bookSourceGroup != null) {
//为了避免空格、首尾位置的差异造成影响,这里做循环处理
String[] tags = (this.bookSourceGroup + ";" + tag).split(";");

List<String> list = new ArrayList<>();
list.add(tag);
for (String s : tags) {
if (!list.contains(s)) {
list.add(s);
}
}
bookSourceGroup = tag;
for (int i = 1; i < list.size(); i++) {
bookSourceGroup = bookSourceGroup + ";" + list.get(i);
}
}
return this;
}

class httpRequest {
String method;
String body;
Expand All @@ -120,39 +147,43 @@ private String searchUrl2RuleSearchUrl(String searchUrl) {
try {
Gson gson = new Gson();
httpRequest request = gson.fromJson(strings[1], httpRequest.class);
if (gson.toJson(request).replaceAll("\\s", "").length() > 0)
q = "";

if (request.charset != null) {
if (request.charset.trim().length() > 0)
q = q + "|char=" + request.charset;
}

if (request.method != null) {
if (request.method.toLowerCase().contains("post"))
q = "@" + q;
else
q = "?" + q;
}

if (request.headers != null) {
if (this.header == null)
this.header = request.headers;
else if (request.headers.trim().length() < 1)
this.header = request.headers;
}

if (request.body != null) {
q = request.body
q = q + request.body
.replace("{{key}}", "searchKey")
.replaceFirst("\\{\\{([^{}]*)page([^{}]*)\\}\\}", "$1searchPage$2")
;

if (request.charset != null) {
if (request.charset.trim().length() > 0)
q = q + "|char=" + request.charset;
}

if (request.method != null) {
if (request.method.toLowerCase().contains("post"))
q = "@" + q;
else
q = "?" + q;
}

if (request.headers != null) {
if (this.header == null)
this.header = request.headers;
else if (request.headers.trim().length() < 1)
this.header = request.headers;
}

return strings[0] + q;
}

RuleSearchUrl = strings[0] + q;

} catch (Exception ignored) {
}
}

return searchUrl.replaceAll("\\s", "")
return RuleSearchUrl.replaceAll("\\s", "")
.replace("{{key}}", "searchKey")
.replaceFirst("\\{\\{([^{}]*)page([^{}]*)\\}\\}", "$1searchPage$2")
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,15 @@ public void setText(String bookSourceStr) {
if (bookSourceStr.trim().length() > 5) {
mView.setText( mathcSourceBean(bookSourceStr.trim()) );
} else {
mView.toast("似乎不是书源内容");
// 理论上这里已经没用了
Gson gson = new Gson();
BookSourceBean bookSourceBean = gson.fromJson(bookSourceStr, BookSourceBean.class);
mView.setText(bookSourceBean);
// Gson gson = new Gson();
// BookSourceBean bookSourceBean = gson.fromJson(bookSourceStr, BookSourceBean.class);
// mView.setText(bookSourceBean);
}
} catch (Exception e) {
if (!setTextV3(bookSourceStr)) {
mView.toast("数据格式不对");
e.printStackTrace();
}
mView.toast("数据格式不对");
e.printStackTrace();
}
}

Expand Down Expand Up @@ -105,48 +104,20 @@ private BookSourceBean mathcSourceBean(String str) {
bookSource2Bean = gson.fromJson(str, BookSourceBean.class);
}
r2 = gson.toJson(bookSource2Bean).length();
if (r2 >= r3)
// r2 r3的计算在调用searchUrl2RuleSearchUrl() 等高级转换方法之前,是简化算法的粗糙的做法
if (r2 > r3)
return bookSource2Bean;
} catch (Exception e) {
e.printStackTrace();
}

if (r3 > 0) {
mView.toast("导入了阅读3.0书源。如有Bug请及时上报");
return bookSource3Bean.toBookSourceBean();
return bookSource3Bean.addGroupTag("阅读3.0书源").toBookSourceBean();
}
return bookSource2Bean;
}

// 把阅读v3.0的书源解析并填充到文本框内
// 输入字符串为多个书源的数组时,只解析第一个源
public boolean setTextV3(String bookSourceStr) {
try {
String str = bookSourceStr.trim();
if (str.length() > 5) {
Gson gson = new Gson();
BookSource3Bean bookSource3Bean;

if (str.charAt(0) == '[' && str.charAt(str.length() - 1) == ']') {
List<BookSource3Bean> list = gson.fromJson(str, new TypeToken<List<BookSource3Bean>>() {
}.getType());
bookSource3Bean = list.get(0);
} else {
bookSource3Bean = gson.fromJson(str, BookSource3Bean.class);
}

mView.setText(bookSource3Bean.toBookSourceBean());
mView.toast("导入了阅读3.0书源。如有Bug请及时上报");
return true;
} else
mView.toast("数据格式不对");
} catch (Exception e) {
e.printStackTrace();
mView.toast("数据格式不对");
}
return false;
}

@Override
public void attachView(@NonNull IView iView) {
super.attachView(iView);
Expand Down