1+ /*
2+ * Created by Christian Schabesberger on 02.03.16.
3+ *
4+ * Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
5+ * YoutubeParsingHelper.java is part of NewPipe Extractor.
6+ *
7+ * NewPipe Extractor is free software: you can redistribute it and/or modify
8+ * it under the terms of the GNU General Public License as published by
9+ * the Free Software Foundation, either version 3 of the License, or
10+ * (at your option) any later version.
11+ *
12+ * NewPipe Extractor is distributed in the hope that it will be useful,
13+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ * GNU General Public License for more details.
16+ *
17+ * You should have received a copy of the GNU General Public License
18+ * along with NewPipe Extractor. If not, see <https://www.gnu.org/licenses/>.
19+ */
20+
121package org .schabi .newpipe .extractor .services .youtube ;
222
323import static org .schabi .newpipe .extractor .NewPipe .getDownloader ;
727import static org .schabi .newpipe .extractor .utils .Utils .UTF_8 ;
828import static org .schabi .newpipe .extractor .utils .Utils .getStringResultFromRegexArray ;
929import static org .schabi .newpipe .extractor .utils .Utils .isNullOrEmpty ;
10- import static org .schabi .newpipe .extractor .utils .Utils .randomStringFromAlphabet ;
1130
1231import com .grack .nanojson .JsonArray ;
1332import com .grack .nanojson .JsonBuilder ;
2948import org .schabi .newpipe .extractor .stream .Description ;
3049import org .schabi .newpipe .extractor .utils .JsonUtils ;
3150import org .schabi .newpipe .extractor .utils .Parser ;
51+ import org .schabi .newpipe .extractor .utils .RandomStringFromAlphabetGenerator ;
3252import org .schabi .newpipe .extractor .utils .Utils ;
3353
3454import java .io .IOException ;
3757import java .net .URL ;
3858import java .net .URLDecoder ;
3959import java .nio .charset .StandardCharsets ;
60+ import java .security .SecureRandom ;
4061import java .time .LocalDate ;
4162import java .time .OffsetDateTime ;
4263import java .time .ZoneOffset ;
5475import javax .annotation .Nonnull ;
5576import javax .annotation .Nullable ;
5677
57- /*
58- * Created by Christian Schabesberger on 02.03.16.
59- *
60- * Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
61- * YoutubeParsingHelper.java is part of NewPipe Extractor.
62- *
63- * NewPipe Extractor is free software: you can redistribute it and/or modify
64- * it under the terms of the GNU General Public License as published by
65- * the Free Software Foundation, either version 3 of the License, or
66- * (at your option) any later version.
67- *
68- * NewPipe Extractor is distributed in the hope that it will be useful,
69- * but WITHOUT ANY WARRANTY; without even the implied warranty of
70- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
71- * GNU General Public License for more details.
72- *
73- * You should have received a copy of the GNU General Public License
74- * along with NewPipe Extractor. If not, see <https://www.gnu.org/licenses/>.
75- */
76-
7778public final class YoutubeParsingHelper {
7879
7980 private YoutubeParsingHelper () {
@@ -83,11 +84,6 @@ private YoutubeParsingHelper() {
8384 public static final String CPN = "cpn" ;
8485 public static final String VIDEO_ID = "videoId" ;
8586
86- /**
87- * Seed that will be used for video tests, in order to mock video requests.
88- */
89- private static final long SEED_FOR_VIDEOS_TESTS = 3000 ;
90-
9187 private static final String HARDCODED_CLIENT_VERSION = "2.20220114.01.00" ;
9288 private static final String HARDCODED_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8" ;
9389
@@ -122,7 +118,7 @@ private YoutubeParsingHelper() {
122118 private static final String CONTENT_PLAYBACK_NONCE_ALPHABET =
123119 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" ;
124120
125- private static Random numberGenerator = new Random ();
121+ private static Random numberGenerator = new SecureRandom ();
126122
127123 /**
128124 * {@code PENDING+} means that the user did not yet submit their choices.
@@ -606,12 +602,13 @@ public static String getClientVersion() throws IOException, ExtractionException
606602
607603 if (keyAndVersionExtracted ) {
608604 return clientVersion ;
609- } else {
610- if (areHardcodedClientVersionAndKeyValid ()) {
611- clientVersion = HARDCODED_CLIENT_VERSION ;
612- return clientVersion ;
613- }
614605 }
606+
607+ if (areHardcodedClientVersionAndKeyValid ()) {
608+ clientVersion = HARDCODED_CLIENT_VERSION ;
609+ return clientVersion ;
610+ }
611+
615612 throw new ExtractionException ("Could not get YouTube WEB client version" );
616613 }
617614
@@ -631,11 +628,11 @@ public static String getKey() throws IOException, ExtractionException {
631628
632629 if (keyAndVersionExtracted ) {
633630 return key ;
634- } else {
635- if ( areHardcodedClientVersionAndKeyValid ()) {
636- key = HARDCODED_KEY ;
637- return key ;
638- }
631+ }
632+
633+ if ( areHardcodedClientVersionAndKeyValid ()) {
634+ key = HARDCODED_KEY ;
635+ return key ;
639636 }
640637
641638 // The ANDROID API key is also valid with the WEB client so return it if we couldn't
@@ -1508,7 +1505,8 @@ public static String unescapeDocument(@Nonnull final String doc) {
15081505 */
15091506 @ Nonnull
15101507 public static String generateContentPlaybackNonce () {
1511- return randomStringFromAlphabet (CONTENT_PLAYBACK_NONCE_ALPHABET , 16 );
1508+ return RandomStringFromAlphabetGenerator .generate (
1509+ CONTENT_PLAYBACK_NONCE_ALPHABET , 16 , numberGenerator );
15121510 }
15131511
15141512 /**
@@ -1524,23 +1522,7 @@ public static String generateContentPlaybackNonce() {
15241522 */
15251523 @ Nonnull
15261524 public static String generateTParameter () {
1527- return randomStringFromAlphabet (CONTENT_PLAYBACK_NONCE_ALPHABET , 12 );
1528- }
1529-
1530- /**
1531- * Set the seed for video tests.
1532- *
1533- * <p>
1534- * This seed will be used to generate the same {@code t} and {@code cpn} values between
1535- * different execution of tests so mocks can be used for stream tests.
1536- * </p>
1537- *
1538- * <p>
1539- * This method will call {@link Utils#setSecureRandomSeed(long)} with the
1540- * {@link #SEED_FOR_VIDEOS_TESTS value}.
1541- * </p>
1542- */
1543- public static void setSeedForVideoTests () {
1544- Utils .setSecureRandomSeed (SEED_FOR_VIDEOS_TESTS );
1525+ return RandomStringFromAlphabetGenerator .generate (
1526+ CONTENT_PLAYBACK_NONCE_ALPHABET , 12 , numberGenerator );
15451527 }
15461528}
0 commit comments