Merge pull request #880 from Isira-Seneviratne/Use_StandardCharsets

Use StandardCharsets.UTF_8.
This commit is contained in:
Tobi 2022-11-12 03:23:44 +01:00 committed by GitHub
commit 43d1c1f8b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 143 additions and 141 deletions

View file

@ -13,7 +13,7 @@ If you're using Gradle, you could add NewPipe Extractor as a dependency with the
1. Add `maven { url 'https://jitpack.io' }` to the `repositories` in your `build.gradle`. 1. Add `maven { url 'https://jitpack.io' }` to the `repositories` in your `build.gradle`.
2. Add `implementation 'com.github.TeamNewPipe:NewPipeExtractor:INSERT_VERSION_HERE'` to the `dependencies` in your `build.gradle`. Replace `INSERT_VERSION_HERE` with the [latest release](https://github.com/TeamNewPipe/NewPipeExtractor/releases/latest). 2. Add `implementation 'com.github.TeamNewPipe:NewPipeExtractor:INSERT_VERSION_HERE'` to the `dependencies` in your `build.gradle`. Replace `INSERT_VERSION_HERE` with the [latest release](https://github.com/TeamNewPipe/NewPipeExtractor/releases/latest).
**Note:** To use NewPipe Extractor in projects with a `minSdkVersion` below 26, [API desugaring](https://developer.android.com/studio/write/java8-support#library-desugaring) is required. **Note:** To use NewPipe Extractor in 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.
### Testing changes ### Testing changes

View file

@ -13,9 +13,9 @@ import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader; import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor; import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor;
import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -33,7 +33,7 @@ public class BandcampSuggestionExtractor extends SuggestionExtractor {
try { try {
final JsonObject fuzzyResults = JsonParser.object().from(downloader final JsonObject fuzzyResults = JsonParser.object().from(downloader
.get(AUTOCOMPLETE_URL + URLEncoder.encode(query, "UTF-8")).responseBody()); .get(AUTOCOMPLETE_URL + Utils.encodeUrlUtf8(query)).responseBody());
return fuzzyResults.getObject("auto").getArray("results").stream() return fuzzyResults.getObject("auto").getArray("results").stream()
.filter(JsonObject.class::isInstance) .filter(JsonObject.class::isInstance)
@ -44,6 +44,5 @@ public class BandcampSuggestionExtractor extends SuggestionExtractor {
} catch (final JsonParserException e) { } catch (final JsonParserException e) {
return Collections.emptyList(); return Collections.emptyList();
} }
} }
} }

View file

@ -2,24 +2,22 @@
package org.schabi.newpipe.extractor.services.bandcamp.linkHandler; package org.schabi.newpipe.extractor.services.bandcamp.linkHandler;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.BASE_URL; import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.BASE_URL;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
import org.schabi.newpipe.extractor.utils.Utils;
import java.io.UnsupportedEncodingException;
import java.util.List;
public class BandcampSearchQueryHandlerFactory extends SearchQueryHandlerFactory { public class BandcampSearchQueryHandlerFactory extends SearchQueryHandlerFactory {
@Override @Override
public String getUrl(final String query, public String getUrl(final String query,
final List<String> contentFilter, final List<String> contentFilter,
final String sortFilter) throws ParsingException { final String sortFilter) throws ParsingException {
try { try {
return BASE_URL + "/search?q=" + URLEncoder.encode(query, "UTF-8") + "&page=1"; return BASE_URL + "/search?q=" + Utils.encodeUrlUtf8(query) + "&page=1";
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
throw new ParsingException("query \"" + query + "\" could not be encoded", e); throw new ParsingException("query \"" + query + "\" could not be encoded", e);
} }

View file

@ -2,13 +2,11 @@ package org.schabi.newpipe.extractor.services.media_ccc.linkHandler;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
import org.schabi.newpipe.extractor.utils.Utils;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List; import java.util.List;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
public class MediaCCCSearchQueryHandlerFactory extends SearchQueryHandlerFactory { public class MediaCCCSearchQueryHandlerFactory extends SearchQueryHandlerFactory {
public static final String ALL = "all"; public static final String ALL = "all";
public static final String CONFERENCES = "conferences"; public static final String CONFERENCES = "conferences";
@ -32,8 +30,7 @@ public class MediaCCCSearchQueryHandlerFactory extends SearchQueryHandlerFactory
public String getUrl(final String query, final List<String> contentFilter, public String getUrl(final String query, final List<String> contentFilter,
final String sortFilter) throws ParsingException { final String sortFilter) throws ParsingException {
try { try {
return "https://media.ccc.de/public/events/search?q=" return "https://media.ccc.de/public/events/search?q=" + Utils.encodeUrlUtf8(query);
+ URLEncoder.encode(query, UTF_8);
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
throw new ParsingException("Could not create search string with query: " + query, e); throw new ParsingException("Could not create search string with query: " + query, e);
} }

View file

@ -1,7 +1,6 @@
package org.schabi.newpipe.extractor.services.peertube.extractors; package org.schabi.newpipe.extractor.services.peertube.extractors;
import static org.schabi.newpipe.extractor.stream.AudioStream.UNKNOWN_BITRATE; import static org.schabi.newpipe.extractor.stream.AudioStream.UNKNOWN_BITRATE;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonArray;
@ -36,7 +35,6 @@ import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -327,8 +325,7 @@ public class PeertubeStreamExtractor extends StreamExtractor {
final StringBuilder params = new StringBuilder(); final StringBuilder params = new StringBuilder();
params.append("start=0&count=8&sort=-createdAt"); params.append("start=0&count=8&sort=-createdAt");
for (final String tag : tags) { for (final String tag : tags) {
params.append("&tagsOneOf="); params.append("&tagsOneOf=").append(Utils.encodeUrlUtf8(tag));
params.append(URLEncoder.encode(tag, UTF_8));
} }
return url + "?" + params; return url + "?" + params;
} }

View file

@ -3,13 +3,11 @@ package org.schabi.newpipe.extractor.services.peertube.linkHandler;
import org.schabi.newpipe.extractor.ServiceList; import org.schabi.newpipe.extractor.ServiceList;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
import org.schabi.newpipe.extractor.utils.Utils;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List; import java.util.List;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
public final class PeertubeSearchQueryHandlerFactory extends SearchQueryHandlerFactory { public final class PeertubeSearchQueryHandlerFactory extends SearchQueryHandlerFactory {
public static final String VIDEOS = "videos"; public static final String VIDEOS = "videos";
@ -43,7 +41,7 @@ public final class PeertubeSearchQueryHandlerFactory extends SearchQueryHandlerF
final String sortFilter, final String sortFilter,
final String baseUrl) throws ParsingException { final String baseUrl) throws ParsingException {
try { try {
return baseUrl + SEARCH_ENDPOINT + "?search=" + URLEncoder.encode(searchString, UTF_8); return baseUrl + SEARCH_ENDPOINT + "?search=" + Utils.encodeUrlUtf8(searchString);
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
throw new ParsingException("Could not encode query", e); throw new ParsingException("Could not encode query", e);
} }

View file

@ -1,7 +1,6 @@
package org.schabi.newpipe.extractor.services.soundcloud; package org.schabi.newpipe.extractor.services.soundcloud;
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud; import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps; import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps;
@ -32,7 +31,6 @@ import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
@ -109,7 +107,7 @@ public final class SoundcloudParsingHelper {
public static JsonObject resolveFor(@Nonnull final Downloader downloader, final String url) public static JsonObject resolveFor(@Nonnull final Downloader downloader, final String url)
throws IOException, ExtractionException { throws IOException, ExtractionException {
final String apiUrl = SOUNDCLOUD_API_V2_URL + "resolve" final String apiUrl = SOUNDCLOUD_API_V2_URL + "resolve"
+ "?url=" + URLEncoder.encode(url, UTF_8) + "?url=" + Utils.encodeUrlUtf8(url)
+ "&client_id=" + clientId(); + "&client_id=" + clientId();
try { try {
@ -131,7 +129,7 @@ public final class SoundcloudParsingHelper {
ReCaptchaException { ReCaptchaException {
final String response = NewPipe.getDownloader().get("https://w.soundcloud.com/player/?url=" final String response = NewPipe.getDownloader().get("https://w.soundcloud.com/player/?url="
+ URLEncoder.encode(apiUrl, UTF_8), SoundCloud.getLocalization()).responseBody(); + Utils.encodeUrlUtf8(apiUrl), SoundCloud.getLocalization()).responseBody();
return Jsoup.parse(response).select("link[rel=\"canonical\"]").first() return Jsoup.parse(response).select("link[rel=\"canonical\"]").first()
.attr("abs:href"); .attr("abs:href");
@ -162,7 +160,7 @@ public final class SoundcloudParsingHelper {
try { try {
final String widgetUrl = "https://api-widget.soundcloud.com/resolve?url=" final String widgetUrl = "https://api-widget.soundcloud.com/resolve?url="
+ URLEncoder.encode(url.toString(), UTF_8) + Utils.encodeUrlUtf8(url.toString())
+ "&format=json&client_id=" + SoundcloudParsingHelper.clientId(); + "&format=json&client_id=" + SoundcloudParsingHelper.clientId();
final String response = NewPipe.getDownloader().get(widgetUrl, final String response = NewPipe.getDownloader().get(widgetUrl,
SoundCloud.getLocalization()).responseBody(); SoundCloud.getLocalization()).responseBody();

View file

@ -4,7 +4,6 @@ import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsing
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.clientId; import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.clientId;
import static org.schabi.newpipe.extractor.stream.AudioStream.UNKNOWN_BITRATE; import static org.schabi.newpipe.extractor.stream.AudioStream.UNKNOWN_BITRATE;
import static org.schabi.newpipe.extractor.stream.Stream.ID_UNKNOWN; import static org.schabi.newpipe.extractor.stream.Stream.ID_UNKNOWN;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonArray;
@ -32,10 +31,10 @@ import org.schabi.newpipe.extractor.stream.StreamExtractor;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.stream.StreamType; import org.schabi.newpipe.extractor.stream.StreamType;
import org.schabi.newpipe.extractor.stream.VideoStream; import org.schabi.newpipe.extractor.stream.VideoStream;
import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -320,7 +319,7 @@ public class SoundcloudStreamExtractor extends StreamExtractor {
private static String urlEncode(final String value) { private static String urlEncode(final String value) {
try { try {
return URLEncoder.encode(value, UTF_8); return Utils.encodeUrlUtf8(value);
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }

View file

@ -1,9 +1,12 @@
package org.schabi.newpipe.extractor.services.soundcloud.extractors; package org.schabi.newpipe.extractor.services.soundcloud.extractors;
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject; import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser; import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException; import com.grack.nanojson.JsonParserException;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader; import org.schabi.newpipe.extractor.downloader.Downloader;
@ -11,15 +14,12 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper; import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper;
import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor; import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor;
import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
public class SoundcloudSuggestionExtractor extends SuggestionExtractor { public class SoundcloudSuggestionExtractor extends SuggestionExtractor {
public SoundcloudSuggestionExtractor(final StreamingService service) { public SoundcloudSuggestionExtractor(final StreamingService service) {
@ -31,9 +31,9 @@ public class SoundcloudSuggestionExtractor extends SuggestionExtractor {
ExtractionException { ExtractionException {
final List<String> suggestions = new ArrayList<>(); final List<String> suggestions = new ArrayList<>();
final Downloader dl = NewPipe.getDownloader(); final Downloader dl = NewPipe.getDownloader();
final String url = SOUNDCLOUD_API_V2_URL + "search/queries" + "?q=" final String url = SOUNDCLOUD_API_V2_URL + "search/queries?q="
+ URLEncoder.encode(query, UTF_8) + "&client_id=" + Utils.encodeUrlUtf8(query) + "&client_id=" + SoundcloudParsingHelper.clientId()
+ SoundcloudParsingHelper.clientId() + "&limit=10"; + "&limit=10";
final String response = dl.get(url, getExtractorLocalization()).responseBody(); final String response = dl.get(url, getExtractorLocalization()).responseBody();
try { try {

View file

@ -1,19 +1,18 @@
package org.schabi.newpipe.extractor.services.soundcloud.linkHandler; package org.schabi.newpipe.extractor.services.soundcloud.linkHandler;
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper; import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper;
import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List; import java.util.List;
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
public class SoundcloudSearchQueryHandlerFactory extends SearchQueryHandlerFactory { public class SoundcloudSearchQueryHandlerFactory extends SearchQueryHandlerFactory {
public static final String TRACKS = "tracks"; public static final String TRACKS = "tracks";
@ -48,9 +47,9 @@ public class SoundcloudSearchQueryHandlerFactory extends SearchQueryHandlerFacto
} }
} }
return url + "?q=" + URLEncoder.encode(id, UTF_8) + "&client_id=" return url + "?q=" + Utils.encodeUrlUtf8(id)
+ SoundcloudParsingHelper.clientId() + "&limit=" + ITEMS_PER_PAGE + "&client_id=" + SoundcloudParsingHelper.clientId()
+ "&offset=0"; + "&limit=" + ITEMS_PER_PAGE + "&offset=0";
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
throw new ParsingException("Could not encode query", e); throw new ParsingException("Could not encode query", e);

View file

@ -23,7 +23,6 @@ package org.schabi.newpipe.extractor.services.youtube;
import static org.schabi.newpipe.extractor.NewPipe.getDownloader; import static org.schabi.newpipe.extractor.NewPipe.getDownloader;
import static org.schabi.newpipe.extractor.utils.Utils.HTTP; import static org.schabi.newpipe.extractor.utils.Utils.HTTP;
import static org.schabi.newpipe.extractor.utils.Utils.HTTPS; import static org.schabi.newpipe.extractor.utils.Utils.HTTPS;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import static org.schabi.newpipe.extractor.utils.Utils.getStringResultFromRegexArray; import static org.schabi.newpipe.extractor.utils.Utils.getStringResultFromRegexArray;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
@ -55,7 +54,6 @@ import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.time.LocalDate; import java.time.LocalDate;
@ -575,7 +573,7 @@ public final class YoutubeParsingHelper {
.end() .end()
.value("fetchLiveState", true) .value("fetchLiveState", true)
.end() .end()
.end().done().getBytes(UTF_8); .end().done().getBytes(StandardCharsets.UTF_8);
// @formatter:on // @formatter:on
final Map<String, List<String>> headers = new HashMap<>(); final Map<String, List<String>> headers = new HashMap<>();
@ -820,7 +818,7 @@ public final class YoutubeParsingHelper {
.end() .end()
.end() .end()
.value("input", "") .value("input", "")
.end().done().getBytes(UTF_8); .end().done().getBytes(StandardCharsets.UTF_8);
// @formatter:on // @formatter:on
final Map<String, List<String>> headers = new HashMap<>(); final Map<String, List<String>> headers = new HashMap<>();
@ -892,7 +890,7 @@ public final class YoutubeParsingHelper {
for (final String param : params) { for (final String param : params) {
if (param.split("=")[0].equals("q")) { if (param.split("=")[0].equals("q")) {
try { try {
return URLDecoder.decode(param.split("=")[1], UTF_8); return Utils.decodeUrlUtf8(param.split("=")[1]);
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
return null; return null;
} }

View file

@ -9,7 +9,6 @@ import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getValidJsonResponseBody; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getValidJsonResponseBody;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonArray;
@ -35,6 +34,7 @@ import org.schabi.newpipe.extractor.utils.JsonUtils;
import org.schabi.newpipe.extractor.utils.Utils; import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -98,7 +98,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
getExtractorLocalization(), getExtractorContentCountry()) getExtractorLocalization(), getExtractorContentCountry())
.value("url", "https://www.youtube.com/" + channelPath) .value("url", "https://www.youtube.com/" + channelPath)
.done()) .done())
.getBytes(UTF_8); .getBytes(StandardCharsets.UTF_8);
final JsonObject jsonResponse = getJsonPostResponse("navigation/resolve_url", final JsonObject jsonResponse = getJsonPostResponse("navigation/resolve_url",
body, getExtractorLocalization()); body, getExtractorLocalization());
@ -146,7 +146,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
.value("browseId", id) .value("browseId", id)
.value("params", "EgZ2aWRlb3M%3D") // Equal to videos .value("params", "EgZ2aWRlb3M%3D") // Equal to videos
.done()) .done())
.getBytes(UTF_8); .getBytes(StandardCharsets.UTF_8);
final JsonObject jsonResponse = getJsonPostResponse("browse", body, final JsonObject jsonResponse = getJsonPostResponse("browse", body,
getExtractorLocalization()); getExtractorLocalization());
@ -397,7 +397,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
getExtractorContentCountry()) getExtractorContentCountry())
.value("continuation", continuation) .value("continuation", continuation)
.done()) .done())
.getBytes(UTF_8); .getBytes(StandardCharsets.UTF_8);
return new Page(YOUTUBEI_V1_URL + "browse?key=" + getKey() return new Page(YOUTUBEI_V1_URL + "browse?key=" + getKey()
+ DISABLE_PRETTY_PRINT_PARAMETER, null, channelIds, null, body); + DISABLE_PRETTY_PRINT_PARAMETER, null, channelIds, null, body);

View file

@ -10,7 +10,6 @@ import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeS
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_PLAYLISTS; import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_PLAYLISTS;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_SONGS; import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_SONGS;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_VIDEOS; import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_VIDEOS;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonArray;
@ -38,6 +37,7 @@ import org.schabi.newpipe.extractor.utils.Parser;
import org.schabi.newpipe.extractor.utils.Utils; import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -113,7 +113,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor {
.end() .end()
.value("query", getSearchString()) .value("query", getSearchString())
.value("params", params) .value("params", params)
.end().done().getBytes(UTF_8); .end().done().getBytes(StandardCharsets.UTF_8);
// @formatter:on // @formatter:on
final Map<String, List<String>> headers = new HashMap<>(); final Map<String, List<String>> headers = new HashMap<>();
@ -248,7 +248,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor {
.value("enableSafetyMode", false) .value("enableSafetyMode", false)
.end() .end()
.end() .end()
.end().done().getBytes(UTF_8); .end().done().getBytes(StandardCharsets.UTF_8);
// @formatter:on // @formatter:on
final Map<String, List<String>> headers = new HashMap<>(); final Map<String, List<String>> headers = new HashMap<>();

View file

@ -8,7 +8,6 @@ import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getValidJsonResponseBody; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getValidJsonResponseBody;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.getSearchParameter; import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.getSearchParameter;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonArray;
@ -20,6 +19,7 @@ import com.grack.nanojson.JsonWriter;
import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.MetaInfo; import org.schabi.newpipe.extractor.MetaInfo;
import org.schabi.newpipe.extractor.MultiInfoItemsCollector;
import org.schabi.newpipe.extractor.Page; import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader; import org.schabi.newpipe.extractor.downloader.Downloader;
@ -28,12 +28,12 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler; import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler;
import org.schabi.newpipe.extractor.localization.Localization; import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.localization.TimeAgoParser; import org.schabi.newpipe.extractor.localization.TimeAgoParser;
import org.schabi.newpipe.extractor.MultiInfoItemsCollector;
import org.schabi.newpipe.extractor.search.SearchExtractor; import org.schabi.newpipe.extractor.search.SearchExtractor;
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper; import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
import org.schabi.newpipe.extractor.utils.JsonUtils; import org.schabi.newpipe.extractor.utils.JsonUtils;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -90,7 +90,7 @@ public class YoutubeSearchExtractor extends SearchExtractor {
jsonBody.value("params", params); jsonBody.value("params", params);
} }
final byte[] body = JsonWriter.string(jsonBody.done()).getBytes(UTF_8); final byte[] body = JsonWriter.string(jsonBody.done()).getBytes(StandardCharsets.UTF_8);
initialData = getJsonPostResponse("search", body, localization); initialData = getJsonPostResponse("search", body, localization);
} }
@ -184,7 +184,7 @@ public class YoutubeSearchExtractor extends SearchExtractor {
getExtractorContentCountry()) getExtractorContentCountry())
.value("continuation", page.getId()) .value("continuation", page.getId())
.done()) .done())
.getBytes(UTF_8); .getBytes(StandardCharsets.UTF_8);
// @formatter:on // @formatter:on
final String responseBody = getValidJsonResponseBody(getDownloader().post( final String responseBody = getValidJsonResponseBody(getDownloader().post(

View file

@ -1,7 +1,6 @@
package org.schabi.newpipe.extractor.services.youtube.extractors; package org.schabi.newpipe.extractor.services.youtube.extractors;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getCookieHeader; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getCookieHeader;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonParser; import com.grack.nanojson.JsonParser;
@ -13,9 +12,9 @@ import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor; import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor;
import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -54,8 +53,8 @@ public class YoutubeSuggestionExtractor extends SuggestionExtractor {
+ "?client=" + "youtube" //"firefox" for JSON, 'toolbar' for xml + "?client=" + "youtube" //"firefox" for JSON, 'toolbar' for xml
+ "&jsonp=" + "JP" + "&jsonp=" + "JP"
+ "&ds=" + "yt" + "&ds=" + "yt"
+ "&gl=" + URLEncoder.encode(getExtractorContentCountry().getCountryCode(), UTF_8) + "&gl=" + Utils.encodeUrlUtf8(getExtractorContentCountry().getCountryCode())
+ "&q=" + URLEncoder.encode(query, UTF_8); + "&q=" + Utils.encodeUrlUtf8(query);
String response = dl.get(url, getCookieHeader(), getExtractorLocalization()).responseBody(); String response = dl.get(url, getCookieHeader(), getExtractorLocalization()).responseBody();
// trim JSONP part "JP(...)" // trim JSONP part "JP(...)"

View file

@ -20,6 +20,11 @@
package org.schabi.newpipe.extractor.services.youtube.extractors; package org.schabi.newpipe.extractor.services.youtube.extractors;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonPostResponse;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextAtKey;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import com.grack.nanojson.JsonObject; import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonWriter; import com.grack.nanojson.JsonWriter;
@ -35,15 +40,10 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonPostResponse;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextAtKey;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class YoutubeTrendingExtractor extends KioskExtractor<StreamInfoItem> { public class YoutubeTrendingExtractor extends KioskExtractor<StreamInfoItem> {
private JsonObject initialData; private JsonObject initialData;
@ -61,7 +61,7 @@ public class YoutubeTrendingExtractor extends KioskExtractor<StreamInfoItem> {
getExtractorContentCountry()) getExtractorContentCountry())
.value("browseId", "FEtrending") .value("browseId", "FEtrending")
.done()) .done())
.getBytes(UTF_8); .getBytes(StandardCharsets.UTF_8);
// @formatter:on // @formatter:on
initialData = getJsonPostResponse("browse", body, getExtractorLocalization()); initialData = getJsonPostResponse("browse", body, getExtractorLocalization());

View file

@ -1,15 +1,15 @@
package org.schabi.newpipe.extractor.services.youtube.linkHandler; package org.schabi.newpipe.extractor.services.youtube.linkHandler;
import static org.schabi.newpipe.extractor.utils.Utils.encodeUrlUtf8;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
import javax.annotation.Nonnull;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List; import java.util.List;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8; import javax.annotation.Nonnull;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public final class YoutubeSearchQueryHandlerFactory extends SearchQueryHandlerFactory { public final class YoutubeSearchQueryHandlerFactory extends SearchQueryHandlerFactory {
@ -44,24 +44,21 @@ public final class YoutubeSearchQueryHandlerFactory extends SearchQueryHandlerFa
default: default:
break; break;
case VIDEOS: case VIDEOS:
return SEARCH_URL + URLEncoder.encode(searchString, UTF_8) return SEARCH_URL + encodeUrlUtf8(searchString) + "&sp=EgIQAQ%253D%253D";
+ "&sp=EgIQAQ%253D%253D";
case CHANNELS: case CHANNELS:
return SEARCH_URL + URLEncoder.encode(searchString, UTF_8) return SEARCH_URL + encodeUrlUtf8(searchString) + "&sp=EgIQAg%253D%253D";
+ "&sp=EgIQAg%253D%253D";
case PLAYLISTS: case PLAYLISTS:
return SEARCH_URL + URLEncoder.encode(searchString, UTF_8) return SEARCH_URL + encodeUrlUtf8(searchString) + "&sp=EgIQAw%253D%253D";
+ "&sp=EgIQAw%253D%253D";
case MUSIC_SONGS: case MUSIC_SONGS:
case MUSIC_VIDEOS: case MUSIC_VIDEOS:
case MUSIC_ALBUMS: case MUSIC_ALBUMS:
case MUSIC_PLAYLISTS: case MUSIC_PLAYLISTS:
case MUSIC_ARTISTS: case MUSIC_ARTISTS:
return MUSIC_SEARCH_URL + URLEncoder.encode(searchString, UTF_8); return MUSIC_SEARCH_URL + encodeUrlUtf8(searchString);
} }
} }
return SEARCH_URL + URLEncoder.encode(searchString, UTF_8); return SEARCH_URL + encodeUrlUtf8(searchString);
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
throw new ParsingException("Could not encode query", e); throw new ParsingException("Could not encode query", e);
} }

View file

@ -20,12 +20,9 @@
package org.schabi.newpipe.extractor.utils; package org.schabi.newpipe.extractor.utils;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -99,7 +96,7 @@ public final class Parser {
for (final String arg : input.split("&")) { for (final String arg : input.split("&")) {
final String[] splitArg = arg.split("="); final String[] splitArg = arg.split("=");
if (splitArg.length > 1) { if (splitArg.length > 1) {
map.put(splitArg[0], URLDecoder.decode(splitArg[1], UTF_8)); map.put(splitArg[0], Utils.decodeUrlUtf8(splitArg[1]));
} else { } else {
map.put(splitArg[0], ""); map.put(splitArg[0], "");
} }

View file

@ -6,6 +6,8 @@ import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
@ -17,14 +19,8 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public final class Utils { public final class Utils {
public static final String HTTP = "http://"; public static final String HTTP = "http://";
public static final String HTTPS = "https://"; public static final String HTTPS = "https://";
/**
* @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_8}
*/
@Deprecated
public static final String UTF_8 = "UTF-8";
private static final Pattern M_PATTERN = Pattern.compile("(https?)?://m\\."); private static final Pattern M_PATTERN = Pattern.compile("(https?)?://m\\.");
private static final Pattern WWW_PATTERN = Pattern.compile("(https?)?://www\\."); private static final Pattern WWW_PATTERN = Pattern.compile("(https?)?://www\\.");
@ -32,6 +28,29 @@ public final class Utils {
// no instance // no instance
} }
/**
* Encodes a string to URL format using the UTF-8 character set.
*
* @param string The string to be encoded.
* @return The encoded URL.
* @throws UnsupportedEncodingException This shouldn't be thrown, as UTF-8 should be supported.
*/
public static String encodeUrlUtf8(final String string) throws UnsupportedEncodingException {
// TODO: Switch to URLEncoder.encode(String, Charset) in Java 10.
return URLEncoder.encode(string, StandardCharsets.UTF_8.name());
}
/**
* Decodes a URL using the UTF-8 character set.
* @param url The URL to be decoded.
* @return The decoded URL.
* @throws UnsupportedEncodingException This shouldn't be thrown, as UTF-8 should be supported.
*/
public static String decodeUrlUtf8(final String url) throws UnsupportedEncodingException {
// TODO: Switch to URLDecoder.decode(String, Charset) in Java 10.
return URLDecoder.decode(url, StandardCharsets.UTF_8.name());
}
/** /**
* Remove all non-digit characters from a string. * Remove all non-digit characters from a string.
* *
@ -139,7 +158,7 @@ public final class Utils {
String query; String query;
try { try {
query = URLDecoder.decode(params[0], UTF_8); query = decodeUrlUtf8(params[0]);
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
// Cannot decode string with UTF-8, using the string without decoding // Cannot decode string with UTF-8, using the string without decoding
query = params[0]; query = params[0];
@ -147,7 +166,7 @@ public final class Utils {
if (query.equals(parameterName)) { if (query.equals(parameterName)) {
try { try {
return URLDecoder.decode(params[1], UTF_8); return decodeUrlUtf8(params[1]);
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
// Cannot decode string with UTF-8, using the string without decoding // Cannot decode string with UTF-8, using the string without decoding
return params[1]; return params[1];
@ -244,9 +263,9 @@ public final class Utils {
public static String followGoogleRedirectIfNeeded(final String url) { public static String followGoogleRedirectIfNeeded(final String url) {
// If the url is a redirect from a Google search, extract the actual URL // If the url is a redirect from a Google search, extract the actual URL
try { try {
final URL decoded = Utils.stringToURL(url); final URL decoded = stringToURL(url);
if (decoded.getHost().contains("google") && decoded.getPath().equals("/url")) { if (decoded.getHost().contains("google") && decoded.getPath().equals("/url")) {
return URLDecoder.decode(Parser.matchGroup1("&url=([^&]+)(?:&|$)", url), UTF_8); return decodeUrlUtf8(Parser.matchGroup1("&url=([^&]+)(?:&|$)", url));
} }
} catch (final Exception ignored) { } catch (final Exception ignored) {
} }

View file

@ -2,10 +2,18 @@
package org.schabi.newpipe.extractor.services.bandcamp; package org.schabi.newpipe.extractor.services.bandcamp;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.ServiceList.Bandcamp;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.schabi.newpipe.downloader.DownloaderTestImpl; import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.*; import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem; import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
import org.schabi.newpipe.extractor.search.SearchExtractor; import org.schabi.newpipe.extractor.search.SearchExtractor;
@ -13,12 +21,9 @@ import org.schabi.newpipe.extractor.services.DefaultSearchExtractorTest;
import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampSearchExtractor; import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampSearchExtractor;
import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import javax.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals; import javax.annotation.Nullable;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.ServiceList.Bandcamp;
/** /**
* Test for {@link BandcampSearchExtractor} * Test for {@link BandcampSearchExtractor}
@ -73,20 +78,17 @@ public class BandcampSearchExtractorTest {
@Test @Test
void testAlbumSearch() throws ExtractionException, IOException { void testAlbumSearch() throws ExtractionException, IOException {
final SearchExtractor extractor = Bandcamp.getSearchExtractor("minecraft volume alpha"); final SearchExtractor extractor = Bandcamp.getSearchExtractor("minecraft volume alpha");
InfoItem minecraft = extractor.getInitialPage() final InfoItem minecraft = extractor.getInitialPage().getItems().get(0);
.getItems().get(0);
// Minecraft volume alpha should be the first result, no? // Minecraft volume alpha should be the first result, no?
assertEquals("Minecraft: Volume Alpha (cover)", minecraft.getName()); assertEquals("Minecraft - Volume Alpha", minecraft.getName());
assertTrue(minecraft.getThumbnailUrl().endsWith(".jpg")); assertTrue(minecraft.getThumbnailUrl().endsWith(".jpg"));
assertTrue(minecraft.getThumbnailUrl().contains("f4.bcbits.com/img/")); assertTrue(minecraft.getThumbnailUrl().contains("f4.bcbits.com/img/"));
assertEquals( assertEquals("https://c418.bandcamp.com/album/minecraft-volume-alpha",
"https://chromacat248.bandcamp.com/album/minecraft-volume-alpha-cover",
minecraft.getUrl()); minecraft.getUrl());
// Verify that playlist tracks counts get extracted correctly // Verify that playlist tracks counts get extracted correctly
assertEquals(3, ((PlaylistInfoItem) minecraft).getStreamCount()); assertEquals(24, ((PlaylistInfoItem) minecraft).getStreamCount());
} }
/** /**

View file

@ -1,5 +1,8 @@
package org.schabi.newpipe.extractor.services.peertube; package org.schabi.newpipe.extractor.services.peertube;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.schabi.newpipe.extractor.ServiceList.PeerTube;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -9,9 +12,6 @@ import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubePlaylistExtractor; import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubePlaylistExtractor;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.schabi.newpipe.extractor.ServiceList.PeerTube;
public class PeertubePlaylistExtractorTest { public class PeertubePlaylistExtractorTest {
public static class Shocking { public static class Shocking {
@ -39,41 +39,41 @@ public class PeertubePlaylistExtractorTest {
} }
@Test @Test
void testGetUploaderUrl() throws ParsingException { void testGetUploaderUrl() {
assertEquals("https://skeptikon.fr/accounts/metadechoc", extractor.getUploaderUrl()); assertEquals("https://skeptikon.fr/accounts/metadechoc", extractor.getUploaderUrl());
} }
@Test @Test
void testGetUploaderAvatarUrl() throws ParsingException { void testGetUploaderAvatarUrl() throws ParsingException {
assertEquals( assertEquals(
"https://framatube.org/lazy-static/avatars/cd0f781d-0287-4be2-94f1-24cd732337b2.jpg", "https://framatube.org/lazy-static/avatars/c6801ff9-cb49-42e6-b2db-3db623248115.jpg",
extractor.getUploaderAvatarUrl()); extractor.getUploaderAvatarUrl());
} }
@Test @Test
void testGetUploaderName() throws ParsingException { void testGetUploaderName() {
assertEquals("Méta de Choc", extractor.getUploaderName()); assertEquals("Méta de Choc", extractor.getUploaderName());
} }
@Test @Test
void testGetStreamCount() throws ParsingException { void testGetStreamCount() {
ExtractorAsserts.assertGreaterOrEqual(39, extractor.getStreamCount()); ExtractorAsserts.assertGreaterOrEqual(39, extractor.getStreamCount());
} }
@Test @Test
void testGetSubChannelUrl() throws ParsingException { void testGetSubChannelUrl() {
assertEquals("https://skeptikon.fr/video-channels/metadechoc_channel", extractor.getSubChannelUrl()); assertEquals("https://skeptikon.fr/video-channels/metadechoc_channel", extractor.getSubChannelUrl());
} }
@Test @Test
void testGetSubChannelName() throws ParsingException { void testGetSubChannelName() {
assertEquals("SHOCKING !", extractor.getSubChannelName()); assertEquals("SHOCKING !", extractor.getSubChannelName());
} }
@Test @Test
void testGetSubChannelAvatarUrl() throws ParsingException { void testGetSubChannelAvatarUrl() throws ParsingException {
assertEquals( assertEquals(
"https://framatube.org/lazy-static/avatars/637753af-fcf2-4b61-88f9-b9857c953457.png", "https://framatube.org/lazy-static/avatars/e801ccce-8694-4309-b0ab-e6f0e552ef77.png",
extractor.getSubChannelAvatarUrl()); extractor.getSubChannelAvatarUrl());
} }
} }

View file

@ -1,5 +1,13 @@
package org.schabi.newpipe.extractor.services.soundcloud.search; package org.schabi.newpipe.extractor.services.soundcloud.search;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
import static org.schabi.newpipe.extractor.services.DefaultTests.assertNoDuplicatedItems;
import static org.schabi.newpipe.extractor.services.soundcloud.linkHandler.SoundcloudSearchQueryHandlerFactory.PLAYLISTS;
import static org.schabi.newpipe.extractor.services.soundcloud.linkHandler.SoundcloudSearchQueryHandlerFactory.TRACKS;
import static org.schabi.newpipe.extractor.services.soundcloud.linkHandler.SoundcloudSearchQueryHandlerFactory.USERS;
import static java.util.Collections.singletonList;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.schabi.newpipe.downloader.DownloaderTestImpl; import org.schabi.newpipe.downloader.DownloaderTestImpl;
@ -11,19 +19,13 @@ import org.schabi.newpipe.extractor.channel.ChannelInfoItem;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.search.SearchExtractor; import org.schabi.newpipe.extractor.search.SearchExtractor;
import org.schabi.newpipe.extractor.services.DefaultSearchExtractorTest; import org.schabi.newpipe.extractor.services.DefaultSearchExtractorTest;
import org.schabi.newpipe.extractor.utils.Utils;
import javax.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List; import java.util.List;
import static java.util.Collections.singletonList; import javax.annotation.Nullable;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
import static org.schabi.newpipe.extractor.services.DefaultTests.assertNoDuplicatedItems;
import static org.schabi.newpipe.extractor.services.soundcloud.linkHandler.SoundcloudSearchQueryHandlerFactory.*;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
public class SoundcloudSearchExtractorTest { public class SoundcloudSearchExtractorTest {
@ -138,7 +140,7 @@ public class SoundcloudSearchExtractorTest {
private static String urlEncode(String value) { private static String urlEncode(String value) {
try { try {
return URLEncoder.encode(value, UTF_8); return Utils.encodeUrlUtf8(value);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View file

@ -1,5 +1,11 @@
package org.schabi.newpipe.extractor.services.youtube; package org.schabi.newpipe.extractor.services.youtube;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.schabi.newpipe.FileUtils.resolveTestResource;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.schabi.newpipe.downloader.DownloaderTestImpl; import org.schabi.newpipe.downloader.DownloaderTestImpl;
@ -13,13 +19,10 @@ import org.schabi.newpipe.extractor.subscription.SubscriptionItem;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.schabi.newpipe.FileUtils.resolveTestResource;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
/** /**
* Test for {@link YoutubeSubscriptionExtractor} * Test for {@link YoutubeSubscriptionExtractor}
*/ */
@ -54,7 +57,7 @@ public class YoutubeSubscriptionExtractorTest {
@Test @Test
public void testEmptySourceException() throws Exception { public void testEmptySourceException() throws Exception {
final List<SubscriptionItem> items = subscriptionExtractor.fromInputStream( final List<SubscriptionItem> items = subscriptionExtractor.fromInputStream(
new ByteArrayInputStream("[]".getBytes(UTF_8))); new ByteArrayInputStream("[]".getBytes(StandardCharsets.UTF_8)));
assertTrue(items.isEmpty()); assertTrue(items.isEmpty());
} }
@ -62,7 +65,7 @@ public class YoutubeSubscriptionExtractorTest {
public void testSubscriptionWithEmptyTitleInSource() throws Exception { public void testSubscriptionWithEmptyTitleInSource() throws Exception {
final String source = "[{\"snippet\":{\"resourceId\":{\"channelId\":\"UCEOXxzW2vU0P-0THehuIIeg\"}}}]"; final String source = "[{\"snippet\":{\"resourceId\":{\"channelId\":\"UCEOXxzW2vU0P-0THehuIIeg\"}}}]";
final List<SubscriptionItem> items = subscriptionExtractor.fromInputStream( final List<SubscriptionItem> items = subscriptionExtractor.fromInputStream(
new ByteArrayInputStream(source.getBytes(UTF_8))); new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8)));
assertEquals(1, items.size()); assertEquals(1, items.size());
assertEquals(ServiceList.YouTube.getServiceId(), items.get(0).getServiceId()); assertEquals(ServiceList.YouTube.getServiceId(), items.get(0).getServiceId());
@ -75,7 +78,7 @@ public class YoutubeSubscriptionExtractorTest {
final String source = "[{\"snippet\":{\"resourceId\":{\"channelId\":\"gibberish\"},\"title\":\"name1\"}}," + final String source = "[{\"snippet\":{\"resourceId\":{\"channelId\":\"gibberish\"},\"title\":\"name1\"}}," +
"{\"snippet\":{\"resourceId\":{\"channelId\":\"UCEOXxzW2vU0P-0THehuIIeg\"},\"title\":\"name2\"}}]"; "{\"snippet\":{\"resourceId\":{\"channelId\":\"UCEOXxzW2vU0P-0THehuIIeg\"},\"title\":\"name2\"}}]";
final List<SubscriptionItem> items = subscriptionExtractor.fromInputStream( final List<SubscriptionItem> items = subscriptionExtractor.fromInputStream(
new ByteArrayInputStream(source.getBytes(UTF_8))); new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8)));
assertEquals(1, items.size()); assertEquals(1, items.size());
assertEquals(ServiceList.YouTube.getServiceId(), items.get(0).getServiceId()); assertEquals(ServiceList.YouTube.getServiceId(), items.get(0).getServiceId());
@ -99,7 +102,7 @@ public class YoutubeSubscriptionExtractorTest {
for (String invalidContent : invalidList) { for (String invalidContent : invalidList) {
try { try {
byte[] bytes = invalidContent.getBytes(UTF_8); byte[] bytes = invalidContent.getBytes(StandardCharsets.UTF_8);
subscriptionExtractor.fromInputStream(new ByteArrayInputStream(bytes)); subscriptionExtractor.fromInputStream(new ByteArrayInputStream(bytes));
fail("Extracting from \"" + invalidContent + "\" didn't throw an exception"); fail("Extracting from \"" + invalidContent + "\" didn't throw an exception");
} catch (final Exception e) { } catch (final Exception e) {