Skip to content

Commit e944f81

Browse files
committed
[duplicated subtitle][YouTube] Add helper methods for parsing YouTube subtitle URL parameters.
- Add `V`, `LANG`, `TLANG` constants to `YoutubeParsingHelper` - Implement `extractVideoId()`, `extractLanguageCode()`, `extractTranslationCode()` - Add `extractQueryParam()` utility in `Utils.java`
1 parent 912a989 commit e944f81

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ private YoutubeParsingHelper() {
130130
public static final String CPN = "cpn";
131131
public static final String VIDEO_ID = "videoId";
132132

133+
/**
134+
* YouTube-specific URL query parameters
135+
*/
136+
public static final String V = "v"; // Video ID parameter in URL (e.g., v=VIDEO_ID)
137+
public static final String LANG = "lang"; // Original language parameter (e.g., lang=en)
138+
public static final String TLANG = "tlang"; // Auto-translate language (e.g., tlang=zh-CN)
139+
133140
/**
134141
* A parameter sent by official clients named {@code contentCheckOk}.
135142
*
@@ -194,6 +201,22 @@ private YoutubeParsingHelper() {
194201

195202
private static boolean consentAccepted = false;
196203

204+
// Extract the videoId (e.g., "b7vmW_5HSpE") from a YouTube subtitle URL
205+
// (e.g., .../api/timedtext?v=b7vmW_5HSpE)
206+
public static String extractVideoId(final String urlString) {
207+
return Utils.extractQueryParam(urlString, V);
208+
}
209+
210+
// Extracts the original language code (e.g., 'en') from a YouTube URL.
211+
public static String extractLanguageCode(final String urlString) {
212+
return Utils.extractQueryParam(urlString, LANG);
213+
}
214+
215+
// Extracts the 'Auto-translate' language code from a YouTube URL.
216+
public static String extractTranslationCode(final String urlString) {
217+
return Utils.extractQueryParam(urlString, TLANG);
218+
}
219+
197220
public static boolean isGoogleURL(final String url) {
198221
final String cachedUrl = extractCachedUrlIfNeeded(url);
199222
try {

extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,24 @@ public static URL stringToURL(final String url) throws MalformedURLException {
195195
}
196196
}
197197

198+
/**
199+
* Extracts the value of a query parameter from a URL string.
200+
*
201+
* @param urlString the URL to parse
202+
* @param key the query parameter key (e.g., "v", "lang", "tlang")
203+
* @return the parameter value if present, otherwise {@code null}
204+
*/
205+
public static String extractQueryParam(final String urlString,
206+
final String key) {
207+
try {
208+
final URL url = stringToURL(urlString);
209+
return getQueryValue(url, key);
210+
} catch (final Exception e) {
211+
// Invalid or malformed URL, or unexpected parsing error
212+
return null;
213+
}
214+
}
215+
198216
public static boolean isHTTP(@Nonnull final URL url) {
199217
// Make sure it's HTTP or HTTPS
200218
final String protocol = url.getProtocol();

0 commit comments

Comments
 (0)