@@ -20,6 +20,8 @@ import com.github.libretube.api.obj.StreamItem.Companion.TYPE_PLAYLIST
2020import com.github.libretube.api.obj.StreamItem.Companion.TYPE_STREAM
2121import com.github.libretube.api.obj.Streams
2222import com.github.libretube.api.obj.Subtitle
23+ import com.github.libretube.extensions.parallelMap
24+ import com.github.libretube.extensions.sha256Sum
2325import com.github.libretube.extensions.toID
2426import com.github.libretube.helpers.NewPipeExtractorInstance
2527import com.github.libretube.helpers.PlayerHelper
@@ -48,7 +50,6 @@ import org.schabi.newpipe.extractor.stream.AudioStream
4850import org.schabi.newpipe.extractor.stream.StreamInfo
4951import org.schabi.newpipe.extractor.stream.StreamInfoItem
5052import org.schabi.newpipe.extractor.stream.VideoStream
51- import java.security.MessageDigest
5253
5354
5455private fun VideoStream.toPipedStream () = PipedStream (
@@ -320,27 +321,33 @@ class NewPipeMediaServiceRepository : MediaServiceRepository {
320321 )
321322 }
322323
323- @OptIn(ExperimentalStdlibApi ::class )
324324 override suspend fun getSegments (
325- videoId : String ,
326- category : List <String >,
327- actionType : List <String >?
328- ): SegmentData {
325+ videoId : String , category : List <String >, actionType : List <String >?
326+ ): SegmentData = RetrofitInstance .externalApi.getSegments(
329327 // use hashed video id for privacy
330328 // 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- }
329+ videoId.sha256Sum().substring(0 , 4 ), category, actionType
330+ ).first { it.videoID == videoId }
341331
342332 override suspend fun getDeArrowContent (videoIds : String ): Map <String , DeArrowContent > =
343- emptyMap()
333+ videoIds.split(' ,' ).chunked(25 ).flatMap {
334+ it.parallelMap { videoId ->
335+ runCatching {
336+ RetrofitInstance .externalApi.getDeArrowContent(
337+ // use hashed video id for privacy
338+ // https://wiki.sponsor.ajay.app/w/API_Docs/DeArrow#GET_/api/branding/:sha256HashPrefix
339+ videoId.sha256Sum().substring(0 , 4 )
340+ )
341+ }.getOrNull()
342+ }
343+ }.filterNotNull().reduce { acc, map -> acc + map }.mapValues { (videoId, value) ->
344+ value.copy(
345+ thumbnails = value.thumbnails.map { thumbnail ->
346+ thumbnail.takeIf { it.original } ? : thumbnail.copy(
347+ thumbnail = " ${DEARROW_THUMBNAIL_URL } ?videoID=$videoId &time=${thumbnail.timestamp} "
348+ )
349+ })
350+ }
344351
345352 override suspend fun getSearchResults (searchQuery : String , filter : String ): SearchResult {
346353 val queryHandler = NewPipeExtractorInstance .extractor.searchQHFactory.fromQuery(
@@ -494,4 +501,8 @@ class NewPipeMediaServiceRepository : MediaServiceRepository {
494501 comments = commentsInfo.items.map { it.toComment() }
495502 )
496503 }
504+
505+ companion object {
506+ private const val DEARROW_THUMBNAIL_URL = " https://dearrow-thumb.ajay.app/api/v1/getThumbnail"
507+ }
497508}
0 commit comments