Skip to content

Commit

Permalink
Added poToken verification for WEB clients
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeucelli committed Aug 5, 2024
1 parent 8e3e295 commit d0dae2c
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/main/java/com/github/felipeucelli/javatube/Youtube.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public class Youtube {
private final String urlVideo;
private final String watchUrl;
private InnerTube innerTube = null;
private String client = null;
private JSONObject vidInfo = null;
private int poTokenAttempts = 1;
private String html = null;
private String js = null;
private JSONObject ytCfg = null;
Expand Down Expand Up @@ -50,7 +52,8 @@ public Youtube(String url) throws Exception {
* MEDIA_CONNECT
* */
public Youtube(String url, String clientName) throws Exception {
innerTube = new InnerTube(clientName);
client = clientName;
innerTube = new InnerTube(client);
urlVideo = url;
watchUrl = "https://www.youtube.com/watch?v=" + videoId();
}
Expand Down Expand Up @@ -183,8 +186,49 @@ private JSONObject getVidInfo() throws Exception {
return vidInfo;
}

void checkPoToken() throws Exception {
if(innerTube == null || client.contains("WEB")){
// https://github.com/yt-dlp/yt-dlp/pull/10456
List<String> poTokenExperiments = List.of("51217476", "51217102");

String value = "";

for (int i = 0; i < vidInfo.getJSONObject("responseContext").getJSONArray("serviceTrackingParams").length(); i++){
JSONObject service = vidInfo.getJSONObject("responseContext").getJSONArray("serviceTrackingParams").getJSONObject(i);
if (service.has("service")){
JSONArray params = service.getJSONArray("params");
for (int j = 0; j < params.length(); j++){
if (Objects.equals(params.getJSONObject(j).getString("key"), "e")){
value = params.getJSONObject(j).getString("value");
break;
}
}
}
}

for (String po : poTokenExperiments){
if (value.contains(po)){
if(poTokenAttempts <= 3){
System.out.print("API returned broken formats (poToken experiment detected). ");
System.out.println("Trying again " + poTokenAttempts + "/3.");
vidInfo = null;
poTokenAttempts += 1;
checkAvailability();
break;
}
System.out.println("Trying ANDROID_TESTSUITE client.");
innerTube = new InnerTube("ANDROID_TESTSUITE");
vidInfo = null;
break;
}
}
}

}

void checkAvailability() throws Exception {
JSONObject playabilityStatus = getVidInfo().getJSONObject("playabilityStatus");
checkPoToken();
String status = "";
String reason = "";

Expand Down

0 comments on commit d0dae2c

Please sign in to comment.