diff --git a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java index 2ac80c968..c0975d92e 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java @@ -7,7 +7,6 @@ import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.impl.DefaultStorIOSQLite; import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult; import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults; -import eu.kanade.mangafeed.util.PostResult; import com.pushtorefresh.storio.sqlite.operations.put.PutResult; import com.pushtorefresh.storio.sqlite.operations.put.PutResults; @@ -25,6 +24,7 @@ import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.data.models.MangaStorIOSQLiteDeleteResolver; import eu.kanade.mangafeed.data.models.MangaStorIOSQLitePutResolver; import eu.kanade.mangafeed.data.resolvers.MangaWithUnreadGetResolver; +import eu.kanade.mangafeed.util.PostResult; import rx.Observable; public class DatabaseHelper implements MangaManager, ChapterManager { diff --git a/app/src/main/java/eu/kanade/mangafeed/data/helpers/PreferencesHelper.java b/app/src/main/java/eu/kanade/mangafeed/data/helpers/PreferencesHelper.java index 7006ea780..80c503f70 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/helpers/PreferencesHelper.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/helpers/PreferencesHelper.java @@ -5,7 +5,7 @@ import android.content.SharedPreferences; import android.preference.PreferenceManager; import eu.kanade.mangafeed.R; -import eu.kanade.mangafeed.sources.Source; +import eu.kanade.mangafeed.sources.base.Source; public class PreferencesHelper { diff --git a/app/src/main/java/eu/kanade/mangafeed/data/helpers/SourceManager.java b/app/src/main/java/eu/kanade/mangafeed/data/helpers/SourceManager.java index c8ea9ed52..fcd906ca9 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/helpers/SourceManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/helpers/SourceManager.java @@ -7,7 +7,7 @@ import java.util.List; import eu.kanade.mangafeed.data.caches.CacheManager; import eu.kanade.mangafeed.sources.Batoto; import eu.kanade.mangafeed.sources.MangaHere; -import eu.kanade.mangafeed.sources.Source; +import eu.kanade.mangafeed.sources.base.Source; public class SourceManager { diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java index d7b06695b..c692a9d92 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java @@ -2,7 +2,6 @@ package eu.kanade.mangafeed.data.managers; import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult; import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults; -import eu.kanade.mangafeed.util.PostResult; import com.pushtorefresh.storio.sqlite.operations.put.PutResult; import com.pushtorefresh.storio.sqlite.operations.put.PutResults; @@ -10,6 +9,7 @@ import java.util.List; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.util.PostResult; import rx.Observable; public interface ChapterManager { diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java index c1bfc0f26..ffe87e175 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java @@ -4,7 +4,6 @@ import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult; import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults; import com.pushtorefresh.storio.sqlite.operations.get.PreparedGetListOfObjects; -import eu.kanade.mangafeed.util.PostResult; import com.pushtorefresh.storio.sqlite.operations.put.PutResult; import com.pushtorefresh.storio.sqlite.operations.put.PutResults; import com.pushtorefresh.storio.sqlite.queries.Query; @@ -14,6 +13,7 @@ import java.util.List; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.data.tables.ChaptersTable; +import eu.kanade.mangafeed.util.PostResult; import rx.Observable; public class ChapterManagerImpl extends BaseManager implements ChapterManager { diff --git a/app/src/main/java/eu/kanade/mangafeed/events/SourceChapterEvent.java b/app/src/main/java/eu/kanade/mangafeed/events/SourceChapterEvent.java index 9c9ed7560..c9fcf1c6f 100644 --- a/app/src/main/java/eu/kanade/mangafeed/events/SourceChapterEvent.java +++ b/app/src/main/java/eu/kanade/mangafeed/events/SourceChapterEvent.java @@ -1,7 +1,7 @@ package eu.kanade.mangafeed.events; import eu.kanade.mangafeed.data.models.Chapter; -import eu.kanade.mangafeed.sources.Source; +import eu.kanade.mangafeed.sources.base.Source; public class SourceChapterEvent { diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java index 3fcb8aa8e..7c0b96187 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java @@ -13,7 +13,7 @@ import javax.inject.Inject; import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.helpers.SourceManager; import eu.kanade.mangafeed.data.models.Manga; -import eu.kanade.mangafeed.sources.Source; +import eu.kanade.mangafeed.sources.base.Source; import eu.kanade.mangafeed.ui.fragment.CatalogueFragment; import eu.kanade.mangafeed.util.PageBundle; import eu.kanade.mangafeed.util.RxPager; diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java index 348d64379..3e676d560 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java @@ -2,8 +2,6 @@ package eu.kanade.mangafeed.presenter; import android.os.Bundle; -import eu.kanade.mangafeed.util.PostResult; - import java.util.List; import javax.inject.Inject; @@ -15,9 +13,10 @@ import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.events.ChapterCountEvent; import eu.kanade.mangafeed.events.SourceChapterEvent; -import eu.kanade.mangafeed.sources.Source; +import eu.kanade.mangafeed.sources.base.Source; import eu.kanade.mangafeed.ui.fragment.MangaChaptersFragment; import eu.kanade.mangafeed.util.EventBusHook; +import eu.kanade.mangafeed.util.PostResult; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java index 66cfda77f..1b916e86e 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java @@ -16,7 +16,7 @@ import eu.kanade.mangafeed.data.helpers.PreferencesHelper; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Page; import eu.kanade.mangafeed.events.SourceChapterEvent; -import eu.kanade.mangafeed.sources.Source; +import eu.kanade.mangafeed.sources.base.Source; import eu.kanade.mangafeed.ui.activity.ReaderActivity; import eu.kanade.mangafeed.util.EventBusHook; import icepick.State; diff --git a/app/src/main/java/eu/kanade/mangafeed/sources/Batoto.java b/app/src/main/java/eu/kanade/mangafeed/sources/Batoto.java index 98752c12e..dbb6690df 100644 --- a/app/src/main/java/eu/kanade/mangafeed/sources/Batoto.java +++ b/app/src/main/java/eu/kanade/mangafeed/sources/Batoto.java @@ -19,6 +19,7 @@ import eu.kanade.mangafeed.data.helpers.NetworkHelper; import eu.kanade.mangafeed.data.helpers.SourceManager; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.sources.base.Source; import rx.Observable; public class Batoto extends Source { @@ -114,19 +115,19 @@ public class Batoto extends Source { } @Override - protected String getMangaUrl(String defaultMangaUrl) { + protected String overrideMangaUrl(String defaultMangaUrl) { String mangaId = defaultMangaUrl.substring(defaultMangaUrl.lastIndexOf("r") + 1); return "http://bato.to/comic_pop?id=" + mangaId; } @Override - protected String getChapterPageUrl(String defaultPageUrl) { + protected String overrideChapterPageUrl(String defaultPageUrl) { String id = defaultPageUrl.substring(defaultPageUrl.indexOf("#") + 1); return INITIAL_PAGE_URL + "id=" + id + "&p=1"; } @Override - protected String getRemainingPagesUrl(String defaultPageUrl) { + protected String overrideRemainingPagesUrl(String defaultPageUrl) { int start = defaultPageUrl.indexOf("#") + 1; int end = defaultPageUrl.indexOf("_", start); String id = defaultPageUrl.substring(start, end); diff --git a/app/src/main/java/eu/kanade/mangafeed/sources/MangaHere.java b/app/src/main/java/eu/kanade/mangafeed/sources/MangaHere.java index ef0838a93..f473ed147 100644 --- a/app/src/main/java/eu/kanade/mangafeed/sources/MangaHere.java +++ b/app/src/main/java/eu/kanade/mangafeed/sources/MangaHere.java @@ -18,6 +18,7 @@ import eu.kanade.mangafeed.data.helpers.NetworkHelper; import eu.kanade.mangafeed.data.helpers.SourceManager; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.sources.base.Source; import rx.Observable; public class MangaHere extends Source { @@ -37,6 +38,7 @@ public class MangaHere extends Source { return NAME; } + @Override public int getSourceId() { return SourceManager.MANGAHERE; } @@ -51,6 +53,12 @@ public class MangaHere extends Source { return INITIAL_SEARCH_URL + "name=" + query + "&page=" + page; } + + @Override + public boolean isLoginRequired() { + return false; + } + public Observable> getGenres() { List genres = new ArrayList<>(30); diff --git a/app/src/main/java/eu/kanade/mangafeed/sources/base/BaseSource.java b/app/src/main/java/eu/kanade/mangafeed/sources/base/BaseSource.java new file mode 100644 index 000000000..b17d328d9 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/sources/base/BaseSource.java @@ -0,0 +1,76 @@ +package eu.kanade.mangafeed.sources.base; + +import com.squareup.okhttp.Headers; + +import java.util.List; + +import eu.kanade.mangafeed.data.models.Chapter; +import eu.kanade.mangafeed.data.models.Manga; + +public abstract class BaseSource { + + // Name of the source to display + public abstract String getName(); + + // Id of the source (must be declared and obtained from SourceManager to avoid conflicts) + public abstract int getSourceId(); + + // True if the source requires a login + public abstract boolean isLoginRequired(); + + // Given a page number, it should return the URL of the page where the manga list is found + protected abstract String getUrlFromPageNumber(int page); + + // From the URL obtained before, this method must return a list of mangas + protected abstract List parsePopularMangasFromHtml(String unparsedHtml); + + // Given a query and a page number, return the URL of the results + protected abstract String getSearchUrl(String query, int page); + + // From the URL obtained before, this method must return a list of mangas + protected abstract List parseSearchFromHtml(String unparsedHtml); + + // Given the URL of a manga and the result of the request, return the details of the manga + protected abstract Manga parseHtmlToManga(String mangaUrl, String unparsedHtml); + + // Given the result of the request to mangas' chapters, return a list of chapters + protected abstract List parseHtmlToChapters(String unparsedHtml); + + // Given the result of the request to a chapter, return the list of URLs of the chapter + protected abstract List parseHtmlToPageUrls(String unparsedHtml); + + // Given the result of the request to a chapter's page, return the URL of the image of the page + protected abstract String parseHtmlToImageUrl(String unparsedHtml); + + + + // Default fields, they can be overriden by sources' implementation + + // Get the URL to the details of a manga, useful if the source provides some kind of API or fast calls + protected String overrideMangaUrl(String defaultMangaUrl) { + return defaultMangaUrl; + } + + // Get the URL of the first page that contains a source image and the page list + protected String overrideChapterPageUrl(String defaultPageUrl) { + return defaultPageUrl; + } + + // Get the URL of the remaining pages that contains source images + protected String overrideRemainingPagesUrl(String defaultPageUrl) { + return defaultPageUrl; + } + + // Default headers, it can be overriden by children or just add new keys + protected Headers.Builder headersBuilder() { + Headers.Builder builder = new Headers.Builder(); + builder.add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64)"); + return builder; + } + + // Number of images to download at the same time. 3 by default + protected int overrideNumberOfConcurrentPageDownloads() { + return 3; + } + +} diff --git a/app/src/main/java/eu/kanade/mangafeed/sources/Source.java b/app/src/main/java/eu/kanade/mangafeed/sources/base/Source.java similarity index 63% rename from app/src/main/java/eu/kanade/mangafeed/sources/Source.java rename to app/src/main/java/eu/kanade/mangafeed/sources/base/Source.java index 2204eaf11..79ba9af11 100644 --- a/app/src/main/java/eu/kanade/mangafeed/sources/Source.java +++ b/app/src/main/java/eu/kanade/mangafeed/sources/base/Source.java @@ -1,4 +1,4 @@ -package eu.kanade.mangafeed.sources; +package eu.kanade.mangafeed.sources.base; import com.squareup.okhttp.Headers; @@ -14,59 +14,7 @@ import eu.kanade.mangafeed.data.models.Page; import rx.Observable; import rx.schedulers.Schedulers; -public abstract class Source { - - // Methods to implement or optionally override - - // Name of the source to display - public abstract String getName(); - - // Id of the source (must be declared and obtained from SourceManager to avoid conflicts) - public abstract int getSourceId(); - - protected abstract String getUrlFromPageNumber(int page); - protected abstract String getSearchUrl(String query, int page); - protected abstract List parsePopularMangasFromHtml(String unparsedHtml); - protected abstract List parseSearchFromHtml(String unparsedHtml); - protected abstract Manga parseHtmlToManga(String mangaUrl, String unparsedHtml); - protected abstract List parseHtmlToChapters(String unparsedHtml); - protected abstract List parseHtmlToPageUrls(String unparsedHtml); - protected abstract String parseHtmlToImageUrl(String unparsedHtml); - - // True if the source requires a login - public boolean isLoginRequired() { - return false; - } - - // Get the URL to the details of a manga, useful if the source provides some kind of API or fast calls - protected String getMangaUrl(String defaultMangaUrl) { - return defaultMangaUrl; - } - - // Get the URL of the first page that contains a source image and the page list - protected String getChapterPageUrl(String defaultPageUrl) { - return defaultPageUrl; - } - - // Get the URL of the remaining pages that contains source images - protected String getRemainingPagesUrl(String defaultPageUrl) { - return defaultPageUrl; - } - - // Default headers, it can be overriden by children or just add new keys - protected Headers.Builder headersBuilder() { - Headers.Builder builder = new Headers.Builder(); - builder.add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64)"); - return builder; - } - - // Number of images to download at the same time - protected int getNumberOfConcurrentPageDownloads() { - return 3; - } - - - // ***** Source class implementation ***** +public abstract class Source extends BaseSource { protected NetworkHelper mNetworkService; protected CacheManager mCacheManager; @@ -96,7 +44,7 @@ public abstract class Source { // Get manga details from the source public Observable pullMangaFromNetwork(final String mangaUrl) { return mNetworkService - .getStringResponse(getMangaUrl(mangaUrl), mNetworkService.NULL_CACHE_CONTROL, mRequestHeaders) + .getStringResponse(overrideMangaUrl(mangaUrl), mNetworkService.NULL_CACHE_CONTROL, mRequestHeaders) .flatMap(unparsedHtml -> Observable.just(parseHtmlToManga(mangaUrl, unparsedHtml))); } @@ -112,7 +60,7 @@ public abstract class Source { return mCacheManager.getPageUrlsFromDiskCache(chapterUrl) .onErrorResumeNext(throwable -> { return mNetworkService - .getStringResponse(getChapterPageUrl(chapterUrl), mNetworkService.NULL_CACHE_CONTROL, mRequestHeaders) + .getStringResponse(overrideChapterPageUrl(chapterUrl), mNetworkService.NULL_CACHE_CONTROL, mRequestHeaders) .flatMap(unparsedHtml -> { List pageUrls = parseHtmlToPageUrls(unparsedHtml); return Observable.just(getFirstImageFromPageUrls(pageUrls, unparsedHtml)); @@ -126,7 +74,7 @@ public abstract class Source { public Observable getRemainingImageUrlsFromPageList(final List pages) { return Observable.from(pages) .filter(page -> page.getImageUrl() == null) - .window(getNumberOfConcurrentPageDownloads()) + .window(overrideNumberOfConcurrentPageDownloads()) .concatMap(batchedPages -> batchedPages.concatMap(this::getImageUrlFromPage) ); @@ -134,7 +82,7 @@ public abstract class Source { private Observable getImageUrlFromPage(final Page page) { return mNetworkService - .getStringResponse(getRemainingPagesUrl(page.getUrl()), mNetworkService.NULL_CACHE_CONTROL, mRequestHeaders) + .getStringResponse(overrideRemainingPagesUrl(page.getUrl()), mNetworkService.NULL_CACHE_CONTROL, mRequestHeaders) .flatMap(unparsedHtml -> Observable.just(parseHtmlToImageUrl(unparsedHtml))) .flatMap(imageUrl -> { page.setImageUrl(imageUrl); diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/SourceHolder.java b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/SourceHolder.java index 8a584f5c8..4bdf927fe 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/SourceHolder.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/SourceHolder.java @@ -4,7 +4,7 @@ import android.view.View; import android.widget.TextView; import eu.kanade.mangafeed.R; -import eu.kanade.mangafeed.sources.Source; +import eu.kanade.mangafeed.sources.base.Source; import uk.co.ribot.easyadapter.ItemViewHolder; import uk.co.ribot.easyadapter.PositionInfo; import uk.co.ribot.easyadapter.annotations.LayoutId; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsAccountsFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsAccountsFragment.java index bf0739ac5..7025bbce1 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsAccountsFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsAccountsFragment.java @@ -22,7 +22,7 @@ import eu.kanade.mangafeed.App; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.helpers.PreferencesHelper; import eu.kanade.mangafeed.data.helpers.SourceManager; -import eu.kanade.mangafeed.sources.Source; +import eu.kanade.mangafeed.sources.base.Source; import eu.kanade.mangafeed.ui.activity.base.BaseActivity; import rx.Observable; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java index fab6b3d6b..96643519f 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java @@ -13,7 +13,7 @@ import butterknife.ButterKnife; import butterknife.OnItemClick; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.presenter.SourcePresenter; -import eu.kanade.mangafeed.sources.Source; +import eu.kanade.mangafeed.sources.base.Source; import eu.kanade.mangafeed.ui.activity.MainActivity; import eu.kanade.mangafeed.ui.adapter.SourceHolder; import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment; diff --git a/app/src/test/java/eu/kanade/mangafeed/BatotoTest.java b/app/src/test/java/eu/kanade/mangafeed/BatotoTest.java index 215627e70..669e1afde 100644 --- a/app/src/test/java/eu/kanade/mangafeed/BatotoTest.java +++ b/app/src/test/java/eu/kanade/mangafeed/BatotoTest.java @@ -18,7 +18,7 @@ import eu.kanade.mangafeed.data.helpers.NetworkHelper; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.sources.Batoto; -import eu.kanade.mangafeed.sources.Source; +import eu.kanade.mangafeed.sources.base.Source; @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) @RunWith(RobolectricGradleTestRunner.class) diff --git a/app/src/test/java/eu/kanade/mangafeed/MangahereTest.java b/app/src/test/java/eu/kanade/mangafeed/MangahereTest.java index 3d6097075..49b06c7d8 100644 --- a/app/src/test/java/eu/kanade/mangafeed/MangahereTest.java +++ b/app/src/test/java/eu/kanade/mangafeed/MangahereTest.java @@ -18,7 +18,7 @@ import eu.kanade.mangafeed.data.helpers.NetworkHelper; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.sources.MangaHere; -import eu.kanade.mangafeed.sources.Source; +import eu.kanade.mangafeed.sources.base.Source; @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) @RunWith(RobolectricGradleTestRunner.class)