Skip to content

Commit a857684

Browse files
committed
Apply changes in YoutubeStreamExtractor
Extract post live DVR streams as post live streams instead of live streams. A new class has been in order to improve code: ItagInfo, which stores an itag, the content (URL) extracted and if its an URL or not. A functional interface has been added in order to abstract the stream building: StreamBuilderHelper. Also add the cver parameter added by the desktop web client on the corresponding streams (a new method has been added in YoutubeParsingHelper to check this and another for Android streams). Some code in these classes has been also refactored/improved/optimized.
1 parent 4330b5f commit a857684

3 files changed

Lines changed: 308 additions & 145 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.schabi.newpipe.extractor.services.youtube;
2+
3+
import javax.annotation.Nonnull;
4+
import java.io.Serializable;
5+
6+
public final class ItagInfo implements Serializable {
7+
8+
@Nonnull
9+
private final String content;
10+
@Nonnull
11+
private final ItagItem itagItem;
12+
private boolean isUrl;
13+
14+
public ItagInfo(@Nonnull final String content,
15+
@Nonnull final ItagItem itagItem) {
16+
this.content = content;
17+
this.itagItem = itagItem;
18+
}
19+
20+
public void setIsUrl(final boolean isUrl) {
21+
this.isUrl = isUrl;
22+
}
23+
24+
@Nonnull
25+
public String getContent() {
26+
return content;
27+
}
28+
29+
@Nonnull
30+
public ItagItem getItagItem() {
31+
return itagItem;
32+
}
33+
34+
public boolean getIsUrl() {
35+
return isUrl;
36+
}
37+
}

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import java.util.Objects;
7272
import java.util.Optional;
7373
import java.util.Random;
74+
import java.util.regex.Pattern;
7475

7576
import javax.annotation.Nonnull;
7677
import javax.annotation.Nullable;
@@ -246,6 +247,11 @@ private YoutubeParsingHelper() {
246247
private static final String FEED_BASE_CHANNEL_ID =
247248
"https://www.youtube.com/feeds/videos.xml?channel_id=";
248249
private static final String FEED_BASE_USER = "https://www.youtube.com/feeds/videos.xml?user=";
250+
private static final Pattern C_WEB_PATTERN = Pattern.compile("&c=WEB");
251+
private static final Pattern C_TVHTML5_SIMPLY_EMBEDDED_PLAYER_PATTERN =
252+
Pattern.compile("&c=TVHTML5_SIMPLY_EMBEDDED_PLAYER");
253+
private static final Pattern C_ANDROID_PATTERN = Pattern.compile("&c=ANDROID");
254+
private static final Pattern C_IOS_PATTERN = Pattern.compile("&c=IOS");
249255

250256
private static boolean isGoogleURL(final String url) {
251257
final String cachedUrl = extractCachedUrlIfNeeded(url);
@@ -1190,7 +1196,7 @@ public static JsonBuilder<JsonObject> prepareTvHtml5EmbedJsonBuilder(
11901196
@Nonnull final Localization localization,
11911197
@Nonnull final ContentCountry contentCountry,
11921198
@Nonnull final String videoId) {
1193-
// @formatter:off
1199+
// @formatter:off
11941200
return JsonObject.builder()
11951201
.object("context")
11961202
.object("client")
@@ -1588,4 +1594,46 @@ public static String generateTParameter() {
15881594
return RandomStringFromAlphabetGenerator.generate(
15891595
CONTENT_PLAYBACK_NONCE_ALPHABET, 12, numberGenerator);
15901596
}
1597+
1598+
/**
1599+
* Check if the streaming URL is a URL from the YouTube {@code WEB} client.
1600+
*
1601+
* @param url the streaming URL on which check if it's a {@code WEB} streaming URL.
1602+
* @return true if it's a {@code WEB} streaming URL, false otherwise
1603+
*/
1604+
public static boolean isWebStreamingUrl(@Nonnull final String url) {
1605+
return Parser.isMatch(C_WEB_PATTERN, url);
1606+
}
1607+
1608+
/**
1609+
* Check if the streaming URL is a URL from the YouTube {@code TVHTML5_SIMPLY_EMBEDDED_PLAYER}
1610+
* client.
1611+
*
1612+
* @param url the streaming URL on which check if it's a {@code TVHTML5_SIMPLY_EMBEDDED_PLAYER}
1613+
* streaming URL.
1614+
* @return true if it's a {@code TVHTML5_SIMPLY_EMBEDDED_PLAYER} streaming URL, false otherwise
1615+
*/
1616+
public static boolean isTvHtml5SimplyEmbeddedPlayerStreamingUrl(@Nonnull final String url) {
1617+
return Parser.isMatch(C_TVHTML5_SIMPLY_EMBEDDED_PLAYER_PATTERN, url);
1618+
}
1619+
1620+
/**
1621+
* Check if the streaming URL is a URL from the YouTube {@code ANDROID} client.
1622+
*
1623+
* @param url the streaming URL on which check if it's a {@code ANDROID} streaming URL.
1624+
* @return true if it's a {@code ANDROID} streaming URL, false otherwise
1625+
*/
1626+
public static boolean isAndroidStreamingUrl(@Nonnull final String url) {
1627+
return Parser.isMatch(C_ANDROID_PATTERN, url);
1628+
}
1629+
1630+
/**
1631+
* Check if the streaming URL is a URL from the YouTube {@code IOS} client.
1632+
*
1633+
* @param url the streaming URL on which check if it's a {@code IOS} streaming URL.
1634+
* @return true if it's a {@code IOS} streaming URL, false otherwise
1635+
*/
1636+
public static boolean isIosStreamingUrl(@Nonnull final String url) {
1637+
return Parser.isMatch(C_IOS_PATTERN, url);
1638+
}
15911639
}

0 commit comments

Comments
 (0)