Skip to content

Commit

Permalink
Merge pull request #470 from Reco1I/catboy
Browse files Browse the repository at this point in the history
Introduce Mino beatmap mirror
  • Loading branch information
Rian8337 authored Dec 17, 2024
2 parents d0633d6 + 13d3cb6 commit f01d848
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 3 deletions.
Binary file added res/drawable/catboy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion src/com/reco1l/osu/beatmaplisting/BeatmapListing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.reco1l.framework.net.JsonArrayRequest
import com.reco1l.osu.*
import com.reco1l.osu.ui.Option
import com.reco1l.osu.ui.SelectDialog
import com.reco1l.toolkt.android.drawableLeft
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -132,6 +133,8 @@ class BeatmapListing : BaseFragment(),
indicator = findViewById(R.id.indicator)!!

logoView = findViewById(R.id.logo)!!
logoView.text = mirror.description
logoView.drawableLeft = requireContext().getDrawable(mirror.logoResource)
logoView.setOnClickListener {
SelectDialog()
.setOptions(BeatmapMirror.entries.map { mirror ->
Expand All @@ -151,6 +154,10 @@ class BeatmapListing : BaseFragment(),
if (value != mirror.ordinal) {
Config.setInt("beatmapMirror", value)
mirror = BeatmapMirror.entries[Config.getInt("beatmapMirror", 0)]

logoView.text = mirror.description
logoView.drawableLeft = requireContext().getDrawable(mirror.logoResource)

search(false)
}
}
Expand Down Expand Up @@ -216,7 +223,8 @@ class BeatmapListing : BaseFragment(),
JsonArrayRequest(
mirror.search.request(
query = searchBox.text.toString(),
offset = offset
offset = offset,
limit = 50
)
).use { request ->

Expand Down
22 changes: 22 additions & 0 deletions src/com/reco1l/osu/beatmaplisting/BeatmapMirror.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.reco1l.osu.beatmaplisting

import androidx.annotation.DrawableRes
import com.reco1l.osu.beatmaplisting.mirrors.CatboyDownloadRequestModel
import com.reco1l.osu.beatmaplisting.mirrors.CatboyPreviewRequestModel
import com.reco1l.osu.beatmaplisting.mirrors.CatboySearchRequestModel
import com.reco1l.osu.beatmaplisting.mirrors.CatboySearchResponseModel
import com.reco1l.osu.beatmaplisting.mirrors.OsuDirectDownloadRequestModel
import com.reco1l.osu.beatmaplisting.mirrors.OsuDirectPreviewRequestModel
import com.reco1l.osu.beatmaplisting.mirrors.OsuDirectSearchRequestModel
Expand Down Expand Up @@ -65,6 +69,24 @@ enum class BeatmapMirror(
),
download = BeatmapMirrorAction(OsuDirectDownloadRequestModel()),
preview = BeatmapMirrorAction(OsuDirectPreviewRequestModel()),
),

/**
* Catboy beatmap mirror.
*
* [See documentation](https://dev.catboy.best/docs)
*/
CATBOY(
homeUrl = "https://catboy.best",
description = "Mino",
logoResource = R.drawable.catboy,

search = BeatmapMirrorActionWithResponse(
request = CatboySearchRequestModel(),
response = CatboySearchResponseModel(),
),
download = BeatmapMirrorAction(CatboyDownloadRequestModel()),
preview = BeatmapMirrorAction(CatboyPreviewRequestModel()),
)

}
Expand Down
2 changes: 1 addition & 1 deletion src/com/reco1l/osu/beatmaplisting/BeatmapMirrorModels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fun interface BeatmapMirrorSearchRequestModel {
* @param query The search query.
* @param offset The search result offset.
*/
operator fun invoke(query: String, offset: Int): HttpUrl
operator fun invoke(query: String, offset: Int, limit: Int): HttpUrl
}

fun interface BeatmapMirrorDownloadRequestModel {
Expand Down
86 changes: 86 additions & 0 deletions src/com/reco1l/osu/beatmaplisting/mirrors/Catboy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.reco1l.osu.beatmaplisting.mirrors

import com.reco1l.osu.beatmaplisting.BeatmapMirrorDownloadRequestModel
import com.reco1l.osu.beatmaplisting.BeatmapMirrorPreviewRequestModel
import com.reco1l.osu.beatmaplisting.BeatmapMirrorSearchRequestModel
import com.reco1l.osu.beatmaplisting.BeatmapMirrorSearchResponseModel
import com.reco1l.osu.beatmaplisting.BeatmapModel
import com.reco1l.osu.beatmaplisting.BeatmapSetModel
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import org.json.JSONArray
import ru.nsu.ccfit.zuev.osu.RankedStatus

// API reference: https://dev.catboy.best/docs


class CatboySearchRequestModel : BeatmapMirrorSearchRequestModel {
override fun invoke(query: String, offset: Int, limit: Int): HttpUrl {
return "https://catboy.best/api/v2/search".toHttpUrl()
.newBuilder()
.addQueryParameter("mode", "0")
.addQueryParameter("query", query)
.addQueryParameter("limit", limit.toString())
.addQueryParameter("offset", offset.toString())
.build()
}
}


class CatboySearchResponseModel : BeatmapMirrorSearchResponseModel {
override fun invoke(response: Any): MutableList<BeatmapSetModel> {
response as JSONArray

return MutableList(response.length()) { i ->
val json = response.getJSONObject(i)

BeatmapSetModel(
id = json.getLong("id"),
title = json.getString("title"),
titleUnicode = json.getString("title_unicode"),
artist = json.getString("artist"),
artistUnicode = json.getString("artist_unicode"),
status = RankedStatus.valueOf(json.getInt("ranked")),
creator = json.getString("creator"),
thumbnail = "https://assets.ppy.sh/beatmaps/${json.getLong("id")}/covers/card.jpg",
beatmaps = json.getJSONArray("beatmaps").let {

MutableList(it.length()) { i ->

val obj = it.getJSONObject(i)

BeatmapModel(
id = obj.getLong("id"),
version = obj.getString("version"),
starRating = obj.getDouble("difficulty_rating"),
ar = obj.getDouble("ar"),
cs = obj.getDouble("cs"),
hp = obj.getDouble("drain"),
od = obj.getDouble("accuracy"),
bpm = obj.getDouble("bpm"),
lengthSec = obj.getLong("hit_length"),
circleCount = obj.getInt("count_circles"),
sliderCount = obj.getInt("count_sliders"),
spinnerCount = obj.getInt("count_spinners")
)

}.sortedBy(BeatmapModel::starRating)
}
)
}
}
}


class CatboyDownloadRequestModel : BeatmapMirrorDownloadRequestModel {
override fun invoke(beatmapSetId: Long): HttpUrl {
return "https://catboy.best/d/$beatmapSetId".toHttpUrl()
}
}


class CatboyPreviewRequestModel : BeatmapMirrorPreviewRequestModel {
override fun invoke(beatmapSetId: Long): HttpUrl {
return "https://catboy.best/preview/audio/$beatmapSetId?set=1".toHttpUrl()
}
}
3 changes: 2 additions & 1 deletion src/com/reco1l/osu/beatmaplisting/mirrors/OsuDirect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import ru.nsu.ccfit.zuev.osu.RankedStatus


class OsuDirectSearchRequestModel : BeatmapMirrorSearchRequestModel {
override fun invoke(query: String, offset: Int): HttpUrl {
override fun invoke(query: String, offset: Int, limit: Int): HttpUrl {
return "https://osu.direct/api/v2/search".toHttpUrl()
.newBuilder()
.addQueryParameter("mode", "0")
.addQueryParameter("query", query)
.addQueryParameter("offset", offset.toString())
.addQueryParameter("amount", limit.toString())
.build()
}
}
Expand Down

0 comments on commit f01d848

Please sign in to comment.