Search in sources :

Example 16 with JsonParserException

use of com.grack.nanojson.JsonParserException in project NewPipe by TeamNewPipe.

the class PeertubeHelper method getInstanceList.

public static List<PeertubeInstance> getInstanceList(final Context context) {
    final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    final String savedInstanceListKey = context.getString(R.string.peertube_instance_list_key);
    final String savedJson = sharedPreferences.getString(savedInstanceListKey, null);
    if (null == savedJson) {
        return Collections.singletonList(getCurrentInstance());
    }
    try {
        final JsonArray array = JsonParser.object().from(savedJson).getArray("instances");
        final List<PeertubeInstance> result = new ArrayList<>();
        for (final Object o : array) {
            if (o instanceof JsonObject) {
                final JsonObject instance = (JsonObject) o;
                final String name = instance.getString("name");
                final String url = instance.getString("url");
                result.add(new PeertubeInstance(url, name));
            }
        }
        return result;
    } catch (final JsonParserException e) {
        return Collections.singletonList(getCurrentInstance());
    }
}
Also used : JsonArray(com.grack.nanojson.JsonArray) SharedPreferences(android.content.SharedPreferences) JsonParserException(com.grack.nanojson.JsonParserException) ArrayList(java.util.ArrayList) PeertubeInstance(org.schabi.newpipe.extractor.services.peertube.PeertubeInstance) JsonObject(com.grack.nanojson.JsonObject) JsonObject(com.grack.nanojson.JsonObject)

Example 17 with JsonParserException

use of com.grack.nanojson.JsonParserException in project NewPipeExtractor by TeamNewPipe.

the class SoundcloudSearchExtractor method onFetchPage.

@Override
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException, ExtractionException {
    final Downloader dl = getDownloader();
    final String url = getUrl();
    try {
        final String response = dl.get(url, getExtractorLocalization()).responseBody();
        initialSearchCollection = JsonParser.object().from(response).getArray("collection");
    } catch (final JsonParserException e) {
        throw new ParsingException("Could not parse json response", e);
    }
    if (initialSearchCollection.isEmpty()) {
        throw new SearchExtractor.NothingFoundException("Nothing found");
    }
}
Also used : JsonParserException(com.grack.nanojson.JsonParserException) ParsingException(org.schabi.newpipe.extractor.exceptions.ParsingException) Downloader(org.schabi.newpipe.extractor.downloader.Downloader)

Example 18 with JsonParserException

use of com.grack.nanojson.JsonParserException in project NewPipeExtractor by TeamNewPipe.

the class SoundcloudSearchExtractor method getPage.

@Override
public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException, ExtractionException {
    if (page == null || isNullOrEmpty(page.getUrl())) {
        throw new IllegalArgumentException("Page doesn't contain an URL");
    }
    final Downloader dl = getDownloader();
    final JsonArray searchCollection;
    try {
        final String response = dl.get(page.getUrl(), getExtractorLocalization()).responseBody();
        searchCollection = JsonParser.object().from(response).getArray("collection");
    } catch (final JsonParserException e) {
        throw new ParsingException("Could not parse json response", e);
    }
    return new InfoItemsPage<>(collectItems(searchCollection), getNextPageFromCurrentUrl(page.getUrl(), currentOffset -> currentOffset + ITEMS_PER_PAGE));
}
Also used : JsonArray(com.grack.nanojson.JsonArray) JsonObject(com.grack.nanojson.JsonObject) org.schabi.newpipe.extractor(org.schabi.newpipe.extractor) SearchExtractor(org.schabi.newpipe.extractor.search.SearchExtractor) MalformedURLException(java.net.MalformedURLException) IntUnaryOperator(java.util.function.IntUnaryOperator) Parser(org.schabi.newpipe.extractor.utils.Parser) URL(java.net.URL) EMPTY_STRING(org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING) Downloader(org.schabi.newpipe.extractor.downloader.Downloader) IOException(java.io.IOException) InfoItemsSearchCollector(org.schabi.newpipe.extractor.search.InfoItemsSearchCollector) SearchQueryHandler(org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler) List(java.util.List) JsonParser(com.grack.nanojson.JsonParser) JsonArray(com.grack.nanojson.JsonArray) JsonParserException(com.grack.nanojson.JsonParserException) ITEMS_PER_PAGE(org.schabi.newpipe.extractor.services.soundcloud.linkHandler.SoundcloudSearchQueryHandlerFactory.ITEMS_PER_PAGE) Utils.isNullOrEmpty(org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty) ParsingException(org.schabi.newpipe.extractor.exceptions.ParsingException) Nonnull(javax.annotation.Nonnull) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) ExtractionException(org.schabi.newpipe.extractor.exceptions.ExtractionException) JsonParserException(com.grack.nanojson.JsonParserException) ParsingException(org.schabi.newpipe.extractor.exceptions.ParsingException) Downloader(org.schabi.newpipe.extractor.downloader.Downloader)

Example 19 with JsonParserException

use of com.grack.nanojson.JsonParserException in project NewPipeExtractor by TeamNewPipe.

the class SoundcloudStreamExtractor method getTranscodingUrl.

@Nonnull
private static String getTranscodingUrl(final String endpointUrl, final String protocol) throws IOException, ExtractionException {
    final Downloader downloader = NewPipe.getDownloader();
    final String apiStreamUrl = endpointUrl + "?client_id=" + SoundcloudParsingHelper.clientId();
    final String response = downloader.get(apiStreamUrl).responseBody();
    final JsonObject urlObject;
    try {
        urlObject = JsonParser.object().from(response);
    } catch (final JsonParserException e) {
        throw new ParsingException("Could not parse streamable url", e);
    }
    final String urlString = urlObject.getString("url");
    if (protocol.equals("progressive")) {
        return urlString;
    } else if (protocol.equals("hls")) {
        try {
            return getSingleUrlFromHlsManifest(urlString);
        } catch (final ParsingException ignored) {
        }
    }
    // else, unknown protocol
    return "";
}
Also used : JsonParserException(com.grack.nanojson.JsonParserException) ParsingException(org.schabi.newpipe.extractor.exceptions.ParsingException) Downloader(org.schabi.newpipe.extractor.downloader.Downloader) JsonObject(com.grack.nanojson.JsonObject) Nonnull(javax.annotation.Nonnull)

Example 20 with JsonParserException

use of com.grack.nanojson.JsonParserException in project NewPipeExtractor by TeamNewPipe.

the class SoundcloudSuggestionExtractor method suggestionList.

@Override
public List<String> suggestionList(final String query) throws IOException, ExtractionException {
    final List<String> suggestions = new ArrayList<>();
    final Downloader dl = NewPipe.getDownloader();
    final String url = SOUNDCLOUD_API_V2_URL + "search/queries" + "?q=" + URLEncoder.encode(query, UTF_8) + "&client_id=" + SoundcloudParsingHelper.clientId() + "&limit=10";
    final String response = dl.get(url, getExtractorLocalization()).responseBody();
    try {
        final JsonArray collection = JsonParser.object().from(response).getArray("collection");
        for (final Object suggestion : collection) {
            if (suggestion instanceof JsonObject)
                suggestions.add(((JsonObject) suggestion).getString("query"));
        }
        return suggestions;
    } catch (final JsonParserException e) {
        throw new ParsingException("Could not parse json response", e);
    }
}
Also used : JsonArray(com.grack.nanojson.JsonArray) JsonParserException(com.grack.nanojson.JsonParserException) ParsingException(org.schabi.newpipe.extractor.exceptions.ParsingException) ArrayList(java.util.ArrayList) Downloader(org.schabi.newpipe.extractor.downloader.Downloader) JsonObject(com.grack.nanojson.JsonObject) JsonObject(com.grack.nanojson.JsonObject)

Aggregations

JsonParserException (com.grack.nanojson.JsonParserException)30 JsonObject (com.grack.nanojson.JsonObject)20 ParsingException (org.schabi.newpipe.extractor.exceptions.ParsingException)18 JsonArray (com.grack.nanojson.JsonArray)11 Downloader (org.schabi.newpipe.extractor.downloader.Downloader)11 IOException (java.io.IOException)10 ReCaptchaException (org.schabi.newpipe.extractor.exceptions.ReCaptchaException)9 Response (org.schabi.newpipe.extractor.downloader.Response)8 ExtractionException (org.schabi.newpipe.extractor.exceptions.ExtractionException)8 ArrayList (java.util.ArrayList)5 MalformedURLException (java.net.MalformedURLException)4 Nonnull (javax.annotation.Nonnull)4 Page (org.schabi.newpipe.extractor.Page)3 SharedPreferences (android.content.SharedPreferences)2 URL (java.net.URL)2 DateTimeParseException (java.time.format.DateTimeParseException)2 CommentsInfoItemsCollector (org.schabi.newpipe.extractor.comments.CommentsInfoItemsCollector)2 PeertubeInstance (org.schabi.newpipe.extractor.services.peertube.PeertubeInstance)2 RegexException (org.schabi.newpipe.extractor.utils.Parser.RegexException)2 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1