-
-
Notifications
You must be signed in to change notification settings - Fork 446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support for DeArrow #4276
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/github/libretube/api/obj/DeArrowContent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.github.libretube.api.obj | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class DeArrowContent( | ||
val thumbnails: List<DeArrowThumbnail>, | ||
val titles: List<DeArrowTitle>, | ||
val randomTime: Float?, | ||
val videoDuration: Float? | ||
) |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/github/libretube/api/obj/DeArrowThumbnail.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.github.libretube.api.obj | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class DeArrowThumbnail( | ||
val UUID: String, | ||
val locked: Boolean, | ||
val original: Boolean, | ||
val thumbnail: String? = null, | ||
val timestamp: Float?, | ||
val votes: Int | ||
) |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/github/libretube/api/obj/DeArrowTitle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.github.libretube.api.obj | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class DeArrowTitle( | ||
val UUID: String, | ||
val locked: Boolean, | ||
val original: Boolean, | ||
val title: String, | ||
val votes: Int | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
app/src/main/java/com/github/libretube/util/DeArrowUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.github.libretube.util | ||
|
||
import android.util.Log | ||
import com.github.libretube.api.JsonHelper | ||
import com.github.libretube.api.RetrofitInstance | ||
import com.github.libretube.api.obj.ContentItem | ||
import com.github.libretube.api.obj.DeArrowContent | ||
import com.github.libretube.api.obj.StreamItem | ||
import com.github.libretube.constants.PreferenceKeys | ||
import com.github.libretube.extensions.toID | ||
import com.github.libretube.helpers.PreferenceHelper | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.decodeFromJsonElement | ||
|
||
object DeArrowUtil { | ||
private fun extractTitleAndThumbnail(data: JsonElement): Pair<String?, String?> { | ||
val content = try { | ||
JsonHelper.json.decodeFromJsonElement<DeArrowContent>(data) | ||
} catch (e: Exception) { | ||
return null to null | ||
} | ||
val newTitle = content.titles.maxByOrNull { it.votes }?.title | ||
val newThumbnail = | ||
content.thumbnails.filter { it.thumbnail != null }.maxByOrNull { it.votes } | ||
?.takeIf { !it.original }?.thumbnail | ||
return newTitle to newThumbnail | ||
} | ||
|
||
/** | ||
* Apply the new titles and thumbnails generated by DeArrow to the stream items | ||
*/ | ||
suspend fun deArrowStreamItems(streamItems: List<StreamItem>): List<StreamItem> { | ||
if (!PreferenceHelper.getBoolean(PreferenceKeys.DEARROW, false)) return streamItems | ||
|
||
val videoIds = streamItems.mapNotNull { it.url?.toID() }.joinToString(",") | ||
val response = try { | ||
RetrofitInstance.api.getDeArrowContent(videoIds) | ||
} catch (e: Exception) { | ||
Log.e(this::class.java.name, e.toString()) | ||
return streamItems | ||
} | ||
for ((videoId, data) in response.entries) { | ||
val (newTitle, newThumbnail) = extractTitleAndThumbnail(data) | ||
val streamItem = streamItems.firstOrNull { it.url?.toID() == videoId } | ||
newTitle?.let { streamItem?.title = newTitle } | ||
newThumbnail?.let { streamItem?.thumbnail = newThumbnail } | ||
} | ||
return streamItems | ||
} | ||
|
||
/** | ||
* Apply the new titles and thumbnails generated by DeArrow to the stream items | ||
*/ | ||
suspend fun deArrowContentItems(contentItems: List<ContentItem>): List<ContentItem> { | ||
if (!PreferenceHelper.getBoolean(PreferenceKeys.DEARROW, false)) return contentItems | ||
|
||
val videoIds = contentItems.filter { it.type == "stream" } | ||
.joinToString(",") { it.url.toID() } | ||
if (videoIds.isEmpty()) return contentItems | ||
|
||
val response = try { | ||
RetrofitInstance.api.getDeArrowContent(videoIds) | ||
} catch (e: Exception) { | ||
Log.e(this::class.java.name, e.toString()) | ||
return contentItems | ||
} | ||
for ((videoId, data) in response.entries) { | ||
val (newTitle, newThumbnail) = extractTitleAndThumbnail(data) | ||
val contentItem = contentItems.firstOrNull { it.url.toID() == videoId } | ||
newTitle?.let { contentItem?.title = newTitle } | ||
newThumbnail?.let { contentItem?.thumbnail = newThumbnail } | ||
} | ||
return contentItems | ||
} | ||
} | ||
|
||
/** | ||
* If enabled in the preferences, this overrides the video's thumbnail and title with the one | ||
* provided by the DeArrow project | ||
*/ | ||
@JvmName("deArrowStreamItems") | ||
suspend fun List<StreamItem>.deArrow() = DeArrowUtil.deArrowStreamItems(this) | ||
|
||
/** | ||
* If enabled in the preferences, this overrides the video's thumbnail and title with the one | ||
* provided by the DeArrow project | ||
*/ | ||
@JvmName("deArrowContentItems") | ||
suspend fun List<ContentItem>.deArrow() = DeArrowUtil.deArrowContentItems(this) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array is presorted optimally (manually approved submissions above others even if less votes). Just take the first element.
However, you still need to make sure the top value is valid, as it will return submissions at -1 votes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thanks!