|
| 1 | +package me.kavin.piped.utils; |
| 2 | + |
| 3 | +import lombok.RequiredArgsConstructor; |
| 4 | +import org.jetbrains.annotations.Nullable; |
| 5 | +import org.schabi.newpipe.extractor.services.youtube.PoTokenProvider; |
| 6 | +import org.schabi.newpipe.extractor.services.youtube.PoTokenResult; |
| 7 | +import rocks.kavin.reqwest4j.ReqwestUtils; |
| 8 | + |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Queue; |
| 11 | +import java.util.concurrent.*; |
| 12 | +import java.util.regex.Pattern; |
| 13 | + |
| 14 | +import static me.kavin.piped.consts.Constants.mapper; |
| 15 | + |
| 16 | +@RequiredArgsConstructor |
| 17 | +public class BgPoTokenProvider implements PoTokenProvider { |
| 18 | + |
| 19 | + private final String bgHelperUrl; |
| 20 | + |
| 21 | + private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); |
| 22 | + |
| 23 | + private String getWebVisitorData() throws Exception { |
| 24 | + var html = RequestUtils.sendGet("https://www.youtube.com").get(); |
| 25 | + var matcher = Pattern.compile("visitorData\":\"([\\w%-]+)\"").matcher(html); |
| 26 | + |
| 27 | + if (matcher.find()) { |
| 28 | + return matcher.group(1); |
| 29 | + } |
| 30 | + |
| 31 | + throw new RuntimeException("Failed to get visitor data"); |
| 32 | + } |
| 33 | + |
| 34 | + private final Queue<PoTokenResult> validPoTokens = new ConcurrentLinkedQueue<>(); |
| 35 | + |
| 36 | + private PoTokenResult getPoTokenPooled() throws Exception { |
| 37 | + PoTokenResult poToken = validPoTokens.poll(); |
| 38 | + |
| 39 | + if (poToken == null) { |
| 40 | + poToken = createWebClientPoToken(); |
| 41 | + } |
| 42 | + |
| 43 | + // if still null, return null |
| 44 | + if (poToken == null) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + // timer to insert back into queue after 10 + random seconds |
| 49 | + int delay = 10_000 + ThreadLocalRandom.current().nextInt(5000); |
| 50 | + PoTokenResult finalPoToken = poToken; |
| 51 | + scheduler.schedule(() -> validPoTokens.offer(finalPoToken), delay, TimeUnit.MILLISECONDS); |
| 52 | + |
| 53 | + return poToken; |
| 54 | + } |
| 55 | + |
| 56 | + private PoTokenResult createWebClientPoToken() throws Exception { |
| 57 | + String visitorDate = getWebVisitorData(); |
| 58 | + |
| 59 | + String poToken = ReqwestUtils.fetch(bgHelperUrl + "/generate", "POST", mapper.writeValueAsBytes(mapper.createObjectNode().put( |
| 60 | + "visitorData", visitorDate |
| 61 | + )), Map.of( |
| 62 | + "Content-Type", "application/json" |
| 63 | + )).thenApply(response -> { |
| 64 | + try { |
| 65 | + return mapper.readTree(response.body()).get("poToken").asText(); |
| 66 | + } catch (Exception e) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + }).join(); |
| 70 | + |
| 71 | + if (poToken != null) { |
| 72 | + return new PoTokenResult(visitorDate, poToken); |
| 73 | + } |
| 74 | + |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public @Nullable PoTokenResult getWebClientPoToken() { |
| 80 | + try { |
| 81 | + return getPoTokenPooled(); |
| 82 | + } catch (Exception e) { |
| 83 | + e.printStackTrace(); |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public @Nullable PoTokenResult getAndroidClientPoToken() { |
| 90 | + // TODO: allow setting from config maybe? |
| 91 | + return null; |
| 92 | + } |
| 93 | +} |
0 commit comments