mirror of
https://github.com/mihonapp/mihon.git
synced 2025-06-28 03:57:50 +02:00
Migrate to Tachiyomi 6.1
Rewrite batch add UI Rewrite lock UI
This commit is contained in:
@ -56,68 +56,75 @@ class GalleryAdder {
|
||||
val outJson = JsonParser().parse(networkHelper.client.newCall(Request.Builder()
|
||||
.url(API_BASE)
|
||||
.post(RequestBody.create(JSON, json.toString()))
|
||||
.build()).execute().body().string()).obj
|
||||
.build()).execute().body()!!.string()).obj
|
||||
|
||||
val obj = outJson["tokenlist"].array.first()
|
||||
return "${uri.scheme}://${uri.host}/g/${obj["gid"].int}/${obj["token"].string}/"
|
||||
}
|
||||
|
||||
fun addGallery(url: String, fav: Boolean = false): Manga {
|
||||
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
|
||||
else -> throw MalformedURLException("Not a valid gallery URL!")
|
||||
}
|
||||
|
||||
val realUrl = when (urlObj.pathSegments.first().toLowerCase()) {
|
||||
"g" -> {
|
||||
//Is already gallery page, do nothing
|
||||
url
|
||||
}
|
||||
"s" -> {
|
||||
//Is page, fetch gallery token and use that
|
||||
getGalleryUrlFromPage(url)
|
||||
}
|
||||
else -> {
|
||||
throw MalformedURLException("Not a valid gallery URL!")
|
||||
}
|
||||
}
|
||||
|
||||
val sourceObj = sourceManager.get(source)
|
||||
?: throw IllegalStateException("Could not find EH source!")
|
||||
|
||||
val pathOnlyUrl = getUrlWithoutDomain(realUrl)
|
||||
|
||||
//Use manga in DB if possible, otherwise, make a new manga
|
||||
val manga = db.getManga(pathOnlyUrl, source).executeAsBlocking()
|
||||
?: Manga.create(source).apply {
|
||||
this.url = pathOnlyUrl
|
||||
title = realUrl
|
||||
}
|
||||
|
||||
//Copy basics
|
||||
manga.copyFrom(sourceObj.fetchMangaDetails(manga).toBlocking().first())
|
||||
|
||||
//Apply metadata
|
||||
metadataHelper.fetchEhMetadata(realUrl, isExSource(source))?.copyTo(manga)
|
||||
|
||||
if(fav) manga.favorite = true
|
||||
|
||||
db.insertManga(manga).executeAsBlocking().insertedId()?.let {
|
||||
manga.id = it
|
||||
}
|
||||
|
||||
//Fetch and copy chapters
|
||||
fun addGallery(url: String, fav: Boolean = false): GalleryAddEvent {
|
||||
try {
|
||||
sourceObj.fetchChapterList(manga).map {
|
||||
syncChaptersWithSource(db, it, manga, sourceObj)
|
||||
}.toBlocking().first()
|
||||
} catch (e: Exception) {
|
||||
Timber.w(e, "Failed to update chapters for gallery: ${manga.title}!")
|
||||
}
|
||||
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
|
||||
else -> return GalleryAddEvent.Fail.UnknownType(url)
|
||||
}
|
||||
|
||||
return manga
|
||||
val realUrl = when (urlObj.pathSegments.first().toLowerCase()) {
|
||||
"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)
|
||||
}
|
||||
}
|
||||
|
||||
val sourceObj = sourceManager.get(source)
|
||||
?: return GalleryAddEvent.Fail.Error(url, "Could not find EH source!")
|
||||
|
||||
val pathOnlyUrl = getUrlWithoutDomain(realUrl)
|
||||
|
||||
//Use manga in DB if possible, otherwise, make a new manga
|
||||
val manga = db.getManga(pathOnlyUrl, source).executeAsBlocking()
|
||||
?: Manga.create(source).apply {
|
||||
this.url = pathOnlyUrl
|
||||
title = realUrl
|
||||
}
|
||||
|
||||
//Copy basics
|
||||
manga.copyFrom(sourceObj.fetchMangaDetails(manga).toBlocking().first())
|
||||
|
||||
//Apply metadata
|
||||
metadataHelper.fetchEhMetadata(realUrl, isExSource(source))?.copyTo(manga)
|
||||
|
||||
if (fav) manga.favorite = true
|
||||
|
||||
db.insertManga(manga).executeAsBlocking().insertedId()?.let {
|
||||
manga.id = it
|
||||
}
|
||||
|
||||
//Fetch and copy chapters
|
||||
try {
|
||||
sourceObj.fetchChapterList(manga).map {
|
||||
syncChaptersWithSource(db, it, manga, sourceObj)
|
||||
}.toBlocking().first()
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Failed to update chapters for gallery: ${manga.title}!")
|
||||
return GalleryAddEvent.Fail.Error(url, "Failed to update chapters for gallery: $url")
|
||||
}
|
||||
|
||||
return GalleryAddEvent.Success(url, manga)
|
||||
} catch(e: Exception) {
|
||||
Timber.e(e, "Could not add gallery!")
|
||||
return GalleryAddEvent.Fail.Error(url,
|
||||
((e.message ?: "Unknown error!") + " (Gallery: $url)").trim())
|
||||
}
|
||||
}
|
||||
|
||||
private fun getUrlWithoutDomain(orig: String): String {
|
||||
@ -133,4 +140,28 @@ class GalleryAdder {
|
||||
return orig
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sealed class GalleryAddEvent {
|
||||
abstract val logMessage: String
|
||||
abstract val galleryUrl: String
|
||||
open val galleryTitle: String? = null
|
||||
|
||||
class Success(override val galleryUrl: String,
|
||||
val manga: Manga): GalleryAddEvent() {
|
||||
override val logMessage = "[OK] Added gallery: $galleryTitle"
|
||||
override val galleryTitle: String
|
||||
get() = manga.title
|
||||
}
|
||||
|
||||
sealed class Fail: GalleryAddEvent() {
|
||||
class UnknownType(override val galleryUrl: String): Fail() {
|
||||
override val logMessage = "[ERROR] Unknown gallery type for gallery: $galleryUrl"
|
||||
}
|
||||
|
||||
class Error(override val galleryUrl: String,
|
||||
val message: String): Fail() {
|
||||
override val logMessage = "[ERROR] $message"
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user