|
1 | 1 | package org.schabi.newpipe.extractor.services.youtube.extractors; |
2 | 2 |
|
3 | | -import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.DISABLE_PRETTY_PRINT_PARAMETER; |
4 | | -import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.YOUTUBEI_V1_URL; |
5 | | -import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.addClientInfoHeaders; |
6 | | -import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.extractCookieValue; |
7 | | -import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.extractPlaylistTypeFromPlaylistId; |
8 | | -import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getKey; |
9 | | -import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getValidJsonResponseBody; |
10 | | -import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder; |
11 | | -import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING; |
12 | | -import static org.schabi.newpipe.extractor.utils.Utils.getQueryValue; |
13 | | -import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; |
14 | | -import static org.schabi.newpipe.extractor.utils.Utils.stringToURL; |
15 | | - |
16 | 3 | import com.grack.nanojson.JsonArray; |
17 | 4 | import com.grack.nanojson.JsonBuilder; |
18 | 5 | import com.grack.nanojson.JsonObject; |
19 | 6 | import com.grack.nanojson.JsonWriter; |
20 | | - |
21 | 7 | import org.schabi.newpipe.extractor.ListExtractor; |
22 | 8 | import org.schabi.newpipe.extractor.Page; |
23 | 9 | import org.schabi.newpipe.extractor.StreamingService; |
24 | 10 | import org.schabi.newpipe.extractor.downloader.Downloader; |
25 | 11 | import org.schabi.newpipe.extractor.downloader.Response; |
| 12 | +import org.schabi.newpipe.extractor.exceptions.ConsentRequiredException; |
26 | 13 | import org.schabi.newpipe.extractor.exceptions.ExtractionException; |
27 | 14 | import org.schabi.newpipe.extractor.exceptions.ParsingException; |
28 | 15 | import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; |
|
35 | 22 | import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; |
36 | 23 | import org.schabi.newpipe.extractor.utils.JsonUtils; |
37 | 24 |
|
| 25 | +import javax.annotation.Nonnull; |
| 26 | +import javax.annotation.Nullable; |
38 | 27 | import java.io.IOException; |
39 | 28 | import java.net.URL; |
40 | 29 | import java.nio.charset.StandardCharsets; |
|
43 | 32 | import java.util.Map; |
44 | 33 | import java.util.Objects; |
45 | 34 |
|
46 | | -import javax.annotation.Nonnull; |
47 | | -import javax.annotation.Nullable; |
| 35 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.DISABLE_PRETTY_PRINT_PARAMETER; |
| 36 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.YOUTUBEI_V1_URL; |
| 37 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.addYouTubeHeaders; |
| 38 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.extractCookieValue; |
| 39 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.extractPlaylistTypeFromPlaylistId; |
| 40 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getKey; |
| 41 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getValidJsonResponseBody; |
| 42 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder; |
| 43 | +import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING; |
| 44 | +import static org.schabi.newpipe.extractor.utils.Utils.getQueryValue; |
| 45 | +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; |
| 46 | +import static org.schabi.newpipe.extractor.utils.Utils.stringToURL; |
48 | 47 |
|
49 | 48 | /** |
50 | 49 | * A {@link YoutubePlaylistExtractor} for a mix (auto-generated playlist). |
@@ -89,16 +88,26 @@ public void onFetchPage(@Nonnull final Downloader downloader) |
89 | 88 | final byte[] body = JsonWriter.string(jsonBody.done()).getBytes(StandardCharsets.UTF_8); |
90 | 89 |
|
91 | 90 | final Map<String, List<String>> headers = new HashMap<>(); |
92 | | - addClientInfoHeaders(headers); |
| 91 | + // Cookie is required due to consent |
| 92 | + addYouTubeHeaders(headers); |
93 | 93 |
|
94 | 94 | final Response response = getDownloader().post(YOUTUBEI_V1_URL + "next?key=" + getKey() |
95 | 95 | + DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization); |
96 | 96 |
|
97 | 97 | initialData = JsonUtils.toJsonObject(getValidJsonResponseBody(response)); |
98 | | - playlistData = initialData.getObject("contents").getObject("twoColumnWatchNextResults") |
99 | | - .getObject("playlist").getObject("playlist"); |
| 98 | + playlistData = initialData |
| 99 | + .getObject("contents") |
| 100 | + .getObject("twoColumnWatchNextResults") |
| 101 | + .getObject("playlist") |
| 102 | + .getObject("playlist"); |
100 | 103 | if (isNullOrEmpty(playlistData)) { |
101 | | - throw new ExtractionException("Could not get playlistData"); |
| 104 | + final ExtractionException ex = new ExtractionException("Could not get playlistData"); |
| 105 | + if (!YoutubeParsingHelper.isConsentAccepted()) { |
| 106 | + throw new ConsentRequiredException( |
| 107 | + "Consent is required in some countries to view Mix playlists", |
| 108 | + ex); |
| 109 | + } |
| 110 | + throw ex; |
102 | 111 | } |
103 | 112 | cookieValue = extractCookieValue(COOKIE_NAME, response); |
104 | 113 | } |
@@ -212,7 +221,8 @@ public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException |
212 | 221 |
|
213 | 222 | final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); |
214 | 223 | final Map<String, List<String>> headers = new HashMap<>(); |
215 | | - addClientInfoHeaders(headers); |
| 224 | + // Cookie is required due to consent |
| 225 | + addYouTubeHeaders(headers); |
216 | 226 |
|
217 | 227 | final Response response = getDownloader().post(page.getUrl(), headers, page.getBody(), |
218 | 228 | getExtractorLocalization()); |
|
0 commit comments