Skip to content

Commit 9a29f9e

Browse files
Use the new URL encode/decode methods introduced in Java 10 (#1196)
* Use Java 10 URLDecoder/URLEncoder methods * Simplify compatParseMap
1 parent 592f159 commit 9a29f9e

14 files changed

Lines changed: 69 additions & 150 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ If you're using Gradle, you could add NewPipe Extractor as a dependency with the
2121
-dontwarn org.mozilla.javascript.tools.**
2222
```
2323

24-
**Note:** To use NewPipe Extractor in Android projects with a `minSdk` below 26, [API desugaring](https://developer.android.com/studio/write/java8-support#library-desugaring) is required. If the `minSdk` is below 19, the `desugar_jdk_libs_nio` artifact is required, which requires Android Gradle Plugin (AGP) version 7.4.0.
24+
**Note:** To use NewPipe Extractor in Android projects with a `minSdk` below 33, [core library desugaring](https://developer.android.com/studio/write/java8-support#library-desugaring) with the `desugar_jdk_libs_nio` artifact is required.
2525

2626
### Testing changes
2727

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/linkHandler/BandcampSearchQueryHandlerFactory.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
99
import org.schabi.newpipe.extractor.utils.Utils;
1010

11-
import java.io.UnsupportedEncodingException;
1211
import java.util.List;
1312

1413
public final class BandcampSearchQueryHandlerFactory extends SearchQueryHandlerFactory {
@@ -28,10 +27,6 @@ public String getUrl(final String query,
2827
final List<String> contentFilter,
2928
final String sortFilter)
3029
throws ParsingException, UnsupportedOperationException {
31-
try {
32-
return BASE_URL + "/search?q=" + Utils.encodeUrlUtf8(query) + "&page=1";
33-
} catch (final UnsupportedEncodingException e) {
34-
throw new ParsingException("query \"" + query + "\" could not be encoded", e);
35-
}
30+
return BASE_URL + "/search?q=" + Utils.encodeUrlUtf8(query) + "&page=1";
3631
}
3732
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/linkHandler/MediaCCCSearchQueryHandlerFactory.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
55
import org.schabi.newpipe.extractor.utils.Utils;
66

7-
import java.io.UnsupportedEncodingException;
87
import java.util.List;
98

109
public final class MediaCCCSearchQueryHandlerFactory extends SearchQueryHandlerFactory {
@@ -42,10 +41,6 @@ public String getUrl(final String query,
4241
final List<String> contentFilter,
4342
final String sortFilter)
4443
throws ParsingException, UnsupportedOperationException {
45-
try {
46-
return "https://media.ccc.de/public/events/search?q=" + Utils.encodeUrlUtf8(query);
47-
} catch (final UnsupportedEncodingException e) {
48-
throw new ParsingException("Could not create search string with query: " + query, e);
49-
}
44+
return "https://media.ccc.de/public/events/search?q=" + Utils.encodeUrlUtf8(query);
5045
}
5146
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamExtractor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.schabi.newpipe.extractor.utils.Utils;
3939

4040
import java.io.IOException;
41-
import java.io.UnsupportedEncodingException;
4241
import java.util.ArrayList;
4342
import java.util.Collections;
4443
import java.util.List;
@@ -379,8 +378,7 @@ public List<Frameset> getFrames() throws ExtractionException {
379378
}
380379

381380
@Nonnull
382-
private String getRelatedItemsUrl(@Nonnull final List<String> tags)
383-
throws UnsupportedEncodingException {
381+
private String getRelatedItemsUrl(@Nonnull final List<String> tags) {
384382
final String url = baseUrl + PeertubeSearchQueryHandlerFactory.SEARCH_ENDPOINT_VIDEOS;
385383
final StringBuilder params = new StringBuilder();
386384
params.append("start=0&count=8&sort=-createdAt");

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/linkHandler/PeertubeSearchQueryHandlerFactory.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
66
import org.schabi.newpipe.extractor.utils.Utils;
77

8-
import java.io.UnsupportedEncodingException;
98
import java.util.List;
109

1110
public final class PeertubeSearchQueryHandlerFactory extends SearchQueryHandlerFactory {
@@ -49,21 +48,17 @@ public String getUrl(final String searchString,
4948
final String sortFilter,
5049
final String baseUrl)
5150
throws ParsingException, UnsupportedOperationException {
52-
try {
53-
final String endpoint;
54-
if (contentFilters.isEmpty()
55-
|| contentFilters.get(0).equals(VIDEOS)
56-
|| contentFilters.get(0).equals(SEPIA_VIDEOS)) {
57-
endpoint = SEARCH_ENDPOINT_VIDEOS;
58-
} else if (contentFilters.get(0).equals(CHANNELS)) {
59-
endpoint = SEARCH_ENDPOINT_CHANNELS;
60-
} else {
61-
endpoint = SEARCH_ENDPOINT_PLAYLISTS;
62-
}
63-
return baseUrl + endpoint + "?search=" + Utils.encodeUrlUtf8(searchString);
64-
} catch (final UnsupportedEncodingException e) {
65-
throw new ParsingException("Could not encode query", e);
51+
final String endpoint;
52+
if (contentFilters.isEmpty()
53+
|| contentFilters.get(0).equals(VIDEOS)
54+
|| contentFilters.get(0).equals(SEPIA_VIDEOS)) {
55+
endpoint = SEARCH_ENDPOINT_VIDEOS;
56+
} else if (contentFilters.get(0).equals(CHANNELS)) {
57+
endpoint = SEARCH_ENDPOINT_CHANNELS;
58+
} else {
59+
endpoint = SEARCH_ENDPOINT_PLAYLISTS;
6660
}
61+
return baseUrl + endpoint + "?search=" + Utils.encodeUrlUtf8(searchString);
6762
}
6863

6964
@Override

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudSearchExtractor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.schabi.newpipe.extractor.utils.Parser;
2424

2525
import java.io.IOException;
26-
import java.io.UnsupportedEncodingException;
2726
import java.net.MalformedURLException;
2827
import java.net.URL;
2928
import java.util.Collections;
@@ -159,7 +158,7 @@ private Page getNextPageFromCurrentUrl(final String currentUrl,
159158
private int getOffsetFromUrl(final String url) throws ParsingException {
160159
try {
161160
return Integer.parseInt(Parser.compatParseMap(new URL(url).getQuery()).get("offset"));
162-
} catch (MalformedURLException | UnsupportedEncodingException e) {
161+
} catch (final MalformedURLException e) {
163162
throw new ParsingException("Could not get offset from page URL", e);
164163
}
165164
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.schabi.newpipe.extractor.utils.Utils;
4040

4141
import java.io.IOException;
42-
import java.io.UnsupportedEncodingException;
4342
import java.util.ArrayList;
4443
import java.util.Collections;
4544
import java.util.List;
@@ -317,14 +316,6 @@ public void extractDownloadableFileIfAvailable(final List<AudioStream> audioStre
317316
}
318317
}
319318

320-
private static String urlEncode(final String value) {
321-
try {
322-
return Utils.encodeUrlUtf8(value);
323-
} catch (final UnsupportedEncodingException e) {
324-
throw new IllegalStateException(e);
325-
}
326-
}
327-
328319
@Override
329320
public List<VideoStream> getVideoStreams() {
330321
return Collections.emptyList();
@@ -344,9 +335,8 @@ public StreamType getStreamType() {
344335
@Override
345336
public StreamInfoItemsCollector getRelatedItems() throws IOException, ExtractionException {
346337
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
347-
348-
final String apiUrl = SOUNDCLOUD_API_V2_URL + "tracks/" + urlEncode(getId())
349-
+ "/related?client_id=" + urlEncode(clientId());
338+
final String apiUrl = SOUNDCLOUD_API_V2_URL + "tracks/" + Utils.encodeUrlUtf8(getId())
339+
+ "/related?client_id=" + Utils.encodeUrlUtf8(clientId());
350340

351341
SoundcloudParsingHelper.getStreamsFromApi(collector, apiUrl);
352342
return collector;

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudSearchQueryHandlerFactory.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.schabi.newpipe.extractor.utils.Utils;
1111

1212
import java.io.IOException;
13-
import java.io.UnsupportedEncodingException;
1413
import java.util.List;
1514

1615
public final class SoundcloudSearchQueryHandlerFactory extends SearchQueryHandlerFactory {
@@ -60,9 +59,6 @@ public String getUrl(final String id,
6059
return url + "?q=" + Utils.encodeUrlUtf8(id)
6160
+ "&client_id=" + SoundcloudParsingHelper.clientId()
6261
+ "&limit=" + ITEMS_PER_PAGE + "&offset=0";
63-
64-
} catch (final UnsupportedEncodingException e) {
65-
throw new ParsingException("Could not encode query", e);
6662
} catch (final ReCaptchaException e) {
6763
throw new ParsingException("ReCaptcha required", e);
6864
} catch (final IOException | ExtractionException e) {

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import org.schabi.newpipe.extractor.utils.Utils;
5353

5454
import java.io.IOException;
55-
import java.io.UnsupportedEncodingException;
5655
import java.net.MalformedURLException;
5756
import java.net.URL;
5857
import java.nio.charset.StandardCharsets;
@@ -810,11 +809,7 @@ public static String getUrlFromNavigationEndpoint(
810809
final String[] params = internUrl.split("&");
811810
for (final String param : params) {
812811
if (param.split("=")[0].equals("q")) {
813-
try {
814-
return Utils.decodeUrlUtf8(param.split("=")[1]);
815-
} catch (final UnsupportedEncodingException e) {
816-
return null;
817-
}
812+
return Utils.decodeUrlUtf8(param.split("=")[1]);
818813
}
819814
}
820815
} else if (internUrl.startsWith("http")) {

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696
import java.util.Collections;
9797
import java.util.List;
9898
import java.util.Locale;
99-
import java.util.Map;
10099
import java.util.Objects;
101100
import java.util.stream.Collectors;
102101

@@ -1304,7 +1303,7 @@ private java.util.stream.Stream<ItagInfo> getStreamsFromStreamingDataKey(
13041303
return buildAndAddItagInfoToList(videoId, formatData, itagItem,
13051304
itagItem.itagType, contentPlaybackNonce);
13061305
}
1307-
} catch (final IOException | ExtractionException ignored) {
1306+
} catch (final ExtractionException ignored) {
13081307
// if the itag is not supported and getItag fails, we end up here
13091308
}
13101309
return null;
@@ -1317,19 +1316,18 @@ private ItagInfo buildAndAddItagInfoToList(
13171316
@Nonnull final JsonObject formatData,
13181317
@Nonnull final ItagItem itagItem,
13191318
@Nonnull final ItagItem.ItagType itagType,
1320-
@Nonnull final String contentPlaybackNonce) throws IOException, ExtractionException {
1319+
@Nonnull final String contentPlaybackNonce) throws ExtractionException {
13211320
String streamUrl;
13221321
if (formatData.has("url")) {
13231322
streamUrl = formatData.getString("url");
13241323
} else {
13251324
// This url has an obfuscated signature
1326-
final String cipherString = formatData.has(CIPHER)
1327-
? formatData.getString(CIPHER)
1328-
: formatData.getString(SIGNATURE_CIPHER);
1329-
final Map<String, String> cipher = Parser.compatParseMap(
1330-
cipherString);
1331-
streamUrl = cipher.get("url") + "&" + cipher.get("sp") + "="
1332-
+ YoutubeJavaScriptPlayerManager.deobfuscateSignature(videoId, cipher.get("s"));
1325+
final String cipherString = formatData.getString(CIPHER,
1326+
formatData.getString(SIGNATURE_CIPHER));
1327+
final var cipher = Parser.compatParseMap(cipherString);
1328+
final String signature = YoutubeJavaScriptPlayerManager.deobfuscateSignature(videoId,
1329+
cipher.getOrDefault("s", ""));
1330+
streamUrl = cipher.get("url") + "&" + cipher.get("sp") + "=" + signature;
13331331
}
13341332

13351333
// Add the content playback nonce to the stream URL

0 commit comments

Comments
 (0)