-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ a working SoundCloudThumbnailFinder
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...tlin/mikufan/cx/songfinder/backend/component/thumbnailfinder/SoundCloudThumbnailFinder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package mikufan.cx.songfinder.backend.component.thumbnailfinder | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import io.ktor.client.* | ||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
import kotlinx.coroutines.withContext | ||
import mikufan.cx.songfinder.backend.db.entity.PvService | ||
import mikufan.cx.songfinder.backend.model.PVInfo | ||
import mikufan.cx.songfinder.backend.model.ThumbnailInfo | ||
import mikufan.cx.songfinder.backend.util.MyDispatchers | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class SoundCloudThumbnailFinder( | ||
private val client: HttpClient, | ||
) : ThumbnailFinder { | ||
override val matchedPvService = PvService.SoundCloud | ||
|
||
companion object { | ||
// https://stackoverflow.com/questions/65352609/how-can-i-get-a-client-id | ||
private val CLIENT_IDS = listOf( | ||
"a3e059563d7fd3372b49b37f00a00bcf", | ||
"RMDyLXBETEYF6qcE4jmndm0VF1QZt2T4" | ||
) | ||
private val RANDOM_CLIENT_ID: String | ||
get() = CLIENT_IDS.random() | ||
} | ||
|
||
override suspend fun findThumbnail(pv: PVInfo): ThumbnailInfo { | ||
val id = pv.id.split(" ").first() | ||
val rsp = withContext(MyDispatchers.ioDispatcher) { | ||
client.get("https://api-v2.soundcloud.com/tracks/") { | ||
url { | ||
appendEncodedPathSegments(id) | ||
parameters["client_id"] = RANDOM_CLIENT_ID | ||
} | ||
} | ||
} | ||
val status = rsp.status.value | ||
when (status) { | ||
in (200..<300) -> { | ||
val json = rsp.body<JsonNode>() | ||
val thumbnailUrlJson = json["artwork_url"] | ||
if (thumbnailUrlJson.isNull) { | ||
throw ThumbnailNotAvailableException("The $matchedPvService video $id does not have an artwork") | ||
} else { | ||
val thumbnailUrl = thumbnailUrlJson.asText() | ||
return ThumbnailInfo(thumbnailUrl) | ||
} | ||
} | ||
|
||
else -> { | ||
throw ThumbnailNotAvailableException("The $matchedPvService video $id is not available, status code: $status") | ||
} | ||
} | ||
} | ||
} |