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