兼容第三方书源

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

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

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

@ -5,6 +5,8 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
@ -138,6 +140,17 @@ public class SourceEditActivity extends BaseActivity {
@Override @Override
public void onTabReselected(TabLayout.Tab tab) { 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; 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 @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_save) { if (item.getItemId() == R.id.action_save) {

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

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

Loading…
Cancel
Save