Skip to content

Commit b871950

Browse files
committed
feat: show the game chapters of Twitch past broadcasts
1 parent acb4daf commit b871950

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎core/data/src/main/java/video/typetype/tv/data/Twitch.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ interface TwitchRepository {
8989

9090
suspend fun vod(id: String): TwitchPlayback
9191

92+
suspend fun chapters(id: String): List<Chapter>
93+
9294
suspend fun clip(slug: String): TwitchClipPlayback
9395

9496
suspend fun searchChannels(query: String, cursor: String? = null): TwitchPage<Channel>

‎core/data/src/main/java/video/typetype/tv/data/remote/RemoteTwitchRepository.kt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package video.typetype.tv.data.remote
22

33
import okhttp3.HttpUrl
44
import video.typetype.tv.data.Channel
5+
import video.typetype.tv.data.Chapter
56
import video.typetype.tv.data.TwitchCategory
67
import video.typetype.tv.data.TwitchChannelPage
78
import video.typetype.tv.data.TwitchClipPlayback
@@ -69,6 +70,9 @@ class RemoteTwitchRepository(
6970
return TwitchPlayback(dto.video.toVideo(clock()), playlist(dto.hlsUrl), live = dto.video.recording)
7071
}
7172

73+
override suspend fun chapters(id: String): List<Chapter> =
74+
api.videoChapters(id).map { Chapter(title = it.title.ifBlank { it.game?.name.orEmpty() }, startSeconds = it.start.toInt()) }
75+
7276
override suspend fun clip(slug: String): TwitchClipPlayback {
7377
val dto = api.clip(slug)
7478
// Clip files are served by Live under the instance origin, like the playlists.

‎core/data/src/main/java/video/typetype/tv/data/remote/TwitchContent.kt‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
package video.typetype.tv.data.remote
22

3+
import kotlinx.coroutines.async
4+
import kotlinx.coroutines.coroutineScope
35
import video.typetype.tv.data.Channel
46
import video.typetype.tv.data.ChannelPage
57
import video.typetype.tv.data.ChannelSummary
68
import video.typetype.tv.data.ChannelTab
9+
import video.typetype.tv.data.Chapter
710
import video.typetype.tv.data.SearchKind
811
import video.typetype.tv.data.SearchResults
912
import video.typetype.tv.data.TwitchRepository
1013
import video.typetype.tv.data.TwitchTarget
1114
import video.typetype.tv.data.TwitchUrls
1215
import video.typetype.tv.data.Video
16+
import video.typetype.tv.data.net.ApiException
17+
import java.io.IOException
1318

1419
// Twitch pages are read from TypeType-Live through the server, not from the stream extractor the other sources use.
1520
internal class TwitchContent(private val twitch: TwitchRepository) {
1621
suspend fun video(target: TwitchTarget): Video? = when (target) {
1722
is TwitchTarget.Live -> twitch.live(target.login)?.video ?: twitch.channel(target.login).live
18-
is TwitchTarget.Vod -> twitch.vod(target.id).video
23+
is TwitchTarget.Vod -> coroutineScope {
24+
val chapters = async { chapters(target.id) }
25+
twitch.vod(target.id).video.copy(chapters = chapters.await())
26+
}
1927
is TwitchTarget.Clip -> twitch.clip(target.slug).video
2028
}
2129

@@ -35,6 +43,15 @@ internal class TwitchContent(private val twitch: TwitchRepository) {
3543
)
3644
}
3745

46+
// A past broadcast's game changes; a single chapter says nothing, and the page stays usable without them.
47+
private suspend fun chapters(id: String): List<Chapter> = try {
48+
twitch.chapters(id).takeIf { it.size > 1 }.orEmpty()
49+
} catch (failure: ApiException) {
50+
emptyList()
51+
} catch (failure: IOException) {
52+
emptyList()
53+
}
54+
3855
suspend fun search(query: String, kind: SearchKind, cursor: String?): SearchResults = when (kind) {
3956
SearchKind.Live -> twitch.searchLives(query, cursor).let { SearchResults(videos = it.items, nextPage = it.cursor) }
4057
else -> twitch.searchChannels(query, cursor).let { page ->

‎core/data/src/test/java/video/typetype/tv/data/remote/TwitchContentTest.kt‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.junit.Assert.assertNull
66
import org.junit.Test
77
import video.typetype.tv.data.Channel
88
import video.typetype.tv.data.ChannelTab
9+
import video.typetype.tv.data.Chapter
910
import video.typetype.tv.data.SearchKind
1011
import video.typetype.tv.data.TwitchCategory
1112
import video.typetype.tv.data.TwitchChannelPage
@@ -72,6 +73,16 @@ class TwitchContentTest {
7273
private fun video(id: String, isLive: Boolean = false): Video =
7374
Video(id, id, channel, 0, "", "", "", "", isLive = isLive)
7475

76+
@Test
77+
fun aPastBroadcastCarriesItsGameChaptersOnlyWhenThereAreSeveral() = runBlocking {
78+
chapterList = listOf(Chapter("Fortnite", 0), Chapter("Just Chatting", 559))
79+
assertEquals(listOf(0, 559), content.video(TwitchTarget.Vod("1"))?.chapters?.map { it.startSeconds })
80+
chapterList = listOf(Chapter("Fortnite", 0))
81+
assertEquals(emptyList<Chapter>(), content.video(TwitchTarget.Vod("1"))?.chapters)
82+
}
83+
84+
private var chapterList: List<Chapter> = emptyList()
85+
7586
private inner class FakeTwitch : TwitchRepository {
7687
override val enabled: Boolean = true
7788
override suspend fun popular(cursor: String?): TwitchPage<Video> = TwitchPage(listOf(live), null)
@@ -83,6 +94,7 @@ class TwitchContentTest {
8394
override suspend fun channelClips(login: String, cursor: String?) = TwitchPage<Video>(emptyList(), null)
8495
override suspend fun live(login: String): TwitchPlayback? = if (online) TwitchPlayback(live, "https://i.example/live", true) else null
8596
override suspend fun vod(id: String): TwitchPlayback = TwitchPlayback(vod, "https://i.example/vod", false)
97+
override suspend fun chapters(id: String) = chapterList
8698
override suspend fun clip(slug: String): TwitchClipPlayback = TwitchClipPlayback(vod, emptyList())
8799
override suspend fun searchChannels(query: String, cursor: String?) =
88100
TwitchPage(listOf(Channel("space", "Space", "space", "1.9M", TwitchUrls.live("space"))), "c2")

0 commit comments

Comments
 (0)