Skip to content

Commit

Permalink
implemented fallback client for videos unavailable to the current client
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeucelli committed Dec 30, 2024
1 parent 383ab13 commit f992145
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions src/main/java/com/github/felipeucelli/javatube/Youtube.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class Youtube {
private JSONObject signatureTimestamp = null;
private String playerJs = null;
private final boolean usePoToken;
private final boolean allowCache;

/**
* Default client: ANDROID_VR
Expand Down Expand Up @@ -88,6 +89,7 @@ public Youtube(String url, boolean usePoToken, boolean allowCache) throws Except
public Youtube(String url, String clientName, boolean usePoToken, boolean allowCache) throws Exception {
client = usePoToken ? "WEB" : clientName;
this.usePoToken = usePoToken;
this.allowCache = allowCache;
innerTube = new InnerTube(client, usePoToken, allowCache);
urlVideo = url;
watchUrl = "https://www.youtube.com/watch?v=" + videoId();
Expand Down Expand Up @@ -208,21 +210,35 @@ private JSONObject setVidInfo() throws Exception {
}

private JSONObject getVidInfo() throws Exception {
if (vidInfo == null) {
if (innerTube == null) {
vidInfo = setVidInfo();
} else {
if (innerTube.getRequireJsPlayer()) {
innerTube.updateInnerTubeContext(innerTube.getInnerTubeContext(), getSignatureTimestamp());
List<String> fallbackClients = Arrays.asList("MWEB", "IOS");
for(String client : fallbackClients) {

if (vidInfo == null) {
if (innerTube == null) {
vidInfo = setVidInfo();
} else {
if (innerTube.getRequireJsPlayer()) {
innerTube.updateInnerTubeContext(innerTube.getInnerTubeContext(), getSignatureTimestamp());
}
vidInfo = innerTube.player(videoId());
}
}
JSONObject playabilityStatus = vidInfo.getJSONObject("playabilityStatus");

if (Objects.equals(playabilityStatus.getString("status"), "UNPLAYABLE")) {
if (playabilityStatus.has("reason") && Objects.equals(playabilityStatus.getString("reason"), "This video is not available")) {
innerTube = new InnerTube(client, usePoToken, allowCache);
vidInfo = null;
System.out.println("ERRO, mudando para cliente: " + client);
}
vidInfo = innerTube.player(videoId());
}
}

return vidInfo;
}

void checkAvailability() throws Exception {
JSONObject playabilityStatus = getVidInfo().getJSONObject("playabilityStatus");

private List<String> extractAvailability(JSONObject playabilityStatus){
String status = "";
String reason = "";

Expand All @@ -237,6 +253,28 @@ void checkAvailability() throws Exception {
}
}

return Arrays.asList(status, reason);
}

void checkAvailability() throws Exception {
JSONObject playabilityStatus = getVidInfo().getJSONObject("playabilityStatus");

List<String> availability = extractAvailability(playabilityStatus);

String status = availability.get(0);
String reason = availability.get(1);

if (playabilityStatus.has("status")){
status = playabilityStatus.getString("status");

if (playabilityStatus.has("reason")){
reason = playabilityStatus.getString("reason");

} else if (playabilityStatus.has("messages")){
reason = playabilityStatus.getJSONArray("messages").getString(0);
}
}

switch (status) {
case "UNPLAYABLE" -> {
if (reason.equals("Join this channel to get access to members-only content like this video, and other exclusive perks.")) {
Expand Down

0 comments on commit f992145

Please sign in to comment.