Skip to content

Commit e463bce

Browse files
committed
refactor: simplify dearrow requests by only accepting a single video id
Simplifies the code to request the dearrow content, by only accepting a single video id, instead of multiple. While the PipedBackend does support retrieving multiple videos at once (as opposed to the official API), we do not take advantage of it.
1 parent 5fc2e20 commit e463bce

5 files changed

Lines changed: 20 additions & 29 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface MediaServiceRepository {
2525
actionType: List<String>? = null
2626
): SegmentData
2727

28-
suspend fun getDeArrowContent(videoIds: String): Map<String, DeArrowContent>
28+
suspend fun getDeArrowContent(videoId: String): DeArrowContent?
2929
suspend fun getCommentsNextPage(videoId: String, nextPage: String): CommentsPage
3030
suspend fun getSearchResults(searchQuery: String, filter: String): SearchResult
3131
suspend fun getSearchResultsNextPage(

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -360,18 +360,14 @@ class NewPipeMediaServiceRepository : MediaServiceRepository {
360360
videoId.sha256Sum().substring(0, 4), category, actionType
361361
).first { it.videoID == videoId }
362362

363-
override suspend fun getDeArrowContent(videoIds: String): Map<String, DeArrowContent> =
364-
videoIds.split(',').chunked(25).flatMap {
365-
it.parallelMap { videoId ->
366-
runCatching {
367-
RetrofitInstance.externalApi.getDeArrowContent(
368-
// use hashed video id for privacy
369-
// https://wiki.sponsor.ajay.app/w/API_Docs/DeArrow#GET_/api/branding/:sha256HashPrefix
370-
videoId.sha256Sum().substring(0, 4)
371-
)
372-
}.getOrNull()
373-
}
374-
}.filterNotNull().reduce { acc, map -> acc + map }.mapValues { (videoId, value) ->
363+
override suspend fun getDeArrowContent(videoId: String): DeArrowContent? =
364+
runCatching {
365+
RetrofitInstance.externalApi.getDeArrowContent(
366+
// use hashed video id for privacy
367+
// https://wiki.sponsor.ajay.app/w/API_Docs/DeArrow#GET_/api/branding/:sha256HashPrefix
368+
videoId.sha256Sum().substring(0, 4)
369+
)
370+
}.getOrDefault(emptyMap())[videoId]?.let { value ->
375371
value.copy(
376372
thumbnails = value.thumbnails.map { thumbnail ->
377373
thumbnail.takeIf { it.original } ?: thumbnail.copy(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ open class PipedMediaServiceRepository : MediaServiceRepository {
4747
JsonHelper.json.encodeToString(actionType)
4848
)
4949

50-
override suspend fun getDeArrowContent(videoIds: String): Map<String, DeArrowContent> =
51-
api.getDeArrowContent(videoIds)
50+
override suspend fun getDeArrowContent(videoId: String): DeArrowContent? =
51+
api.getDeArrowContent(videoId)[videoId]
5252

5353
override suspend fun getCommentsNextPage(videoId: String, nextPage: String): CommentsPage =
5454
api.getCommentsNextPage(videoId, nextPage)

app/src/main/java/com/github/libretube/ui/dialogs/SubmitDeArrowDialog.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class SubmitDeArrowDialog: DialogFragment() {
7373
val data = try {
7474
withContext(Dispatchers.IO) {
7575
MediaServiceRepository.instance.getDeArrowContent(videoId)
76-
}.getOrElse(videoId) { return }
76+
} ?: return
7777
} catch (e: Exception) {
7878
return
7979
}

app/src/main/java/com/github/libretube/util/DeArrowUtil.kt

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ object DeArrowUtil {
1818
}
1919

2020

21-
private suspend fun fetchDeArrowContent(videoId: String): Map<String, DeArrowContent>? {
21+
private suspend fun fetchDeArrowContent(videoId: String): DeArrowContent? {
2222
return try {
2323
MediaServiceRepository.instance.getDeArrowContent(videoId)
24+
.also { memoryCache.put(videoId, CacheObject(it)) }
2425
} catch (e: Exception) {
2526
Log.e(this::class.java.name, "Failed to fetch DeArrow content: ${e.message}")
2627
null
@@ -33,13 +34,11 @@ object DeArrowUtil {
3334
suspend fun deArrowStreams(streams: Streams, vidId: String): Streams {
3435
if (!PreferenceHelper.getBoolean(PreferenceKeys.DEARROW, false)) return streams
3536

36-
val response = fetchDeArrowContent(vidId) ?: return streams
37+
val data = fetchDeArrowContent(vidId) ?: return streams
38+
val (newTitle, newThumbnail) = extractTitleAndThumbnail(data)
3739

38-
response[vidId]?.let { data ->
39-
val (newTitle, newThumbnail) = extractTitleAndThumbnail(data)
40-
if (newTitle != null) streams.title = newTitle
41-
if (newThumbnail != null) streams.thumbnailUrl = newThumbnail
42-
}
40+
if (newTitle != null) streams.title = newTitle
41+
if (newThumbnail != null) streams.thumbnailUrl = newThumbnail
4342

4443
return streams
4544
}
@@ -50,11 +49,7 @@ object DeArrowUtil {
5049
suspend fun deArrowVideoId(videoId: String): Pair<String?, String?>? {
5150
if (!PreferenceHelper.getBoolean(PreferenceKeys.DEARROW, false)) return null
5251

53-
val response = fetchDeArrowContent(videoId) ?: return null
54-
response[videoId]?.let { data ->
55-
return extractTitleAndThumbnail(data)
56-
}
57-
58-
return null
52+
val data = fetchDeArrowContent(videoId) ?: return null
53+
return extractTitleAndThumbnail(data)
5954
}
6055
}

0 commit comments

Comments
 (0)