Skip to content

Commit a9e82cf

Browse files
committed
Fix '0' viewers count on some goodgame channels
1 parent 75e6a19 commit a9e82cf

7 files changed

Lines changed: 42 additions & 53 deletions

File tree

src/main/kotlin/failchat/Dependencies.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,7 @@ class Dependencies {
354354
// Goodgame
355355
val ggApi2Client = GgApi2Client(
356356
httpClient = okHttpClient,
357-
objectMapper = objectMapper,
358-
apiUrl = configuration.getString("goodgame.api2-url")
357+
objectMapper = objectMapper
359358
)
360359
val ggBadgeHandler = { channel: GgChannel ->
361360
GgBadgeHandler(channel, configuration)

src/main/kotlin/failchat/goodgame/GgApi2Client.kt

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,33 @@ import okhttp3.Request
1111

1212
class GgApi2Client(
1313
private val httpClient: OkHttpClient,
14-
private val objectMapper: ObjectMapper,
15-
apiUrl: String
14+
private val objectMapper: ObjectMapper
1615
) {
1716

18-
// http://api2.goodgame.ru/apigility/documentation/Goodgame-v2
19-
20-
private val apiUrl = apiUrl.removeSuffix("/")
17+
private companion object {
18+
// Documentation: https://api2.goodgame.ru/apigility/documentation/Goodgame-v2
19+
const val apiUrl = "https://api2.goodgame.ru/v2"
20+
}
2121

22-
/** @return viewers count for only a goodgame player. */
22+
/** @return viewers count for a goodgame video player. */
2323
suspend fun requestViewersCount(channelName: String): Int {
2424
val request = Request.Builder()
2525
.get()
2626
.url("$apiUrl/streams/$channelName")
2727
.header("Accept", "application/vnd.goodgame.v2+json")
2828
.build()
2929

30-
val response = httpClient.newCall(request).await()
31-
32-
if (response.code != 200) throw UnexpectedResponseCodeException(response.code)
33-
val responseBody = response.body ?: throw UnexpectedResponseException("null body")
34-
val responseNode = objectMapper.readTree(responseBody.string())
35-
36-
37-
val streamLive = responseNode.get("status").textValue() == "Live"
38-
val ggPlayerLive = responseNode.get("is_broadcast").booleanValue()
30+
val response = httpClient.newCall(request).await().use { response ->
31+
if (response.code != 200) throw UnexpectedResponseCodeException(response.code)
32+
val responseBody = response.body ?: throw UnexpectedResponseException("null body")
33+
objectMapper.readValue(responseBody.charStream(), StreamResponse::class.java)
34+
}
3935

40-
if (!streamLive) throw ChannelOfflineException(GOODGAME, channelName)
41-
if (!ggPlayerLive) return 0
36+
if (response.status != "Live") {
37+
throw ChannelOfflineException(GOODGAME, channelName)
38+
}
4239

43-
return responseNode.get("player_viewers").asInt()
40+
return response.playerViewers
4441
}
4542

4643
}

src/main/kotlin/failchat/goodgame/GgApiClient.kt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package failchat.goodgame
22

33
import com.fasterxml.jackson.databind.JsonNode
44
import com.fasterxml.jackson.databind.ObjectMapper
5-
import failchat.Origin
6-
import failchat.exception.ChannelNotFoundException
7-
import failchat.exception.ChannelOfflineException
85
import failchat.exception.UnexpectedResponseCodeException
96
import failchat.exception.UnexpectedResponseException
107
import failchat.util.await
@@ -62,16 +59,6 @@ class GgApiClient(
6259
)
6360
}
6461

65-
/** @return viewers count on all players. */
66-
suspend fun requestViewersCount(channelName: String): Int {
67-
// https://github.com/GoodGame/API/blob/master/Streams/stream_api.md
68-
val response = requestChannelStatus(channelName)
69-
val statusNode = response.firstOrNull()
70-
?: throw ChannelNotFoundException("goodgame: $channelName")
71-
if (statusNode.get("status").asText() != "Live") throw ChannelOfflineException(Origin.GOODGAME, channelName)
72-
return statusNode.get("viewers").asText().toInt()
73-
}
74-
7562
private suspend fun requestChannelStatus(channelName: String): JsonNode {
7663
val parameters = "?fmt=json&id=$channelName"
7764

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package failchat.goodgame
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty
4+
5+
data class StreamResponse(
6+
val status: String,
7+
@JsonProperty("player_viewers")
8+
val playerViewers: Int
9+
)

src/main/resources/config/default.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ bttv.api-url = https://api.betterttv.net/
7575
frankerfacez.api-url = https://api.frankerfacez.com/v1/
7676
goodgame.ws-url = wss://chat-1.goodgame.ru/chat2/
7777
goodgame.api-url = http://goodgame.ru/api/
78-
goodgame.api2-url = https://api2.goodgame.ru/v2/
7978
goodgame.emoticon-js-url = https://goodgame.ru/js/minified/global.js
8079
goodgame.badge-url = https://static.goodgame.ru/files/icons/
8180

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
package failchat.goodgame
22

3-
import failchat.defaultConfig
3+
import failchat.exception.ChannelOfflineException
44
import failchat.okHttpClient
55
import failchat.testObjectMapper
66
import kotlinx.coroutines.runBlocking
7+
import mu.KotlinLogging
8+
import kotlin.test.Test
79

810
class GgApi2ClientTest {
911

10-
private val client = GgApi2Client(okHttpClient, testObjectMapper, defaultConfig.getString("goodgame.api2-url"))
12+
private companion object {
13+
val logger = KotlinLogging.logger {}
14+
}
15+
16+
private val client = GgApi2Client(okHttpClient, testObjectMapper)
1117

12-
// todo
13-
// @Test
18+
@Test
1419
fun requestViewersCountTest() = runBlocking<Unit> {
15-
val count = client.requestViewersCount("Miker")
16-
println(count)
20+
try {
21+
val count = client.requestViewersCount("Fotos")
22+
logger.debug("gg viewers count: {}", count)
23+
} catch (ignored: ChannelOfflineException) {
24+
logger.debug("gg channel is offline")
25+
}
1726
}
1827

1928
}

src/test/kotlin/failchat/goodgame/GgApiTest.kt renamed to src/test/kotlin/failchat/goodgame/GgApiClientTest.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package failchat.goodgame
22

33
import failchat.defaultConfig
4-
import failchat.exception.ChannelOfflineException
54
import failchat.okHttpClient
65
import failchat.testObjectMapper
76
import kotlinx.coroutines.runBlocking
@@ -10,10 +9,10 @@ import org.slf4j.Logger
109
import org.slf4j.LoggerFactory
1110
import kotlin.system.measureTimeMillis
1211

13-
class GgApiTest {
12+
class GgApiClientTest {
1413

1514
private companion object {
16-
val log: Logger = LoggerFactory.getLogger(GgApiTest::class.java)
15+
val log: Logger = LoggerFactory.getLogger(GgApiClientTest::class.java)
1716
}
1817

1918
private val apiClient = GgApiClient(
@@ -37,16 +36,6 @@ class GgApiTest {
3736
log.debug("gg emoticons load time: {} ms", t)
3837
}
3938

40-
@Test
41-
fun viewersCountTest() = runBlocking<Unit> {
42-
try {
43-
val count = apiClient.requestViewersCount("Miker")
44-
log.debug("gg viewers count: {}", count)
45-
} catch (ignored: ChannelOfflineException) {
46-
log.debug("gg channel is offline")
47-
}
48-
}
49-
5039
@Test
5140
fun requestChannelInfoTest() = runBlocking<Unit> {
5241
val c = apiClient.requestChannelInfo("Miker")

0 commit comments

Comments
 (0)