Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ private YoutubeParsingHelper() {
public static final String CPN = "cpn";
public static final String VIDEO_ID = "videoId";

/**
* YouTube-specific URL query parameters
*/
public static final String V = "v"; // Video ID parameter in URL (e.g., v=VIDEO_ID)
public static final String LANG = "lang"; // Original language parameter (e.g., lang=en)
public static final String TLANG = "tlang"; // Auto-translate language (e.g., tlang=zh-CN)

/**
* A parameter sent by official clients named {@code contentCheckOk}.
*
Expand Down Expand Up @@ -194,6 +201,22 @@ private YoutubeParsingHelper() {

private static boolean consentAccepted = false;

// Extract the videoId (e.g., "b7vmW_5HSpE") from a YouTube subtitle URL
// (e.g., .../api/timedtext?v=b7vmW_5HSpE)
public static String extractVideoId(final String urlString) {
return Utils.extractQueryParam(urlString, V);
}

// Extracts the original language code (e.g., 'en') from a YouTube URL.
public static String extractLanguageCode(final String urlString) {
return Utils.extractQueryParam(urlString, LANG);
}

// Extracts the 'Auto-translate' language code from a YouTube URL.
public static String extractTranslationCode(final String urlString) {
return Utils.extractQueryParam(urlString, TLANG);
}

public static boolean isGoogleURL(final String url) {
final String cachedUrl = extractCachedUrlIfNeeded(url);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ public static URL stringToURL(final String url) throws MalformedURLException {
}
}

/**
* Extracts the value of a query parameter from a URL string.
*
* @param urlString the URL to parse
* @param key the query parameter key (e.g., "v", "lang", "tlang")
* @return the parameter value if present, otherwise {@code null}
*/
public static String extractQueryParam(final String urlString,
final String key) {
try {
final URL url = stringToURL(urlString);
return getQueryValue(url, key);
} catch (final Exception e) {
// Invalid or malformed URL, or unexpected parsing error
return null;
}
}

public static boolean isHTTP(@Nonnull final URL url) {
// Make sure it's HTTP or HTTPS
final String protocol = url.getProtocol();
Expand Down