兼容第三方书源

pull/5/head
fengyuecanzhu 4 years ago
parent 49acebd2f9
commit 1e30cf0629
  1. 11
      app/src/main/java/xyz/fycz/myreader/entity/sourcedebug/DebugEntity.java
  2. 20
      app/src/main/java/xyz/fycz/myreader/model/SearchEngine.java
  3. 12
      app/src/main/java/xyz/fycz/myreader/ui/activity/BookDetailedActivity.java
  4. 20
      app/src/main/java/xyz/fycz/myreader/ui/activity/SourceEditActivity.java
  5. 14
      app/src/main/java/xyz/fycz/myreader/webapi/CommonApi.java
  6. 2
      app/src/main/java/xyz/fycz/myreader/widget/page/PageLoader.java

@ -18,6 +18,7 @@ public class DebugEntity implements Parcelable {
private int debugMode;
private BookSource bookSource;
private String url;
private String key;
private String parseResult;
private String html;
@ -28,6 +29,7 @@ public class DebugEntity implements Parcelable {
debugMode = in.readInt();
bookSource = in.readParcelable(BookSource.class.getClassLoader());
url = in.readString();
key = in.readString();
parseResult = in.readString();
html = in.readString();
}
@ -37,6 +39,7 @@ public class DebugEntity implements Parcelable {
dest.writeInt(debugMode);
dest.writeParcelable(bookSource, flags);
dest.writeString(url);
dest.writeString(key);
dest.writeString(parseResult);
dest.writeString(html);
}
@ -82,6 +85,14 @@ public class DebugEntity implements Parcelable {
this.url = url;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getParseResult() {
return parseResult;
}

@ -137,15 +137,7 @@ public class SearchEngine {
searchSiteIndex++;
if (searchSiteIndex < mSourceList.size()) {
ReadCrawler crawler = mSourceList.get(searchSiteIndex);
String searchKey = keyword;
if (crawler.getSearchCharset() != null && crawler.getSearchCharset().toLowerCase().equals("gbk")) {
try {
searchKey = URLEncoder.encode(keyword, crawler.getSearchCharset());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
CommonApi.search(searchKey, crawler)
CommonApi.search(keyword, crawler)
.subscribeOn(scheduler)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ConcurrentMultiValueMap<SearchBookBean, Book>>() {
@ -191,15 +183,7 @@ public class SearchEngine {
searchSiteIndex++;
if (searchSiteIndex < mSourceList.size()) {
ReadCrawler crawler = mSourceList.get(searchSiteIndex);
String searchKey = title;
if (crawler.getSearchCharset().toLowerCase().equals("gbk")) {
try {
searchKey = URLEncoder.encode(title, crawler.getSearchCharset());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
CommonApi.search(searchKey, crawler)
CommonApi.search(title, crawler)
.subscribeOn(scheduler)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ConcurrentMultiValueMap<SearchBookBean, Book>>() {

@ -106,6 +106,7 @@ public class BookDetailedActivity extends BaseActivity {
private BookGroupDialog mBookGroupDia;
private List<String> tagList = new ArrayList<>();
private Disposable chaptersDis;
private boolean thirdSource;
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
@ -174,6 +175,7 @@ public class BookDetailedActivity extends BaseActivity {
}
mBookGroupDia = new BookGroupDialog(this);
mReadCrawler = ReadCrawlerUtil.getReadCrawler(mBook.getSource());
thirdSource = mReadCrawler instanceof ThirdCrawler;
}
private void initTagList() {
@ -208,7 +210,7 @@ public class BookDetailedActivity extends BaseActivity {
binding.ic.bookDetailRvCatalog.setLayoutManager(new LinearLayoutManager(this));
binding.ic.bookDetailRvCatalog.setAdapter(mCatalogAdapter);
initChapters(false);
if (!thirdSource) initChapters(false);
mCatalogAdapter.setOnItemClickListener((view, pos) -> {
mBook.setHisttoryChapterNum(mChapters.size() - pos - 1);
@ -401,7 +403,7 @@ public class BookDetailedActivity extends BaseActivity {
BookSource source = BookSourceManager.getBookSourceByStr(mBook.getSource());
binding.ih.bookDetailSource.setText(String.format("书源:%s", source.getSourceName()));
ReadCrawler rc = ReadCrawlerUtil.getReadCrawler(source);
if (rc instanceof BookInfoCrawler && StringHelper.isEmpty(mBook.getImgUrl())) {
if ((rc instanceof BookInfoCrawler && StringHelper.isEmpty(mBook.getImgUrl())) || thirdSource) {
binding.pbLoading.setVisibility(View.VISIBLE);
BookInfoCrawler bic = (BookInfoCrawler) rc;
CommonApi.getBookInfo(mBook, bic).compose(RxUtils::toSimpleSingle).subscribe(new MyObserver<Book>() {
@ -410,6 +412,9 @@ public class BookDetailedActivity extends BaseActivity {
if (!App.isDestroy(BookDetailedActivity.this)) {
mHandler.sendMessage(mHandler.obtainMessage(4));
}
if (thirdSource){
initChapters(false);
}
}
@Override
@ -477,7 +482,8 @@ public class BookDetailedActivity extends BaseActivity {
@Override
public void onError(Throwable e) {
binding.pbLoading.setVisibility(View.GONE);
ToastUtils.showError("最新章节加载失败!");
mCatalogAdapter.clear();
ToastUtils.showError("最新章节加载失败,请尝试重新加载!");
if (App.isDebug()) e.printStackTrace();
}
});

@ -5,6 +5,8 @@ import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import androidx.appcompat.widget.Toolbar;
@ -138,6 +140,17 @@ public class SourceEditActivity extends BaseActivity {
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
binding.sSourceType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
invalidateOptionsMenu();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@ -148,6 +161,13 @@ public class SourceEditActivity extends BaseActivity {
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem debug = menu.findItem(R.id.action_debug);
debug.setVisible(!APPCONST.THIRD_SOURCE.equals(getSource().getSourceType()));
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_save) {

@ -2,6 +2,8 @@ package xyz.fycz.myreader.webapi;
import android.text.TextUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
@ -149,19 +151,27 @@ public class CommonApi {
} else {
charset = rc.getCharset();
}
if (rc.getSearchCharset() != null && rc.getSearchCharset().toLowerCase().equals("gbk")) {
try {
key = URLEncoder.encode(key, rc.getSearchCharset());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
String finalCharset = charset;
String finalKey = key;
return Observable.create(emitter -> {
try {
if (rc.isPost()) {
String url = rc.getSearchLink();
String[] urlInfo = url.split(",");
url = urlInfo[0];
String body = makeSearchUrl(urlInfo[1], key);
String body = makeSearchUrl(urlInfo[1], finalKey);
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody requestBody = RequestBody.create(mediaType, body);
emitter.onNext(rc.getBooksFromSearchHtml(OkHttpUtils.getHtml(url, requestBody, finalCharset, rc.getHeaders())));
} else {
emitter.onNext(rc.getBooksFromSearchHtml(OkHttpUtils.getHtml(makeSearchUrl(rc.getSearchLink(), key), finalCharset, rc.getHeaders())));
emitter.onNext(rc.getBooksFromSearchHtml(OkHttpUtils.getHtml(makeSearchUrl(rc.getSearchLink(), finalKey), finalCharset, rc.getHeaders())));
}
} catch (Exception e) {
e.printStackTrace();

@ -1673,7 +1673,7 @@ public abstract class PageLoader {
}
// 重置段落
if (!showTitle) {
paragraph = StringUtils.trim(paragraph);
paragraph = StringUtils.trim(paragraph.replace("\t", ""));
// 如果只有换行符,那么就不执行
if (paragraph.equals("")) continue;
paragraph = indent + paragraph + "\n";

Loading…
Cancel
Save