-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07785f1
commit da72483
Showing
5 changed files
with
112 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package me.kavin.piped.utils; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.schabi.newpipe.extractor.services.youtube.PoTokenProvider; | ||
import org.schabi.newpipe.extractor.services.youtube.PoTokenResult; | ||
import rocks.kavin.reqwest4j.ReqwestUtils; | ||
|
||
import java.util.Map; | ||
import java.util.Queue; | ||
import java.util.concurrent.*; | ||
import java.util.regex.Pattern; | ||
|
||
import static me.kavin.piped.consts.Constants.mapper; | ||
|
||
@RequiredArgsConstructor | ||
public class BgPoTokenProvider implements PoTokenProvider { | ||
|
||
private final String bgHelperUrl; | ||
|
||
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | ||
|
||
private String getWebVisitorData() throws Exception { | ||
var html = RequestUtils.sendGet("https://www.youtube.com").get(); | ||
var matcher = Pattern.compile("visitorData\":\"([\\w%-]+)\"").matcher(html); | ||
|
||
if (matcher.find()) { | ||
return matcher.group(1); | ||
} | ||
|
||
throw new RuntimeException("Failed to get visitor data"); | ||
} | ||
|
||
private final Queue<PoTokenResult> validPoTokens = new ConcurrentLinkedQueue<>(); | ||
|
||
private PoTokenResult getPoTokenPooled() throws Exception { | ||
PoTokenResult poToken = validPoTokens.poll(); | ||
|
||
if (poToken == null) { | ||
poToken = createWebClientPoToken(); | ||
} | ||
|
||
// if still null, return null | ||
if (poToken == null) { | ||
return null; | ||
} | ||
|
||
// timer to insert back into queue after 10 + random seconds | ||
int delay = 10_000 + ThreadLocalRandom.current().nextInt(5000); | ||
PoTokenResult finalPoToken = poToken; | ||
scheduler.schedule(() -> validPoTokens.offer(finalPoToken), delay, TimeUnit.MILLISECONDS); | ||
|
||
return poToken; | ||
} | ||
|
||
private PoTokenResult createWebClientPoToken() throws Exception { | ||
String visitorDate = getWebVisitorData(); | ||
|
||
String poToken = ReqwestUtils.fetch(bgHelperUrl + "/generate", "POST", mapper.writeValueAsBytes(mapper.createObjectNode().put( | ||
"visitorData", visitorDate | ||
)), Map.of( | ||
"Content-Type", "application/json" | ||
)).thenApply(response -> { | ||
try { | ||
return mapper.readTree(response.body()).get("poToken").asText(); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
}).join(); | ||
|
||
if (poToken != null) { | ||
return new PoTokenResult(visitorDate, poToken); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public @Nullable PoTokenResult getWebClientPoToken() { | ||
try { | ||
return getPoTokenPooled(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public @Nullable PoTokenResult getAndroidClientPoToken() { | ||
// TODO: allow setting from config maybe? | ||
return null; | ||
} | ||
} |
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