Skip to content

Commit 24eba62

Browse files
Theta-DevAudricV
authored andcommitted
fix: extract YouTube trends from new "Videos" tab
1 parent a9ca5c4 commit 24eba62

4 files changed

Lines changed: 87 additions & 34 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeTrendingExtractor.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@
4141

4242
import java.io.IOException;
4343
import java.nio.charset.StandardCharsets;
44+
import java.util.stream.Stream;
4445

4546
import javax.annotation.Nonnull;
4647

4748
public class YoutubeTrendingExtractor extends KioskExtractor<StreamInfoItem> {
4849
private JsonObject initialData;
4950

51+
private static final String VIDEOS_TAB_PARAMS = "4gIOGgxtb3N0X3BvcHVsYXI%3D";
52+
5053
public YoutubeTrendingExtractor(final StreamingService service,
5154
final ListLinkHandler linkHandler,
5255
final String kioskId) {
@@ -60,6 +63,7 @@ public void onFetchPage(@Nonnull final Downloader downloader)
6063
final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder(getExtractorLocalization(),
6164
getExtractorContentCountry())
6265
.value("browseId", "FEtrending")
66+
.value("params", VIDEOS_TAB_PARAMS)
6367
.done())
6468
.getBytes(StandardCharsets.UTF_8);
6569
// @formatter:on
@@ -94,7 +98,10 @@ public String getName() throws ParsingException {
9498
public InfoItemsPage<StreamInfoItem> getInitialPage() throws ParsingException {
9599
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
96100
final TimeAgoParser timeAgoParser = getTimeAgoParser();
97-
final JsonObject tabContent = getTrendingTabContent();
101+
final JsonObject tab = getTrendingTab();
102+
final JsonObject tabContent = tab.getObject("content");
103+
final boolean isVideoTab = tab.getObject("endpoint").getObject("browseEndpoint")
104+
.getString("params", "").equals(VIDEOS_TAB_PARAMS);
98105

99106
if (tabContent.has("richGridRenderer")) {
100107
tabContent.getObject("richGridRenderer")
@@ -110,7 +117,7 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws ParsingException {
110117
.forEachOrdered(videoRenderer -> collector.commit(
111118
new YoutubeStreamInfoItemExtractor(videoRenderer, timeAgoParser)));
112119
} else if (tabContent.has("sectionListRenderer")) {
113-
tabContent.getObject("sectionListRenderer")
120+
final Stream<JsonObject> shelves = tabContent.getObject("sectionListRenderer")
114121
.getArray("contents")
115122
.stream()
116123
.filter(JsonObject.class::isInstance)
@@ -120,11 +127,19 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws ParsingException {
120127
.stream())
121128
.filter(JsonObject.class::isInstance)
122129
.map(JsonObject.class::cast)
123-
.map(content -> content.getObject("shelfRenderer"))
124-
// Filter Trending shorts and Recently trending sections which have a title,
125-
// contrary to normal trends
126-
.filter(shelfRenderer -> !shelfRenderer.has("title"))
127-
.flatMap(shelfRenderer -> shelfRenderer.getObject("content")
130+
.map(content -> content.getObject("shelfRenderer"));
131+
132+
final Stream<JsonObject> items;
133+
if (isVideoTab) {
134+
// The first shelf of the Videos tab contains the normal trends
135+
items = shelves.findFirst().stream();
136+
} else {
137+
// Filter Trending shorts and Recently trending sections which have a title,
138+
// contrary to normal trends
139+
items = shelves.filter(shelfRenderer -> !shelfRenderer.has("title"));
140+
}
141+
142+
items.flatMap(shelfRenderer -> shelfRenderer.getObject("content")
128143
.getObject("expandedShelfContentsRenderer")
129144
.getArray("items")
130145
.stream())
@@ -138,7 +153,7 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws ParsingException {
138153
return new InfoItemsPage<>(collector, null);
139154
}
140155

141-
private JsonObject getTrendingTabContent() throws ParsingException {
156+
private JsonObject getTrendingTab() throws ParsingException {
142157
return initialData.getObject("contents")
143158
.getObject("twoColumnBrowseResultsRenderer")
144159
.getArray("tabs")
@@ -150,7 +165,7 @@ private JsonObject getTrendingTabContent() throws ParsingException {
150165
.filter(tabRenderer -> tabRenderer.has("content"))
151166
// There should be at most one tab selected
152167
.findFirst()
153-
.orElseThrow(() -> new ParsingException("Could not get \"Now\" trending tab"))
154-
.getObject("content");
168+
.orElseThrow(() ->
169+
new ParsingException("Could not get \"Now\" or \"Videos\" trending tab"));
155170
}
156171
}

extractor/src/test/resources/org/schabi/newpipe/extractor/services/youtube/extractor/kiosk/trending/generated_mock_0.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"https://www.youtube.com"
3030
],
3131
"alt-svc": [
32-
"h3\u003d\":443\"; ma\u003d2592000,h3-29\u003d\":443\"; ma\u003d2592000,h3-Q050\u003d\":443\"; ma\u003d2592000,h3-Q046\u003d\":443\"; ma\u003d2592000,h3-Q043\u003d\":443\"; ma\u003d2592000,quic\u003d\":443\"; ma\u003d2592000; v\u003d\"46,43\""
32+
"h3\u003d\":443\"; ma\u003d2592000,h3-29\u003d\":443\"; ma\u003d2592000"
3333
],
3434
"cache-control": [
3535
"private, max-age\u003d0"
@@ -41,10 +41,10 @@
4141
"same-origin; report-to\u003d\"youtube_main\""
4242
],
4343
"date": [
44-
"Tue, 22 Nov 2022 10:40:26 GMT"
44+
"Sun, 02 Apr 2023 00:11:13 GMT"
4545
],
4646
"expires": [
47-
"Tue, 22 Nov 2022 10:40:26 GMT"
47+
"Sun, 02 Apr 2023 00:11:13 GMT"
4848
],
4949
"p3p": [
5050
"CP\u003d\"This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl\u003den-GB for more info.\""
@@ -59,9 +59,9 @@
5959
"ESF"
6060
],
6161
"set-cookie": [
62-
"YSC\u003daSSq4mC6HTI; Domain\u003d.youtube.com; Path\u003d/; Secure; HttpOnly; SameSite\u003dnone",
63-
"VISITOR_INFO1_LIVE\u003d; Domain\u003d.youtube.com; Expires\u003dWed, 26-Feb-2020 10:40:26 GMT; Path\u003d/; Secure; HttpOnly; SameSite\u003dnone",
64-
"CONSENT\u003dPENDING+953; expires\u003dThu, 21-Nov-2024 10:40:26 GMT; path\u003d/; domain\u003d.youtube.com; Secure"
62+
"YSC\u003dPYs50GAOR5M; Domain\u003d.youtube.com; Path\u003d/; Secure; HttpOnly; SameSite\u003dnone",
63+
"VISITOR_INFO1_LIVE\u003d; Domain\u003d.youtube.com; Expires\u003dMon, 06-Jul-2020 00:11:13 GMT; Path\u003d/; Secure; HttpOnly; SameSite\u003dnone",
64+
"CONSENT\u003dPENDING+445; expires\u003dTue, 01-Apr-2025 00:11:13 GMT; path\u003d/; domain\u003d.youtube.com; Secure"
6565
],
6666
"strict-transport-security": [
6767
"max-age\u003d31536000"

extractor/src/test/resources/org/schabi/newpipe/extractor/services/youtube/extractor/kiosk/trending/generated_mock_1.json

Lines changed: 6 additions & 6 deletions
Large diffs are not rendered by default.

extractor/src/test/resources/org/schabi/newpipe/extractor/services/youtube/extractor/kiosk/trending/generated_mock_2.json

Lines changed: 50 additions & 12 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)