Skip to content

Commit eda1b6e

Browse files
committed
Use Page.body for YoutubePlaylistExtractor
1 parent 3af26a2 commit eda1b6e

22 files changed

Lines changed: 147 additions & 151 deletions

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.schabi.newpipe.extractor.services.youtube;
22

33
import com.grack.nanojson.JsonArray;
4+
import com.grack.nanojson.JsonBuilder;
45
import com.grack.nanojson.JsonObject;
56
import com.grack.nanojson.JsonParser;
67
import com.grack.nanojson.JsonParserException;
@@ -676,6 +677,19 @@ public static JsonArray getJsonResponse(final Page page, final Localization loca
676677
return JsonUtils.toJsonArray(getValidJsonResponseBody(response));
677678
}
678679

680+
public static JsonBuilder<JsonObject> prepareJsonBuilder()
681+
throws IOException, ExtractionException {
682+
// @formatter:off
683+
return JsonObject.builder()
684+
.object("context")
685+
.object("client")
686+
.value("clientName", "1")
687+
.value("clientVersion", getClientVersion())
688+
.end()
689+
.end();
690+
// @formatter:on
691+
}
692+
679693
/**
680694
* Shared alert detection function, multiple endpoints return the error similarly structured.
681695
* <p>

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor;
2121
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
2222
import org.schabi.newpipe.extractor.stream.StreamType;
23+
import org.schabi.newpipe.extractor.utils.JsonUtils;
2324
import org.schabi.newpipe.extractor.utils.Utils;
2425

2526
import java.io.IOException;
@@ -28,13 +29,12 @@
2829
import javax.annotation.Nullable;
2930

3031
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.fixThumbnailUrl;
31-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getClientVersion;
3232
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonResponse;
3333
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getKey;
3434
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
3535
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
3636
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getValidJsonResponseBody;
37-
import static org.schabi.newpipe.extractor.utils.JsonUtils.toJsonObject;
37+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareJsonBuilder;
3838
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
3939
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
4040

@@ -217,25 +217,11 @@ public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException
217217
throw new IllegalArgumentException("Page doesn't contain an URL");
218218
}
219219

220-
// @formatter:off
221-
byte[] json = JsonWriter.string()
222-
.object()
223-
.object("context")
224-
.object("client")
225-
.value("clientName", "1")
226-
.value("clientVersion", getClientVersion())
227-
.end()
228-
.end()
229-
.value("continuation", page.getId())
230-
.end()
231-
.done()
232-
.getBytes(UTF_8);
233-
// @formatter:on
234-
235220
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
236-
final Response response = getDownloader().post(page.getUrl(), null, json, getExtractorLocalization());
237221

238-
final JsonObject ajaxJson = toJsonObject(getValidJsonResponseBody(response));
222+
final Response response = getDownloader().post(page.getUrl(), null, page.getBody(),
223+
getExtractorLocalization());
224+
final JsonObject ajaxJson = JsonUtils.toJsonObject(getValidJsonResponseBody(response));
239225

240226
final JsonArray continuation = ajaxJson.getArray("onResponseReceivedActions")
241227
.getObject(0)
@@ -259,9 +245,15 @@ private Page getNextPageFrom(final JsonArray contents) throws IOException, Extra
259245
.getObject("continuationEndpoint")
260246
.getObject("continuationCommand")
261247
.getString("token");
248+
249+
final byte[] body = JsonWriter.string(prepareJsonBuilder()
250+
.value("continuation", continuation)
251+
.done())
252+
.getBytes(UTF_8);
253+
262254
return new Page(
263255
"https://www.youtube.com/youtubei/v1/browse?key=" + getKey(),
264-
continuation);
256+
body);
265257
} else {
266258
return null;
267259
}

extractor/src/test/resources/org/schabi/newpipe/extractor/services/youtube/extractor/playlist/TimelessPopHits/generated_mock_0.json

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

extractor/src/test/resources/org/schabi/newpipe/extractor/services/youtube/extractor/playlist/TimelessPopHits/generated_mock_1.json

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

extractor/src/test/resources/org/schabi/newpipe/extractor/services/youtube/extractor/playlist/TimelessPopHits/generated_mock_2.json

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

extractor/src/test/resources/org/schabi/newpipe/extractor/services/youtube/extractor/playlist/TimelessPopHits/generated_mock_3.json

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

extractor/src/test/resources/org/schabi/newpipe/extractor/services/youtube/extractor/playlist/continuations/generated_mock_0.json

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

extractor/src/test/resources/org/schabi/newpipe/extractor/services/youtube/extractor/playlist/continuations/generated_mock_1.json

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

extractor/src/test/resources/org/schabi/newpipe/extractor/services/youtube/extractor/playlist/continuations/generated_mock_2.json

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

extractor/src/test/resources/org/schabi/newpipe/extractor/services/youtube/extractor/playlist/continuations/generated_mock_3.json

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

0 commit comments

Comments
 (0)