Skip to content

Commit 31613c8

Browse files
committed
feat(MediaService/Local): implement SponsorBlock support
Allows users to use SponsorBlock when using full local mode. Closes: libre-tube#7193
1 parent 2497fa8 commit 31613c8

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

app/src/main/java/com/github/libretube/api/ExternalApi.kt

+9
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package com.github.libretube.api
33
import com.github.libretube.api.obj.DeArrowBody
44
import com.github.libretube.api.obj.PipedConfig
55
import com.github.libretube.api.obj.PipedInstance
6+
import com.github.libretube.api.obj.SegmentData
67
import com.github.libretube.api.obj.SubmitSegmentResponse
78
import com.github.libretube.api.obj.VoteInfo
89
import com.github.libretube.obj.update.UpdateInfo
910
import retrofit2.http.Body
1011
import retrofit2.http.GET
1112
import retrofit2.http.POST
13+
import retrofit2.http.Path
1214
import retrofit2.http.Query
1315
import retrofit2.http.Url
1416

@@ -43,6 +45,13 @@ interface ExternalApi {
4345
@Query("description") description: String = ""
4446
): List<SubmitSegmentResponse>
4547

48+
@GET("$SB_API_URL/api/skipSegments/{videoId}")
49+
suspend fun getSegments(
50+
@Path("videoId") videoId: String,
51+
@Query("category") category: List<String>,
52+
@Query("actionType") actionType: List<String>? = null
53+
): List<SegmentData>
54+
4655
@POST("$SB_API_URL/api/branding")
4756
suspend fun submitDeArrow(@Body body: DeArrowBody)
4857

app/src/main/java/com/github/libretube/api/NewPipeMediaServiceRepository.kt

+17-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import org.schabi.newpipe.extractor.stream.AudioStream
4848
import org.schabi.newpipe.extractor.stream.StreamInfo
4949
import org.schabi.newpipe.extractor.stream.StreamInfoItem
5050
import org.schabi.newpipe.extractor.stream.VideoStream
51+
import java.security.MessageDigest
5152

5253

5354
private fun VideoStream.toPipedStream() = PipedStream(
@@ -319,11 +320,24 @@ class NewPipeMediaServiceRepository : MediaServiceRepository {
319320
)
320321
}
321322

323+
@OptIn(ExperimentalStdlibApi::class)
322324
override suspend fun getSegments(
323325
videoId: String,
324-
category: String,
325-
actionType: String?
326-
): SegmentData = SegmentData()
326+
category: List<String>,
327+
actionType: List<String>?
328+
): SegmentData {
329+
// use hashed video id for privacy
330+
// https://wiki.sponsor.ajay.app/w/API_Docs#GET_/api/skipSegments/:sha256HashPrefix
331+
val hashedId = MessageDigest.getInstance("SHA-256")
332+
.digest(videoId.toByteArray())
333+
.toHexString()
334+
335+
return RetrofitInstance.externalApi.getSegments(
336+
hashedId.substring(0..4),
337+
category,
338+
actionType
339+
).first { it.videoID == videoId }
340+
}
327341

328342
override suspend fun getDeArrowContent(videoIds: String): Map<String, DeArrowContent> =
329343
emptyMap()

0 commit comments

Comments
 (0)