|
| 1 | +package org.schabi.newpipe.extractor.services.youtube; |
| 2 | + |
| 3 | +import com.grack.nanojson.JsonObject; |
| 4 | +import com.grack.nanojson.JsonWriter; |
| 5 | +import org.schabi.newpipe.extractor.exceptions.ExtractionException; |
| 6 | +import org.schabi.newpipe.extractor.localization.ContentCountry; |
| 7 | +import org.schabi.newpipe.extractor.localization.Localization; |
| 8 | +import org.schabi.newpipe.extractor.utils.JsonUtils; |
| 9 | + |
| 10 | +import javax.annotation.Nonnull; |
| 11 | +import java.io.IOException; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | + |
| 14 | +import static org.schabi.newpipe.extractor.NewPipe.getDownloader; |
| 15 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.CONTENT_CHECK_OK; |
| 16 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.CPN; |
| 17 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.DISABLE_PRETTY_PRINT_PARAMETER; |
| 18 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.RACY_CHECK_OK; |
| 19 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.VIDEO_ID; |
| 20 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.YOUTUBEI_V1_URL; |
| 21 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.generateTParameter; |
| 22 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonAndroidPostResponse; |
| 23 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonIosPostResponse; |
| 24 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getValidJsonResponseBody; |
| 25 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getYouTubeHeaders; |
| 26 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareAndroidMobileJsonBuilder; |
| 27 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder; |
| 28 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareIosMobileJsonBuilder; |
| 29 | + |
| 30 | +public final class YoutubeStreamHelper { |
| 31 | + |
| 32 | + private static final String STREAMING_DATA = "streamingData"; |
| 33 | + private static final String PLAYER = "player"; |
| 34 | + private static final String SERVICE_INTEGRITY_DIMENSIONS = "serviceIntegrityDimensions"; |
| 35 | + private static final String PO_TOKEN = "poToken"; |
| 36 | + |
| 37 | + private YoutubeStreamHelper() { |
| 38 | + } |
| 39 | + |
| 40 | + @Nonnull |
| 41 | + public static JsonObject getWebMetadataPlayerResponse( |
| 42 | + @Nonnull final Localization localization, |
| 43 | + @Nonnull final ContentCountry contentCountry, |
| 44 | + @Nonnull final String videoId) throws IOException, ExtractionException { |
| 45 | + final byte[] body = JsonWriter.string( |
| 46 | + prepareDesktopJsonBuilder(localization, contentCountry) |
| 47 | + .value(VIDEO_ID, videoId) |
| 48 | + .value(CONTENT_CHECK_OK, true) |
| 49 | + .value(RACY_CHECK_OK, true) |
| 50 | + .done()) |
| 51 | + .getBytes(StandardCharsets.UTF_8); |
| 52 | + final String url = YOUTUBEI_V1_URL + "player" + "?" + DISABLE_PRETTY_PRINT_PARAMETER |
| 53 | + + "&$fields=microformat,playabilityStatus,storyboards,videoDetails"; |
| 54 | + |
| 55 | + return JsonUtils.toJsonObject(getValidJsonResponseBody( |
| 56 | + getDownloader().postWithContentTypeJson( |
| 57 | + url, getYouTubeHeaders(), body, localization))); |
| 58 | + } |
| 59 | + |
| 60 | + @Nonnull |
| 61 | + public static JsonObject getWebFullPlayerResponse( |
| 62 | + @Nonnull final Localization localization, |
| 63 | + @Nonnull final ContentCountry contentCountry, |
| 64 | + @Nonnull final String videoId, |
| 65 | + @Nonnull final PoTokenResult webPoTokenResult) throws IOException, ExtractionException { |
| 66 | + final byte[] body = JsonWriter.string( |
| 67 | + prepareDesktopJsonBuilder(localization, contentCountry, webPoTokenResult.visitorData) |
| 68 | + .value(VIDEO_ID, videoId) |
| 69 | + .value(CONTENT_CHECK_OK, true) |
| 70 | + .value(RACY_CHECK_OK, true) |
| 71 | + .object(SERVICE_INTEGRITY_DIMENSIONS) |
| 72 | + .value(PO_TOKEN, webPoTokenResult.poToken) |
| 73 | + .end() |
| 74 | + .done()) |
| 75 | + .getBytes(StandardCharsets.UTF_8); |
| 76 | + final String url = YOUTUBEI_V1_URL + "player" + "?" + DISABLE_PRETTY_PRINT_PARAMETER; |
| 77 | + |
| 78 | + return JsonUtils.toJsonObject(getValidJsonResponseBody( |
| 79 | + getDownloader().postWithContentTypeJson( |
| 80 | + url, getYouTubeHeaders(), body, localization))); |
| 81 | + } |
| 82 | + |
| 83 | + public static JsonObject getAndroidPlayerResponse(@Nonnull final ContentCountry contentCountry, |
| 84 | + @Nonnull final Localization localization, |
| 85 | + @Nonnull final String videoId, |
| 86 | + @Nonnull final String androidCpn, |
| 87 | + @Nonnull final PoTokenResult androidPoTokenResult) |
| 88 | + throws IOException, ExtractionException { |
| 89 | + final byte[] mobileBody = JsonWriter.string( |
| 90 | + prepareAndroidMobileJsonBuilder(localization, contentCountry, androidPoTokenResult.visitorData) |
| 91 | + .value(VIDEO_ID, videoId) |
| 92 | + .value(CPN, androidCpn) |
| 93 | + .value(CONTENT_CHECK_OK, true) |
| 94 | + .value(RACY_CHECK_OK, true) |
| 95 | + .object(SERVICE_INTEGRITY_DIMENSIONS) |
| 96 | + .value(PO_TOKEN, androidPoTokenResult.poToken) |
| 97 | + .end() |
| 98 | + .done()) |
| 99 | + .getBytes(StandardCharsets.UTF_8); |
| 100 | + |
| 101 | + return getJsonAndroidPostResponse( |
| 102 | + "player", |
| 103 | + mobileBody, |
| 104 | + localization, |
| 105 | + "&t=" + generateTParameter() + "&id=" + videoId); |
| 106 | + } |
| 107 | + |
| 108 | + public static JsonObject getAndroidReelPlayerResponse(@Nonnull final ContentCountry contentCountry, |
| 109 | + @Nonnull final Localization localization, |
| 110 | + @Nonnull final String videoId, |
| 111 | + @Nonnull final String androidCpn) |
| 112 | + throws IOException, ExtractionException { |
| 113 | + final byte[] mobileBody = JsonWriter.string( |
| 114 | + prepareAndroidMobileJsonBuilder(localization, contentCountry, null) |
| 115 | + .object("playerRequest") |
| 116 | + .value(VIDEO_ID, videoId) |
| 117 | + .end() |
| 118 | + .value("disablePlayerResponse", false) |
| 119 | + .value(VIDEO_ID, videoId) |
| 120 | + .value(CPN, androidCpn) |
| 121 | + .value(CONTENT_CHECK_OK, true) |
| 122 | + .value(RACY_CHECK_OK, true) |
| 123 | + .done()) |
| 124 | + .getBytes(StandardCharsets.UTF_8); |
| 125 | + |
| 126 | + final JsonObject androidPlayerResponse = getJsonAndroidPostResponse( |
| 127 | + "reel/reel_item_watch", |
| 128 | + mobileBody, |
| 129 | + localization, |
| 130 | + "&t=" + generateTParameter() + "&id=" + videoId + "&$fields=playerResponse"); |
| 131 | + |
| 132 | + return androidPlayerResponse.getObject("playerResponse"); |
| 133 | + } |
| 134 | + |
| 135 | + public static JsonObject getIosPlayerResponse(@Nonnull final ContentCountry contentCountry, |
| 136 | + @Nonnull final Localization localization, |
| 137 | + @Nonnull final String videoId, |
| 138 | + @Nonnull final String iosCpn) |
| 139 | + throws IOException, ExtractionException { |
| 140 | + final byte[] mobileBody = JsonWriter.string( |
| 141 | + prepareIosMobileJsonBuilder(localization, contentCountry) |
| 142 | + .value(VIDEO_ID, videoId) |
| 143 | + .value(CPN, iosCpn) |
| 144 | + .value(CONTENT_CHECK_OK, true) |
| 145 | + .value(RACY_CHECK_OK, true) |
| 146 | + .done()) |
| 147 | + .getBytes(StandardCharsets.UTF_8); |
| 148 | + |
| 149 | + return getJsonIosPostResponse(PLAYER, |
| 150 | + mobileBody, localization, "&t=" + generateTParameter() |
| 151 | + + "&id=" + videoId); |
| 152 | + } |
| 153 | +} |
0 commit comments