Skip to content
This repository has been archived by the owner on Oct 26, 2024. It is now read-only.

feat(Sync for Reddit): Add Fix video downloads patch #710

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package app.revanced.integrations.syncforreddit;

import app.revanced.integrations.syncforreddit.internal.RedditVideoPlaylistParser;

/**
* @noinspection unused
*/
public class FixRedditVideoDownloadPatch {

public static String[] getLinks(byte[] data) {
return RedditVideoPlaylistParser.INSTANCE.parse(data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package app.revanced.integrations.syncforreddit.internal

import org.w3c.dom.Element
import java.io.ByteArrayInputStream
import javax.xml.parsers.DocumentBuilderFactory

object RedditVideoPlaylistParser {
1fexd marked this conversation as resolved.
Show resolved Hide resolved
class MpdEntry(val bandwidth: Int, val baseUrl: String)

private fun getBestMpEntry(element: Element): MpdEntry? {
val representations = element.getElementsByTagName("Representation")
val entries = mutableListOf<MpdEntry>()
for (i in 0 until representations.length) {
val representation = representations.item(i) as Element
val bandwidth = representation.getAttribute("bandwidth")?.toIntOrNull()
val baseUrl = representation.getElementsByTagName("BaseURL").item(0)

if (bandwidth != null && baseUrl != null) {
entries.add(MpdEntry(bandwidth, baseUrl.textContent))
}
}

return entries.maxByOrNull { it.bandwidth }
}

fun parse(data: ByteArray): Array<String?> {
val adaptionSets = DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.parse(ByteArrayInputStream(data))
.getElementsByTagName("AdaptationSet")

var videoUrl: String? = null
var audioUrl: String? = null

for (i in 0 until adaptionSets.length) {
val element = adaptionSets.item(i) as Element
val contentType = element.getAttribute("contentType")
val bestEntry = getBestMpEntry(element) ?: continue

when (contentType) {
"video" -> videoUrl = bestEntry.baseUrl
"audio" -> audioUrl = bestEntry.baseUrl
}
}

return arrayOf(videoUrl, audioUrl)
}
}