pages api done

This commit is contained in:
Aria Moradi 2021-01-20 00:04:12 +03:30
parent 21c0ea4e90
commit 5d9173d3f7
12 changed files with 128 additions and 33 deletions

View File

@ -1,6 +1,6 @@
package eu.kanade.tachiyomi.source.model
//import android.net.Uri
import android.net.Uri
import eu.kanade.tachiyomi.network.ProgressListener
import rx.subjects.Subject
@ -8,7 +8,7 @@ open class Page(
val index: Int,
val url: String = "",
var imageUrl: String? = null,
// @Transient var uri: Uri? = null // Deprecated but can't be deleted due to extensions
@Transient var uri: Uri? = null // Deprecated but can't be deleted due to extensions
): ProgressListener {
val number: Int

View File

@ -12,7 +12,7 @@ interface SChapter : Serializable {
var chapter_number: Float
var scanlator: String?
var scanlator: String?
fun copyFrom(other: SChapter) {
name = other.name

View File

@ -76,12 +76,15 @@ class Main {
ctx.json(getManga(mangaId))
}
app.get("/api/v1/chapters/:mangaId/") { ctx ->
app.get("/api/v1/manga/:mangaId/chapters") { ctx ->
val mangaId = ctx.pathParam("mangaId").toInt()
ctx.json(getChapterList(mangaId))
}
app.get("/api/v1/chapter/:chapterId") { ctx ->
val chapterId = ctx.pathParam("chapterId").toInt()
ctx.json(getPages(chapterId))
}
}

View File

@ -1,6 +1,7 @@
package ir.armor.tachidesk.database
import ir.armor.tachidesk.Config
import ir.armor.tachidesk.database.table.ChapterTable
import ir.armor.tachidesk.database.table.ExtensionsTable
import ir.armor.tachidesk.database.table.MangaTable
import ir.armor.tachidesk.database.table.SourceTable
@ -22,5 +23,6 @@ fun makeDataBaseTables() {
SchemaUtils.create(ExtensionsTable)
SchemaUtils.create(SourceTable)
SchemaUtils.create(MangaTable)
SchemaUtils.create(ChapterTable)
}
}

View File

@ -1,9 +1,11 @@
package ir.armor.tachidesk.database.dataclass
data class ChapterDataClass(
val id: Int,
val url: String,
val name: String,
val date_upload: String,
val date_upload: Long,
val chapter_number: Float,
val scanlator: String?,
val mangaId: Int,
)

View File

@ -0,0 +1,6 @@
package ir.armor.tachidesk.database.dataclass
data class PageDataClass(
val index: Int,
var imageUrl: String,
)

View File

@ -0,0 +1,14 @@
package ir.armor.tachidesk.database.table
import eu.kanade.tachiyomi.source.model.SManga
import org.jetbrains.exposed.dao.id.IntIdTable
object ChapterTable : IntIdTable() {
val url = varchar("url", 2048)
val name = varchar("name", 512)
val date_upload = long("date_upload").default(0)
val chapter_number = float("chapter_number").default(-1f)
val scanlator = varchar("scanlator",128).nullable()
val manga = reference("manga", MangaTable)
}

View File

@ -0,0 +1,87 @@
package ir.armor.tachidesk.util
import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.online.HttpSource
import ir.armor.tachidesk.database.dataclass.ChapterDataClass
import ir.armor.tachidesk.database.dataclass.PageDataClass
import ir.armor.tachidesk.database.entity.MangaEntity
import ir.armor.tachidesk.database.table.ChapterTable
import ir.armor.tachidesk.database.table.MangaTable
import org.jetbrains.exposed.sql.insertAndGetId
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
fun getChapterList(mangaId: Int): List<ChapterDataClass> {
val mangaDetails = getManga(mangaId)
val source = getHttpSource(mangaDetails.sourceId)
val chapterList = source.fetchChapterList(
SManga.create().apply {
title = mangaDetails.title
url = mangaDetails.url
}
).toBlocking().first()
return transaction {
chapterList.forEach { fetchedChapter ->
val chapterEntry = ChapterTable.select { ChapterTable.url eq fetchedChapter.url }.firstOrNull()
if (chapterEntry == null) {
ChapterTable.insertAndGetId {
it[url] = fetchedChapter.url
it[name] = fetchedChapter.name
it[date_upload] = fetchedChapter.date_upload
it[chapter_number] = fetchedChapter.chapter_number
it[scanlator] = fetchedChapter.scanlator
it[manga] = mangaId
}
}
}
return@transaction chapterList.map {
ChapterDataClass(
ChapterTable.select { ChapterTable.url eq it.url }.firstOrNull()!![ChapterTable.id].value,
it.url,
it.name,
it.date_upload,
it.chapter_number,
it.scanlator,
mangaId
)
}
}
}
fun getPages(chapterId: Int): List<PageDataClass> {
return transaction {
val chapterEntry = ChapterTable.select { ChapterTable.id eq chapterId }.firstOrNull()!!
val mangaId = chapterEntry[ChapterTable.manga].value
val mangaEntry = MangaTable.select { MangaTable.id eq mangaId }.firstOrNull()!!
val source = getHttpSource(mangaEntry[MangaTable.sourceReference].value)
val pagesList = source.fetchPageList(
SChapter.create().apply {
url = chapterEntry[ChapterTable.url]
name = chapterEntry[ChapterTable.name]
}
).toBlocking().first()
return@transaction pagesList.map {
PageDataClass(
it.index,
getTrueImageUrl(it,source)
)
}
}
}
fun getTrueImageUrl(page: Page, source: HttpSource): String {
return if ( page.imageUrl == null){
source.fetchImageUrl(page).toBlocking().first()!!
} else page.imageUrl!!
}

View File

@ -1,7 +1,6 @@
package ir.armor.tachidesk.util
import eu.kanade.tachiyomi.source.model.SManga
import ir.armor.tachidesk.database.dataclass.ChapterDataClass
import ir.armor.tachidesk.database.dataclass.MangaDataClass
import ir.armor.tachidesk.database.table.MangaStatus
import ir.armor.tachidesk.database.table.MangaTable
@ -77,24 +76,3 @@ fun getManga(mangaId: Int): MangaDataClass {
}
}
fun getChapterList(mangaId: Int): List<ChapterDataClass> {
val mangaDetails = getManga(mangaId)
val source = getHttpSource(mangaDetails.sourceId)
val chapterList = source.fetchChapterList(
SManga.create().apply {
title = mangaDetails.title
url = mangaDetails.url
}
).toBlocking().first()
return chapterList.map {
ChapterDataClass(
it.url,
it.name,
it.date_upload.toString(),
it.chapter_number,
it.scanlator,
)
}
}

View File

@ -39,6 +39,8 @@ export default function ChapterCard(props: IProps) {
const classes = useStyles();
const { chapter } = props;
const dateStr = chapter.date_upload && new Date(chapter.date_upload).toISOString().slice(0, 10);
return (
<>
<li>
@ -53,13 +55,12 @@ export default function ChapterCard(props: IProps) {
<Typography variant="caption" display="block" gutterBottom>
{chapter.scanlator}
{chapter.scanlator && ' '}
{chapter.date_upload
&& new Date(chapter.date_upload).toISOString().slice(0, 10)}
{dateStr}
</Typography>
</div>
</div>
<div style={{ display: 'flex' }}>
<Button variant="outlined" style={{ marginLeft: 20 }} onClick={() => { /* window.location.href = 'sources/popular/'; */ }}>open</Button>
<Button variant="outlined" style={{ marginLeft: 20 }} onClick={() => { window.location.href = `http://127.0.0.1:4567/api/v1/chapter/${chapter.id}`; }}>open</Button>
</div>
</CardContent>
</Card>

View File

@ -16,7 +16,7 @@ export default function Manga() {
}, []);
useEffect(() => {
fetch(`http://127.0.0.1:4567/api/v1/chapters/${id}/`)
fetch(`http://127.0.0.1:4567/api/v1/manga/${id}/chapters`)
.then((response) => response.json())
.then((data) => setChapters(data));
}, []);

View File

@ -23,9 +23,11 @@ interface IManga {
}
interface IChapter {
id: number
url: string
name: string
date_upload: string
date_upload: number
chapter_number: number
scanlator: String
mangaId: number
}