Skip to content

Commit

Permalink
improved errors logs on social network wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Dec 19, 2024
1 parent c5d98c0 commit cc136cd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
16 changes: 8 additions & 8 deletions src/main/kotlin/fr/shikkanime/wrappers/BskyWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import org.jsoup.Jsoup
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.util.regex.MatchResult
import java.util.regex.Pattern
import java.util.stream.Collectors

private const val TYPE = "\$type"

Expand Down Expand Up @@ -57,7 +57,7 @@ object BskyWrapper {
)
)

require(response.status.value == 200) { "Failed to create session" }
require(response.status == HttpStatusCode.OK) { "Failed to create session (${response.status.value} - ${response.bodyAsText()})" }
return ObjectParser.fromJson(response.bodyAsText())
}

Expand All @@ -73,7 +73,7 @@ object BskyWrapper {
body = content,
)

require(response.status.value == 200) { "Failed to upload blob" }
require(response.status == HttpStatusCode.OK) { "Failed to upload blob (${response.status.value} - ${response.bodyAsText()})" }
return ObjectParser.fromJson(response.bodyAsText()).getAsJsonObject("blob")
}

Expand Down Expand Up @@ -165,14 +165,14 @@ object BskyWrapper {
)
)

require(response.status.value == 200) { "Failed to create record (${response.bodyAsText()})" }
require(response.status == HttpStatusCode.OK) { "Failed to create record (${response.status.value} - ${response.bodyAsText()})" }
return ObjectParser.fromJson(response.bodyAsText(), Record::class.java)
}

private fun countEmoji(text: String) = Pattern.compile("\\p{So}+")
private fun countEmoji(text: String): List<MatchResult> = Pattern.compile("\\p{So}+")
.matcher(text)
.results()
.collect(Collectors.toUnmodifiableList())
.toList()

private fun getFacets(text: String): Pair<String, List<Facet>> {
var tmpText = text
Expand All @@ -185,7 +185,7 @@ object BskyWrapper {
val beautifulLink = link.replace("https?://www\\.|\\?.*".toRegex(), "").trim()
tmpText = tmpText.replace(link, beautifulLink)

val emojiCount = countEmoji(tmpText.substringBeforeLast(beautifulLink))!!
val emojiCount = countEmoji(tmpText.substringBeforeLast(beautifulLink))
val added =
if (emojiCount.isNotEmpty()) emojiCount.sumOf { it.group().length } + emojiCount.size else 0

Expand All @@ -195,7 +195,7 @@ object BskyWrapper {
}

link.startsWith("#") -> {
val emojiCount = countEmoji(tmpText.substringBeforeLast(link))!!
val emojiCount = countEmoji(tmpText.substringBeforeLast(link))
val added =
if (emojiCount.isNotEmpty()) emojiCount.sumOf { it.group().length } + emojiCount.size else 1

Expand Down
16 changes: 6 additions & 10 deletions src/main/kotlin/fr/shikkanime/wrappers/ThreadsWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ object ThreadsWrapper {
headers = mapOf(HttpHeaders.ContentType to ContentType.Application.Json.toString()),
)

require(response.status == HttpStatusCode.OK) { "Failed to get token" }

val json = ObjectParser.fromJson(response.bodyAsText())
return json["access_token"].asString
require(response.status == HttpStatusCode.OK) { "Failed to get token (${response.status.value} - ${response.bodyAsText()})" }
return ObjectParser.fromJson(response.bodyAsText())["access_token"].asString
}

suspend fun getLongLivedAccessToken(appSecret: String, accessToken: String): String {
Expand All @@ -54,10 +52,8 @@ object ThreadsWrapper {
"grant_type=th_exchange_token",
)

require(response.status == HttpStatusCode.OK) { "Failed to get long-lived token" }

val json = ObjectParser.fromJson(response.bodyAsText())
return json["access_token"].asString
require(response.status == HttpStatusCode.OK) { "Failed to get long-lived token (${response.status.value} - ${response.bodyAsText()})" }
return ObjectParser.fromJson(response.bodyAsText())["access_token"].asString
}

suspend fun post(
Expand All @@ -82,7 +78,7 @@ object ThreadsWrapper {
headers = mapOf(HttpHeaders.ContentType to ContentType.Application.Json.toString()),
)

require(createResponse.status == HttpStatusCode.OK) { "Failed to post" }
require(createResponse.status == HttpStatusCode.OK) { "Failed to post (${createResponse.status.value} - ${createResponse.bodyAsText()})" }
val creationId = ObjectParser.fromJson(createResponse.bodyAsText())["id"].asString

val publishResponse = httpRequest.post(
Expand All @@ -92,7 +88,7 @@ object ThreadsWrapper {
headers = mapOf(HttpHeaders.ContentType to ContentType.Application.Json.toString()),
)

require(publishResponse.status == HttpStatusCode.OK) { "Failed to publish" }
require(publishResponse.status == HttpStatusCode.OK) { "Failed to publish (${publishResponse.status.value} - ${publishResponse.bodyAsText()})" }
return ObjectParser.fromJson(publishResponse.bodyAsText())["id"].asLong
}
}

0 comments on commit cc136cd

Please sign in to comment.