Insert metadata in sync and when importing galleries

This commit is contained in:
NerdNumber9 2019-04-14 09:09:24 -04:00
parent 5e4b9fb15b
commit 4d15ac2fa3

View File

@ -1,6 +1,7 @@
package exh
import android.net.Uri
import com.elvishew.xlog.XLog
import com.github.salomonbrys.kotson.*
import com.google.gson.JsonArray
import com.google.gson.JsonObject
@ -14,7 +15,6 @@ import exh.metadata.metadata.EHentaiSearchMetadata
import okhttp3.MediaType
import okhttp3.Request
import okhttp3.RequestBody
import timber.log.Timber
import uy.kohesive.injekt.injectLazy
import java.net.URI
import java.net.URISyntaxException
@ -28,7 +28,7 @@ class GalleryAdder {
private val networkHelper: NetworkHelper by injectLazy()
companion object {
const val API_BASE = "https://api.e-hentai.org/api.php"
const val EH_API_BASE = "https://api.e-hentai.org/api.php"
val JSON = MediaType.parse("application/json; charset=utf-8")!!
}
@ -50,7 +50,7 @@ class GalleryAdder {
}
val outJson = JsonParser().parse(networkHelper.client.newCall(Request.Builder()
.url(API_BASE)
.url(EH_API_BASE)
.post(RequestBody.create(JSON, json.toString()))
.build()).execute().body()!!.string()).obj
@ -61,6 +61,7 @@ class GalleryAdder {
fun addGallery(url: String,
fav: Boolean = false,
forceSource: Long? = null): GalleryAddEvent {
XLog.d("Importing gallery (url: %s, fav: %s, forceSource: %s)...", url, fav, forceSource)
try {
val urlObj = Uri.parse(url)
val lowercasePs = urlObj.pathSegments.map(String::toLowerCase)
@ -86,6 +87,9 @@ class GalleryAdder {
return GalleryAddEvent.Fail.UnknownType(url)
}
val sourceObj = sourceManager.get(source)
?: return GalleryAddEvent.Fail.Error(url, "Source not installed!")
val realUrl = when(source) {
EH_SOURCE_ID, EXH_SOURCE_ID -> when (lcFirstPathSegment) {
"g" -> {
@ -133,9 +137,6 @@ class GalleryAdder {
else -> return GalleryAddEvent.Fail.UnknownType(url)
}
val sourceObj = sourceManager.get(source)
?: return GalleryAddEvent.Fail.Error(url, "Could not find EH source!")
val cleanedUrl = when(source) {
EH_SOURCE_ID, EXH_SOURCE_ID -> EHentaiSearchMetadata.normalizeUrl(getUrlWithoutDomain(realUrl))
NHENTAI_SOURCE_ID -> getUrlWithoutDomain(realUrl)
@ -154,16 +155,22 @@ class GalleryAdder {
title = realUrl
}
//Copy basics
// Insert created manga if not in DB before fetching details
// This allows us to keep the metadata when fetching details
if(manga.id == null) {
db.insertManga(manga).executeAsBlocking().insertedId()?.let {
manga.id = it
}
}
// Fetch and copy details
val newManga = sourceObj.fetchMangaDetails(manga).toBlocking().first()
manga.copyFrom(newManga)
manga.title = newManga.title //Forcibly copy title as copyFrom does not copy title
if (fav) manga.favorite = true
db.insertManga(manga).executeAsBlocking().insertedId()?.let {
manga.id = it
}
db.insertManga(manga).executeAsBlocking()
//Fetch and copy chapters
try {
@ -171,13 +178,13 @@ class GalleryAdder {
syncChaptersWithSource(db, it, manga, sourceObj)
}.toBlocking().first()
} catch (e: Exception) {
Timber.e(e, "Failed to update chapters for gallery: ${manga.title}!")
XLog.w("Failed to update chapters for gallery: %s!", 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!")
XLog.w("Could not add gallery!", e)
return GalleryAddEvent.Fail.Error(url,
((e.message ?: "Unknown error!") + " (Gallery: $url)").trim())
}