Skip to content

Commit c8edc9a

Browse files
committed
move ProtoBuilder, add tests, fix incompatible rand call
1 parent 40059ed commit c8edc9a

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.schabi.newpipe.extractor.stream.AudioTrackType;
4949
import org.schabi.newpipe.extractor.utils.JsonUtils;
5050
import org.schabi.newpipe.extractor.utils.Parser;
51+
import org.schabi.newpipe.extractor.utils.ProtoBuilder;
5152
import org.schabi.newpipe.extractor.utils.RandomStringFromAlphabetGenerator;
5253
import org.schabi.newpipe.extractor.utils.Utils;
5354

@@ -212,7 +213,7 @@ private YoutubeParsingHelper() {
212213
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
213214

214215
/**
215-
* The device machine id for the iPhone 15, used to get 60fps with the {@code iOS} client.
216+
* The device machine id for the iPhone 16, used to get 60fps with the {@code iOS} client.
216217
*
217218
* <p>
218219
* See <a href="https://gist.github.com/adamawolf/3048717">this GitHub Gist</a> for more
@@ -222,23 +223,23 @@ private YoutubeParsingHelper() {
222223
private static final String IOS_DEVICE_MODEL = "iPhone16,2";
223224

224225
/**
225-
* Spoofing an iPhone 15 Pro Max running iOS 17.5.1 with the hardcoded version of the iOS app.
226+
* Spoofing an iPhone 16 Pro Max running iOS 18.1.0 with the hardcoded version of the iOS app.
226227
* To be used for the {@code "osVersion"} field in JSON POST requests.
227228
* <p>
228229
* The value of this field seems to use the following structure:
229230
* "iOS major version.minor version.patch version.build version", where
230231
* "patch version" is equal to 0 if it isn't set
231232
* The build version corresponding to the iOS version used can be found on
232-
* <a href="https://theapplewiki.com/wiki/Firmware/iPhone/17.x#iPhone_15_Pro_Max">
233-
* https://theapplewiki.com/wiki/Firmware/iPhone/17.x#iPhone_15_Pro_Max</a>
233+
* <a href="https://theapplewiki.com/wiki/Firmware/iPhone/18.x#iPhone_16_Pro_Max">
234+
* https://theapplewiki.com/wiki/Firmware/iPhone/18.x#iPhone_16_Pro_Max</a>
234235
* </p>
235236
*
236237
* @see #IOS_USER_AGENT_VERSION
237238
*/
238239
private static final String IOS_OS_VERSION = "18.1.0.22B83";
239240

240241
/**
241-
* Spoofing an iPhone 15 running iOS 17.5.1 with the hardcoded version of the iOS app. To be
242+
* Spoofing an iPhone 15 running iOS 18.1.0 with the hardcoded version of the iOS app. To be
242243
* used in the user agent for requests.
243244
*
244245
* @see #IOS_OS_VERSION
@@ -306,7 +307,7 @@ public static boolean isY2ubeURL(@Nonnull final URL url) {
306307
public static String randomVisitorData(final ContentCountry country) {
307308
final ProtoBuilder pbE2 = new ProtoBuilder();
308309
pbE2.string(2, "");
309-
pbE2.varint(4, numberGenerator.nextInt(1, 256));
310+
pbE2.varint(4, numberGenerator.nextInt(255) + 1);
310311

311312
final ProtoBuilder pbE = new ProtoBuilder();
312313
pbE.string(1, country.getCountryCode());
@@ -1181,10 +1182,11 @@ public static JsonBuilder<JsonObject> prepareDesktopJsonBuilder(
11811182
public static JsonBuilder<JsonObject> prepareDesktopJsonBuilder(
11821183
@Nonnull final Localization localization,
11831184
@Nonnull final ContentCountry contentCountry,
1184-
@Nullable String visitorData)
1185+
@Nullable final String visitorData)
11851186
throws IOException, ExtractionException {
1186-
if (visitorData == null) {
1187-
visitorData = randomVisitorData(contentCountry);
1187+
String vData = visitorData;
1188+
if (vData == null) {
1189+
vData = randomVisitorData(contentCountry);
11881190
}
11891191

11901192
// @formatter:off
@@ -1198,7 +1200,7 @@ public static JsonBuilder<JsonObject> prepareDesktopJsonBuilder(
11981200
.value("originalUrl", "https://www.youtube.com")
11991201
.value("platform", "DESKTOP")
12001202
.value("utcOffsetMinutes", 0)
1201-
.value("visitorData", visitorData)
1203+
.value("visitorData", vData)
12021204
.end()
12031205
.object("request")
12041206
.array("internalExperimentFlags")

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,12 +829,14 @@ public void onFetchPage(@Nonnull final Downloader downloader)
829829
// Fetching successfully the iOS player is mandatory to get streams
830830
fetchIosMobileJsonPlayer(contentCountry, localization, videoId);
831831

832+
/*
832833
try {
833834
fetchAndroidMobileJsonPlayer(contentCountry, localization, videoId);
834835
} catch (final Exception ignored) {
835836
// Ignore exceptions related to ANDROID client fetch or parsing, as it is not
836837
// compulsory to play contents
837838
}
839+
*/
838840
}
839841

840842
// The microformat JSON object of the content is only returned on the WEB client,
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.schabi.newpipe.extractor.services.youtube;
1+
package org.schabi.newpipe.extractor.utils;
22

33
import java.io.ByteArrayOutputStream;
44
import java.io.IOException;
@@ -22,22 +22,23 @@ public String toUrlencodedBase64() {
2222
return URLEncoder.encode(b64, StandardCharsets.UTF_8);
2323
}
2424

25-
private void writeVarint(long val) {
25+
private void writeVarint(final long val) {
2626
try {
2727
if (val == 0) {
2828
byteBuffer.write(new byte[]{(byte) 0});
2929
} else {
30-
while (val != 0) {
31-
byte b = (byte) (val & 0x7f);
32-
val >>= 7;
30+
long v = val;
31+
while (v != 0) {
32+
byte b = (byte) (v & 0x7f);
33+
v >>= 7;
3334

34-
if (val != 0) {
35+
if (v != 0) {
3536
b |= (byte) 0x80;
3637
}
3738
byteBuffer.write(new byte[]{b});
3839
}
3940
}
40-
} catch (IOException e) {
41+
} catch (final IOException e) {
4142
throw new RuntimeException(e);
4243
}
4344
}
@@ -64,7 +65,7 @@ public void bytes(final int field, final byte[] bytes) {
6465
writeVarint(bytes.length);
6566
try {
6667
byteBuffer.write(bytes);
67-
} catch (IOException e) {
68+
} catch (final IOException e) {
6869
throw new RuntimeException(e);
6970
}
7071
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.schabi.newpipe.extractor.utils;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class ProtoBuilderTest {
8+
@Test
9+
public void testProtoBuilder() {
10+
final ProtoBuilder pb = new ProtoBuilder();
11+
pb.varint(1, 128);
12+
pb.varint(2, 1234567890);
13+
pb.varint(3, 1234567890123456789L);
14+
pb.string(4, "Hello");
15+
pb.bytes(5, new byte[]{1, 2, 3});
16+
assertEquals("CIABENKF2MwEGJWCpu_HnoSRESIFSGVsbG8qAwECAw%3D%3D", pb.toUrlencodedBase64());
17+
}
18+
}

0 commit comments

Comments
 (0)