-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
999 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/ru/stersh/bookcrawler/module/litnet/Constants.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package ru.stersh.bookcrawler.module.litnet | ||
|
||
val LITNET_PROVIDER_NAME = "litnet" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package ru.stersh.bookcrawler.module.litnet | ||
|
||
import org.jetbrains.exposed.sql.ResultRow | ||
import org.jetbrains.exposed.sql.Table | ||
import java.time.ZonedDateTime | ||
import java.time.format.DateTimeFormatter | ||
|
||
|
||
object LitnetBookDb : Table() { | ||
val id = long("id") | ||
val title = varchar("title", 1024) | ||
val author = varchar("author", 1024) | ||
val inSeries = bool("inSeries").default(false) | ||
val authorId = long("authorId") | ||
val completed = bool("completed").default(true) | ||
val read = bool("read").default(false) | ||
val inLibrary = bool("inLibrary").default(false) | ||
val purchased = bool("purchased").default(false) | ||
val coverUrl = varchar("coverPath", 1024).nullable() | ||
val updatedAt = varchar("updatedAt", 1024) | ||
val lastModificationTime = long("lastModificationTime") | ||
override val primaryKey = PrimaryKey(id) | ||
} | ||
|
||
data class LitnetBook( | ||
val id: Long, | ||
val title: String, | ||
val author: String, | ||
val inSeries: Boolean, | ||
val coverUrl: String?, | ||
val authorId: Long, | ||
val completed: Boolean, | ||
val inLibrary: Boolean, | ||
val purchased: Boolean, | ||
val read: Boolean, | ||
val updatedAt: ZonedDateTime, | ||
val lastModificationTime: Long, | ||
) { | ||
companion object { | ||
fun fromRow(row: ResultRow): LitnetBook { | ||
return LitnetBook( | ||
id = row[LitnetBookDb.id], | ||
title = row[LitnetBookDb.title], | ||
authorId = row[LitnetBookDb.authorId], | ||
inSeries = row[LitnetBookDb.inSeries], | ||
author = row[LitnetBookDb.author], | ||
completed = row[LitnetBookDb.completed], | ||
inLibrary = row[LitnetBookDb.inLibrary], | ||
purchased = row[LitnetBookDb.purchased], | ||
read = row[LitnetBookDb.read], | ||
coverUrl = row[LitnetBookDb.coverUrl], | ||
updatedAt = ZonedDateTime.from(DateTimeFormatter.ISO_ZONED_DATE_TIME.parse(row[LitnetBookDb.updatedAt])), | ||
lastModificationTime = row[LitnetBookDb.lastModificationTime] | ||
) | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/kotlin/ru/stersh/bookcrawler/module/litnet/LitnetActionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package ru.stersh.bookcrawler.module.litnet | ||
|
||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import org.jetbrains.exposed.sql.update | ||
import ru.stersh.bookcrawler.core.Action | ||
import ru.stersh.bookcrawler.core.ActionsManager | ||
import ru.stersh.bookcrawler.core.BookHandlerManager | ||
import ru.stersh.bookcrawler.core.BookId | ||
import ru.stersh.bookcrawler.logger | ||
import ru.stersh.bookcrawler.module.litnet.api.Litnet | ||
|
||
class LitnetActionHandler : ActionsManager.ActionHandler { | ||
override val name: String = LITNET_PROVIDER_NAME | ||
|
||
override suspend fun invokeAction(bookId: BookId, action: Action) { | ||
if (bookId.provider != name) return | ||
when(action) { | ||
Action.ADD_TO_LIBRARY -> addToLibrary(bookId) | ||
Action.MARK_READ -> markRead(bookId) | ||
} | ||
} | ||
|
||
private suspend fun markRead(bookId: BookId) { | ||
runCatching { | ||
Litnet.markRead(bookId.id) | ||
transaction { | ||
LitnetBookDb.update( | ||
{ LitnetBookDb.id eq bookId.id } | ||
) { | ||
it[read] = true | ||
} | ||
} | ||
}.onFailure { | ||
logger.warn("[Litnet] Filed to mark read book $bookId in library", it) | ||
} | ||
} | ||
|
||
private suspend fun addToLibrary(bookId: BookId) { | ||
runCatching { | ||
Litnet.addToLibrary(bookId.id) | ||
transaction { | ||
LitnetBookDb.update( | ||
{ LitnetBookDb.id eq bookId.id } | ||
) { | ||
it[inLibrary] = true | ||
} | ||
} | ||
val book = Litnet.getBook(bookId.id) | ||
BookHandlerManager.onBookCreated(book) | ||
}.onFailure { | ||
logger.warn("[Litnet] Filed to add book $bookId to library", it) | ||
} | ||
} | ||
} |
Oops, something went wrong.