|
| 1 | +package app.revanced.integrations.syncforreddit; |
| 2 | + |
| 3 | +import android.util.Pair; |
| 4 | +import androidx.annotation.Nullable; |
| 5 | +import org.w3c.dom.Element; |
| 6 | +import org.xml.sax.SAXException; |
| 7 | + |
| 8 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 9 | +import javax.xml.parsers.ParserConfigurationException; |
| 10 | +import java.io.ByteArrayInputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Collections; |
| 14 | + |
| 15 | +/** |
| 16 | + * @noinspection unused |
| 17 | + */ |
| 18 | +public class FixRedditVideoDownloadPatch { |
| 19 | + private static @Nullable Pair<Integer, String> getBestMpEntry(Element element) { |
| 20 | + var representations = element.getElementsByTagName("Representation"); |
| 21 | + var entries = new ArrayList<Pair<Integer, String>>(); |
| 22 | + |
| 23 | + for (int i = 0; i < representations.getLength(); i++) { |
| 24 | + Element representation = (Element) representations.item(i); |
| 25 | + var bandwidthStr = representation.getAttribute("bandwidth"); |
| 26 | + try { |
| 27 | + var bandwidth = Integer.parseInt(bandwidthStr); |
| 28 | + var baseUrl = representation.getElementsByTagName("BaseURL").item(0); |
| 29 | + if (baseUrl != null) { |
| 30 | + entries.add(new Pair<>(bandwidth, baseUrl.getTextContent())); |
| 31 | + } |
| 32 | + } catch (NumberFormatException ignored) { |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + if (entries.isEmpty()) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + Collections.sort(entries, (e1, e2) -> e2.first - e1.first); |
| 41 | + return entries.get(0); |
| 42 | + } |
| 43 | + |
| 44 | + private static String[] parse(byte[] data) throws ParserConfigurationException, IOException, SAXException { |
| 45 | + var adaptionSets = DocumentBuilderFactory |
| 46 | + .newInstance() |
| 47 | + .newDocumentBuilder() |
| 48 | + .parse(new ByteArrayInputStream(data)) |
| 49 | + .getElementsByTagName("AdaptationSet"); |
| 50 | + |
| 51 | + String videoUrl = null; |
| 52 | + String audioUrl = null; |
| 53 | + |
| 54 | + for (int i = 0; i < adaptionSets.getLength(); i++) { |
| 55 | + Element element = (Element) adaptionSets.item(i); |
| 56 | + var contentType = element.getAttribute("contentType"); |
| 57 | + var bestEntry = getBestMpEntry(element); |
| 58 | + if (bestEntry == null) continue; |
| 59 | + |
| 60 | + if (contentType.equalsIgnoreCase("video")) { |
| 61 | + videoUrl = bestEntry.second; |
| 62 | + } else if (contentType.equalsIgnoreCase("audio")) { |
| 63 | + audioUrl = bestEntry.second; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return new String[]{videoUrl, audioUrl}; |
| 68 | + } |
| 69 | + |
| 70 | + public static String[] getLinks(byte[] data) { |
| 71 | + try { |
| 72 | + return parse(data); |
| 73 | + } catch (ParserConfigurationException | IOException | SAXException e) { |
| 74 | + return new String[]{null, null}; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments