Skip to content

Commit

Permalink
litnet support (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
siper committed Jan 2, 2024
1 parent c2275fe commit 903944d
Show file tree
Hide file tree
Showing 23 changed files with 999 additions and 13 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

## Overview

Bookcrawler - selfhosted tool for monitoring your library on book publishing platforms (like author.today), and deliver updates with Dropbox, telegram and etc.
Bookcrawler - selfhosted tool for monitoring your library on book publishing platforms (like author.today and litnet.com), and deliver updates with Dropbox, telegram and etc.

## Usage

Serve it with Docker:

```Docker
docker run -d \
--name=bookcrawler \
-e PUID=1000 \
-e PGID=1000 \
-e TZ=Etc/UTC \
-v /path/to/data:/config \
--restart unless-stopped \
stersh/bookcrawler-dev:latest
--name=bookcrawler \
-e PUID=1000 \
-e PGID=1000 \
-e TZ=Etc/UTC \
-v /path/to/data:/config \
--restart unless-stopped \
stersh/bookcrawler-dev:latest
```

Or use docker compose:
Expand Down Expand Up @@ -46,6 +46,9 @@ logging.app=INFO # app log level, may be:
logging.db=true # log database transactions (default: false)
at.accessToken=<author.today token here> # author.today access token
at.libraryCheckPeriod=10 # library check interval in minutes (default: 10)
litnet.username=<litnet.com username> # litnet.com username (email or phone)
litnet.password=<litnet.com username> # litnet.com password
litnet.token=<litnet.com access token> # your can set username and password, or token manually
telegram.botToken=<your telegram token here> # telegram bot token (use Bot Father to receive it)
telegram.chatId=<telegram chatId> # telegram chat id (use chat id bot to receive it)
telegram.uploadBookUpdates=true # send book updates to telegram bot (default: false)
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies {
implementation("io.ktor:ktor-client-auth:$ktor_version")
implementation("io.ktor:ktor-client-logging:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
implementation("ch.qos.logback:logback-classic:1.2.9")
implementation('ch.qos.logback:logback-classic:1.4.12')
implementation("com.h2database:h2:2.2.224")
def exposed_version = "0.39.2"
implementation("org.jetbrains.exposed:exposed-core:$exposed_version")
Expand All @@ -46,7 +46,7 @@ dependencies {
}

kotlin {
jvmToolchain(8)
jvmToolchain(18)
}

application {
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
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
2 changes: 2 additions & 0 deletions src/main/kotlin/ru/stersh/bookcrawler/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ru.stersh.bookcrawler
import ru.stersh.bookcrawler.core.ModuleManager
import ru.stersh.bookcrawler.module.at.AtModule
import ru.stersh.bookcrawler.module.dropbox.DropboxModule
import ru.stersh.bookcrawler.module.litnet.LitnetModule
import ru.stersh.bookcrawler.module.local.LocalModule
import ru.stersh.bookcrawler.module.mail.MailModule
import ru.stersh.bookcrawler.module.telegram.TelegramModule
Expand All @@ -17,4 +18,5 @@ fun main(args: Array<String>) {
ModuleManager.addModule(DropboxModule())
ModuleManager.addModule(LocalModule())
ModuleManager.addModule(MailModule())
ModuleManager.addModule(LitnetModule())
}
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"
57 changes: 57 additions & 0 deletions src/main/kotlin/ru/stersh/bookcrawler/module/litnet/Db.kt
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]
)
}
}
}
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)
}
}
}
Loading

0 comments on commit 903944d

Please sign in to comment.