Skip to content

Commit

Permalink
Merge pull request #13 from daksh7011/fix/torrent_command_403
Browse files Browse the repository at this point in the history
Fix torrents command 403
  • Loading branch information
daksh7011 authored Aug 12, 2024
2 parents fdf78e9 + 340202d commit 0f609e9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 26 deletions.
25 changes: 25 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Note: Quotation marks are required for the leading asterisk

dependency:
- changed-files:
- any-glob-to-any-file: 'gradle/*.toml'

ci:
- changed-files:
- any-glob-to-any-file: 'github/**/*'

commands:
- changed-files:
- any-glob-to-any-file: 'src/main/kotlin/qbtbot/commands/**/*'

# Add 'feature' label to any PR where the head branch name starts with `feature` or has a `feature` section in the name
feature:
- head-branch: ['^feature', 'feature']

# Add 'fix' label to any PR where the head branch name starts with `fix` or has a `fix` section in the name
fix:
- head-branch: ['^fix', 'fix']

# Add 'release' label to any PR that is opened against the `master` branch
release:
- base-branch: 'master'
10 changes: 7 additions & 3 deletions .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ repository:
labels:
- name: bug
color: CC0000
- name: fix
color: D93F0B
- name: feature
color: 0E8A16
- name: commands
Expand All @@ -53,10 +55,12 @@ labels:
- name: maintenance
color: 1D76DB
- name: major
color: fbca04
color: FBCA04
- name: minor
color: fbca04
color: FBCA04
- name: patch
color: fbca04
color: FBCA04
- name: release
color: 0052CC
- name: first-timers-only
oldname: Help Wanted
15 changes: 8 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
name: Build (CI)

on:
push:
branches-ignore:
- master
- develop

pull_request:

concurrency:
Expand All @@ -14,11 +9,19 @@ concurrency:

jobs:
build-ci:

permissions:
contents: write
pull-requests: write

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Labeler
uses: actions/labeler@v5.0.0

- name: Set up Java
uses: actions/setup-java@v4
with:
Expand All @@ -36,14 +39,12 @@ jobs:

- name: Upload artifacts (Main JAR)
uses: actions/upload-artifact@v4

with:
name: Main JAR
path: build/libs/*-all.jar

- name: Upload artifacts (JARs)
uses: actions/upload-artifact@v4

with:
name: JARs
path: build/libs/*.jar
4 changes: 2 additions & 2 deletions detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ formatting:
continuationIndentSize: 4
MaximumLineLength:
active: true
maxLineLength: 120
maxLineLength: 180
ModifierOrdering:
active: true
autoCorrect: true
Expand Down Expand Up @@ -575,7 +575,7 @@ style:
active: true
MaxLineLength:
active: true
maxLineLength: 120
maxLineLength: 180
excludePackageStatements: true
excludeImportStatements: true
excludeCommentStatements: false
Expand Down
30 changes: 20 additions & 10 deletions src/main/kotlin/qbtbot/commands/TorrentInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,47 @@ import com.kotlindiscord.kord.extensions.extensions.chatCommand
import com.kotlindiscord.kord.extensions.utils.respond
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.http.HttpStatusCode
import qbtbot.model.Torrent
import qbtbot.util.BASE_URL
import qbtbot.util.bold
import qbtbot.util.httpClient
import qbtbot.util.requestQbitTorrentForLogin
import qbtbot.util.round
import qbtbot.util.testGuild

@Suppress("MagicNumber") // TODO: Remove
class TorrentInfo : Extension() {
override val name: String = "Torrent Info"

override suspend fun setup() {
chatCommand {
name = "torrents"
description = "Get information of available torrents"
check { failIf(event.message.author == null) }
check { failIf(event.message.getGuild().id != testGuild) }
action {
val torrents: List<Torrent> = httpClient.get("$BASE_URL/torrents/info").body()
val response = torrents.groupBy({ it.category }, { it })
var response = httpClient.get("$BASE_URL/torrents/info")
if (response.status == HttpStatusCode.Forbidden) {
requestQbitTorrentForLogin()
// Try again single time after requesting login again in case of 403 due to timeout
response = httpClient.get("$BASE_URL/torrents/info").body()
}
val torrents: List<Torrent> = response.body()
val finalMessage = torrents.groupBy({ it.category }, { it })
.map { (category, torrents) ->
"Category: ${category.bold()}\n${
torrents.joinToString(separator = "\n") {
"${
it.name.substring(
0,
it.name.length.coerceAtMost(50)
)
} is ${(it.progress * 100).round()}%"
"${it.name.substring(0, it.name.length.coerceAtMost(TORRENT_NAME_MAX_LENGTH))} is ${(it.progress * PROGRESS_FACTOR).round()}%"
}
}"
}.joinToString(separator = "\n\n\n")
message.respond(response)
message.respond(finalMessage)
}
}
}

private companion object {
private const val PROGRESS_FACTOR = 100
private const val TORRENT_NAME_MAX_LENGTH = 50
}
}
12 changes: 8 additions & 4 deletions src/main/kotlin/qbtbot/util/Extension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.kord.core.Kord
import dev.kord.core.entity.Message
import dev.kord.rest.builder.message.EmbedBuilder
import io.ktor.client.request.forms.submitForm
import io.ktor.client.statement.HttpResponse
import io.ktor.http.parameters
import kotlin.math.round

Expand All @@ -28,16 +29,19 @@ fun String?.bold(): String = "**$this**"
fun String?.italic(): String = "*$this*"

Check warning on line 29 in src/main/kotlin/qbtbot/util/Extension.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Function "italic" is never used

suspend fun ExtensibleBot.loginToBackend() {
// try to log in to qBitTorrent
val response = httpClient.submitForm(
val response = requestQbitTorrentForLogin()
logger.info { "qBitTorrent login -> status: ${response.status}" }
}

suspend fun requestQbitTorrentForLogin(): HttpResponse =
// try to log in to qBitTorrent and save cookie
httpClient.submitForm(
url = "$BASE_URL/auth/login",
formParameters = parameters {
append("username", env(Environment.USERNAME))
append("password", env(Environment.PASSWORD))
}
)
logger.info { "qBitTorrent login -> status: ${response.status}" }
}

@Suppress("MagicNumber")
fun Double.round(decimals: Int = 2): Double {
Expand Down

0 comments on commit 0f609e9

Please sign in to comment.