mirror of
https://github.com/mihonapp/mihon.git
synced 2025-05-03 09:36:31 +02:00
Add the ability to import galleries by specifying the URL of one of it's pages
This commit is contained in:
parent
0a300822a4
commit
029f159aea
@ -1,17 +1,25 @@
|
|||||||
package exh
|
package exh
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import com.github.salomonbrys.kotson.*
|
||||||
|
import com.google.gson.JsonArray
|
||||||
|
import com.google.gson.JsonObject
|
||||||
|
import com.google.gson.JsonParser
|
||||||
import eu.kanade.tachiyomi.data.database.DatabaseHelper
|
import eu.kanade.tachiyomi.data.database.DatabaseHelper
|
||||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||||
|
import eu.kanade.tachiyomi.network.NetworkHelper
|
||||||
import eu.kanade.tachiyomi.source.SourceManager
|
import eu.kanade.tachiyomi.source.SourceManager
|
||||||
import eu.kanade.tachiyomi.util.syncChaptersWithSource
|
import eu.kanade.tachiyomi.util.syncChaptersWithSource
|
||||||
import exh.metadata.MetadataHelper
|
import exh.metadata.MetadataHelper
|
||||||
import exh.metadata.copyTo
|
import exh.metadata.copyTo
|
||||||
|
import okhttp3.MediaType
|
||||||
|
import okhttp3.Request
|
||||||
|
import okhttp3.RequestBody
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import uy.kohesive.injekt.injectLazy
|
import uy.kohesive.injekt.injectLazy
|
||||||
import java.net.MalformedURLException
|
import java.net.MalformedURLException
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
import java.net.URISyntaxException
|
import java.net.URISyntaxException
|
||||||
import java.net.URL
|
|
||||||
|
|
||||||
class GalleryAdder {
|
class GalleryAdder {
|
||||||
|
|
||||||
@ -21,30 +29,78 @@ class GalleryAdder {
|
|||||||
|
|
||||||
private val metadataHelper = MetadataHelper()
|
private val metadataHelper = MetadataHelper()
|
||||||
|
|
||||||
|
private val networkHelper: NetworkHelper by injectLazy()
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val API_BASE = "https://api.e-hentai.org/api.php"
|
||||||
|
val JSON = MediaType.parse("application/json; charset=utf-8")!!
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getGalleryUrlFromPage(url: String): String {
|
||||||
|
val uri = Uri.parse(url)
|
||||||
|
val lastSplit = uri.pathSegments.last().split("-")
|
||||||
|
val pageNum = lastSplit.last()
|
||||||
|
val gallery = lastSplit.first()
|
||||||
|
val pageToken = uri.pathSegments.elementAt(1)
|
||||||
|
|
||||||
|
val json = JsonObject()
|
||||||
|
json["method"] = "gtoken"
|
||||||
|
json["pagelist"] = JsonArray().apply {
|
||||||
|
add(JsonArray().apply {
|
||||||
|
add(gallery.toInt())
|
||||||
|
add(pageToken)
|
||||||
|
add(pageNum.toInt())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
val outJson = JsonParser().parse(networkHelper.client.newCall(Request.Builder()
|
||||||
|
.url(API_BASE)
|
||||||
|
.post(RequestBody.create(JSON, json.toString()))
|
||||||
|
.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 {
|
fun addGallery(url: String, fav: Boolean = false): Manga {
|
||||||
val source = when(URL(url).host) {
|
val urlObj = Uri.parse(url)
|
||||||
|
val source = when(urlObj.host) {
|
||||||
"g.e-hentai.org", "e-hentai.org" -> EH_SOURCE_ID
|
"g.e-hentai.org", "e-hentai.org" -> EH_SOURCE_ID
|
||||||
"exhentai.org" -> EXH_SOURCE_ID
|
"exhentai.org" -> EXH_SOURCE_ID
|
||||||
else -> throw MalformedURLException("Not a valid gallery URL!")
|
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)
|
val sourceObj = sourceManager.get(source)
|
||||||
?: throw IllegalStateException("Could not find EH source!")
|
?: throw IllegalStateException("Could not find EH source!")
|
||||||
|
|
||||||
val pathOnlyUrl = getUrlWithoutDomain(url)
|
val pathOnlyUrl = getUrlWithoutDomain(realUrl)
|
||||||
|
|
||||||
//Use manga in DB if possible, otherwise, make a new manga
|
//Use manga in DB if possible, otherwise, make a new manga
|
||||||
val manga = db.getManga(pathOnlyUrl, source).executeAsBlocking()
|
val manga = db.getManga(pathOnlyUrl, source).executeAsBlocking()
|
||||||
?: Manga.create(source).apply {
|
?: Manga.create(source).apply {
|
||||||
this.url = pathOnlyUrl
|
this.url = pathOnlyUrl
|
||||||
title = url
|
title = realUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
//Copy basics
|
//Copy basics
|
||||||
manga.copyFrom(sourceObj.fetchMangaDetails(manga).toBlocking().first())
|
manga.copyFrom(sourceObj.fetchMangaDetails(manga).toBlocking().first())
|
||||||
|
|
||||||
//Apply metadata
|
//Apply metadata
|
||||||
metadataHelper.fetchEhMetadata(url, isExSource(source))?.copyTo(manga)
|
metadataHelper.fetchEhMetadata(realUrl, isExSource(source))?.copyTo(manga)
|
||||||
|
|
||||||
if(fav) manga.favorite = true
|
if(fav) manga.favorite = true
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:text="Enter the galleries to add (separated by a new line):"
|
android:text="Enter the galleries to add (E-Hentai/ExHentai only, separated by a new line):"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:id="@+id/textView"
|
android:id="@+id/textView"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user