Skip to content

Commit 11fdba7

Browse files
authored
Bugfixes (#48)
1 parent b20d197 commit 11fdba7

10 files changed

Lines changed: 48 additions & 28 deletions

File tree

src/main/kotlin/ru/stersh/bookcrawler/core/Notification.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ data class Notification(
44
val id: BookId,
55
val type: MessageType,
66
val title: String,
7-
val coverUrl: String,
7+
val coverUrl: String?,
88
val authors: List<String>,
99
val series: Series?,
1010
val availableActions: List<Action>

src/main/kotlin/ru/stersh/bookcrawler/module/at/Utils.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ fun getDecodedText(userId: String, key: String, text: String): String {
1414
return stringBuilder.toString()
1515
}
1616

17+
private val StopChars = listOf('?', '!', '/', '\\','*', ':', '<', '>', '"', '|', '+')
18+
1719
fun normalizeBookName(source: String): String {
1820
val translator = Translator(Schemas.WIKIPEDIA)
19-
return translator.translate(source)
21+
var newName = translator.translate(source)
22+
for (char in StopChars) {
23+
newName = newName.replace(char.toString(), "")
24+
}
25+
return newName
2026
}

src/main/kotlin/ru/stersh/bookcrawler/module/at/api/At.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,20 @@ object At {
171171
null
172172
}
173173

174-
val coverResponse = client.get(workDetails.coverUrl)
174+
val cover = if (workDetails.coverUrl != null) {
175+
val coverResponse = client.get(workDetails.coverUrl)
175176

176-
val coverType = Book.Image.Type.fromContentType(coverResponse.headers["Content-Type"])
177-
val coverBytes = coverResponse.readBytes()
177+
val coverType = Book.Image.Type.fromContentType(coverResponse.headers["Content-Type"])
178+
val coverBytes = coverResponse.readBytes()
178179

179-
val cover = Book.Image.Raw(
180-
bytes = coverBytes,
181-
type = coverType,
182-
name = "cover"
183-
)
180+
Book.Image.Raw(
181+
bytes = coverBytes,
182+
type = coverType,
183+
name = "cover"
184+
)
185+
} else {
186+
null
187+
}
184188

185189
return Book(
186190
id = BookId(workId, PROVIDER_NAME),

src/main/kotlin/ru/stersh/bookcrawler/module/at/api/Recommendation.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ data class Recommendation(
2121
@SerialName("coAuthorUserName")
2222
val coAuthorUserName: String?,
2323
@SerialName("coverUrl")
24-
val coverUrl: String,
24+
val coverUrl: String?,
2525
@SerialName("discount")
2626
val discount: Float?,
2727
@SerialName("finishTime")
28-
val finishTime: String,
28+
val finishTime: String?,
2929
@SerialName("finished")
3030
val finished: Boolean,
3131
@SerialName("format")

src/main/kotlin/ru/stersh/bookcrawler/module/at/api/Work.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ data class Work(
2929
@SerialName("commentCount")
3030
val commentCount: Long,
3131
@SerialName("coverUrl")
32-
val coverUrl: String,
32+
val coverUrl: String?,
3333
@SerialName("enableRedLine")
3434
val enableRedLine: Boolean,
3535
@SerialName("enableTTS")

src/main/kotlin/ru/stersh/bookcrawler/module/at/api/WorkDetails.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ data class WorkDetails(
4343
@SerialName("commentCount")
4444
val commentCount: Int,
4545
@SerialName("coverUrl")
46-
val coverUrl: String,
46+
val coverUrl: String?,
4747
@SerialName("discount")
4848
val discount: Float?,
4949
@SerialName("downloadErrorCode")
@@ -57,7 +57,7 @@ data class WorkDetails(
5757
@SerialName("finishTime")
5858
val finishTime: String?,
5959
@SerialName("firstSubGenreId")
60-
val firstSubGenreId: Long,
60+
val firstSubGenreId: Long?,
6161
@SerialName("format")
6262
val format: String,
6363
@SerialName("freeChapterCount")
@@ -139,7 +139,7 @@ data class WorkDetails(
139139
@SerialName("seriesTitle")
140140
val seriesTitle: String?,
141141
@SerialName("seriesWorkIds")
142-
val seriesWorkIds: List<Int>,
142+
val seriesWorkIds: List<Int>?,
143143
@SerialName("seriesWorkNumber")
144144
val seriesWorkNumber: Int,
145145
@SerialName("state")

src/main/kotlin/ru/stersh/bookcrawler/module/litnet/LitnetLibraryCheckTask.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class LitnetLibraryCheckTask : TaskManager.Task {
148148
for (localId in localLibraryIds) {
149149
val allSeriesBookResult = runCatching { Litnet.getAllSeriesBooks(localId) }
150150
.onFailure {
151-
logger.warn("[Litnet] Filed to get all series books for $localId")
151+
logger.warn("[Litnet] Filed to get all series books for $localId", it)
152152
}
153153

154154
val allSeriesBookIds = allSeriesBookResult
@@ -189,7 +189,7 @@ class LitnetLibraryCheckTask : TaskManager.Task {
189189
val book = Litnet.getBook(bookId)
190190
BookHandlerManager.onBookCreated(book)
191191
}.onFailure {
192-
logger.warn("[Litnet] Filed to download book $bookId")
192+
logger.warn("[Litnet] Filed to download book $bookId", it)
193193
}
194194
}
195195

src/main/kotlin/ru/stersh/bookcrawler/module/litnet/api/BookDetailsResponse.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import kotlinx.serialization.Serializable
66
@Serializable
77
data class BookDetailsResponse(
88
@SerialName("series")
9-
val series: List<SeriesBook>
9+
val series: List<SeriesBook>?
1010
)

src/main/kotlin/ru/stersh/bookcrawler/module/litnet/api/Litnet.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ object Litnet {
8080
addDefaultParams()
8181
parameter("bookId", bookId)
8282
}
83-
.body<ArrayList<Chapter>>()
83+
.body<List<Chapter>>()
8484
}
8585

8686
suspend fun getChapterTexts(chapterIds: List<Long>): List<ChapterText> {
@@ -91,7 +91,7 @@ object Litnet {
9191
parameter("chapter_ids[]", it)
9292
}
9393
}
94-
.body<ArrayList<ChapterText>>()
94+
.body<List<ChapterText>>()
9595
.map {
9696
it.copy(text = decrypt(it.text))
9797
}
@@ -106,6 +106,7 @@ object Litnet {
106106
}
107107
.body<BookDetailsResponse>()
108108
.series
109+
.orEmpty()
109110
}
110111

111112
suspend fun getBook(bookId: Long): Book {

src/main/kotlin/ru/stersh/bookcrawler/module/telegram/TelegramNotificationHandler.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,22 @@ class TelegramNotificationHandler(
4343
message.append("__${notification.series.title} (${notification.series.order})__")
4444
}
4545

46-
bot.sendPhoto(
47-
chatId = ChatId.fromId(chatId),
48-
photo = TelegramFile.ByUrl(notification.coverUrl),
49-
caption = message.toString(),
50-
parseMode = ParseMode.MARKDOWN,
51-
replyMarkup = createReplyMarkup(notification.id, notification.availableActions)
52-
)
46+
if (notification.coverUrl != null) {
47+
bot.sendPhoto(
48+
chatId = ChatId.fromId(chatId),
49+
photo = TelegramFile.ByUrl(notification.coverUrl),
50+
caption = message.toString(),
51+
parseMode = ParseMode.MARKDOWN,
52+
replyMarkup = createReplyMarkup(notification.id, notification.availableActions)
53+
)
54+
} else {
55+
bot.sendMessage(
56+
chatId = ChatId.fromId(chatId),
57+
text = message.toString(),
58+
parseMode = ParseMode.MARKDOWN,
59+
replyMarkup = createReplyMarkup(notification.id, notification.availableActions)
60+
)
61+
}
5362
}
5463

5564
private fun createReplyMarkup(bookId: BookId, actions: List<Action>): ReplyMarkup? {

0 commit comments

Comments
 (0)