Add nhentai URL importing

Allow fast importing of single URLs by inputting the URL into the source's search bar
This commit is contained in:
NerdNumber9
2017-08-24 18:31:08 -04:00
parent dcb6ae44dd
commit 239b36c31a
5 changed files with 85 additions and 17 deletions

View File

@ -62,38 +62,56 @@ class GalleryAdder {
return "${uri.scheme}://${uri.host}/g/${obj["gid"].int}/${obj["token"].string}/"
}
fun addGallery(url: String, fav: Boolean = false): GalleryAddEvent {
fun addGallery(url: String,
fav: Boolean = false,
forceSource: Long? = null): GalleryAddEvent {
try {
val urlObj = Uri.parse(url)
val source = when (urlObj.host) {
"g.e-hentai.org", "e-hentai.org" -> EH_SOURCE_ID
"exhentai.org" -> EXH_SOURCE_ID
"nhentai.net" -> NHENTAI_SOURCE_ID
else -> return GalleryAddEvent.Fail.UnknownType(url)
}
val realUrl = when (urlObj.pathSegments.first().toLowerCase()) {
"g" -> {
//Is already gallery page, do nothing
url
if(forceSource != null && source != forceSource) {
return GalleryAddEvent.Fail.UnknownType(url)
}
val firstPathSegment = urlObj.pathSegments.firstOrNull()?.toLowerCase()
val realUrl = when(source) {
EH_SOURCE_ID, EXH_SOURCE_ID -> when (firstPathSegment) {
"g" -> {
//Is already gallery page, do nothing
url
}
"s" -> {
//Is page, fetch gallery token and use that
getGalleryUrlFromPage(url)
}
else -> return GalleryAddEvent.Fail.UnknownType(url)
}
"s" -> {
//Is page, fetch gallery token and use that
getGalleryUrlFromPage(url)
}
else -> {
return GalleryAddEvent.Fail.UnknownType(url)
NHENTAI_SOURCE_ID -> when {
firstPathSegment == "g" -> url
urlObj.pathSegments.size >= 3 -> "https://nhentai.net/g/${urlObj.pathSegments[1]}/"
else -> return GalleryAddEvent.Fail.UnknownType(url)
}
else -> return GalleryAddEvent.Fail.UnknownType(url)
}
val sourceObj = sourceManager.get(source)
?: return GalleryAddEvent.Fail.Error(url, "Could not find EH source!")
val pathOnlyUrl = getUrlWithoutDomain(realUrl)
val cleanedUrl = when(source) {
EH_SOURCE_ID, EXH_SOURCE_ID -> getUrlWithoutDomain(realUrl)
NHENTAI_SOURCE_ID -> realUrl //nhentai uses URLs directly (oops, my bad when implementing this source)
else -> return GalleryAddEvent.Fail.UnknownType(url)
}
//Use manga in DB if possible, otherwise, make a new manga
val manga = db.getManga(pathOnlyUrl, source).executeAsBlocking()
val manga = db.getManga(cleanedUrl, source).executeAsBlocking()
?: Manga.create(source).apply {
this.url = pathOnlyUrl
this.url = cleanedUrl
title = realUrl
}
@ -101,7 +119,12 @@ class GalleryAdder {
manga.copyFrom(sourceObj.fetchMangaDetails(manga).toBlocking().first())
//Apply metadata
metadataHelper.fetchEhMetadata(realUrl, isExSource(source))?.copyTo(manga)
when(source) {
EH_SOURCE_ID, EXH_SOURCE_ID ->
metadataHelper.fetchEhMetadata(realUrl, isExSource(source))?.copyTo(manga)
NHENTAI_SOURCE_ID ->
metadataHelper.fetchNhentaiMetadata(realUrl)?.copyTo(manga)
}
if (fav) manga.favorite = true

View File

@ -11,7 +11,7 @@ class NHentaiMetadata : SearchableGalleryMetadata() {
var url get() = id?.let { "$BASE_URL/g/$it" }
set(a) {
a?.let {
id = a.split("/").last().toLong()
id = a.split("/").last { it.isNotBlank() }.toLong()
}
}

View File

@ -0,0 +1,28 @@
package exh.util
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.model.MangasPage
import exh.GalleryAddEvent
import exh.GalleryAdder
import rx.Observable
private val galleryAdder by lazy {
GalleryAdder()
}
/**
* A version of fetchSearchManga that supports URL importing
*/
fun Source.urlImportFetchSearchManga(query: String, fail: () -> Observable<MangasPage>) =
when {
query.startsWith("http://") || query.startsWith("https://") -> {
Observable.fromCallable {
val res = galleryAdder.addGallery(query, false, id)
MangasPage((if(res is GalleryAddEvent.Success)
listOf(res.manga)
else
emptyList()), false)
}
}
else -> fail()
}