Search in sources :

Example 21 with JsonParserException

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

the class YoutubeSubscriptionExtractor method fromJsonInputStream.

public List<SubscriptionItem> fromJsonInputStream(@Nonnull final InputStream contentInputStream) throws ExtractionException {
    final JsonArray subscriptions;
    try {
        subscriptions = JsonParser.array().from(contentInputStream);
    } catch (JsonParserException e) {
        throw new InvalidSourceException("Invalid json input stream", e);
    }
    boolean foundInvalidSubscription = false;
    final List<SubscriptionItem> subscriptionItems = new ArrayList<>();
    for (final Object subscriptionObject : subscriptions) {
        if (!(subscriptionObject instanceof JsonObject)) {
            foundInvalidSubscription = true;
            continue;
        }
        final JsonObject subscription = ((JsonObject) subscriptionObject).getObject("snippet");
        final String id = subscription.getObject("resourceId").getString("channelId", "");
        if (id.length() != 24) {
            // e.g. UCsXVk37bltHxD1rDPwtNM8Q
            foundInvalidSubscription = true;
            continue;
        }
        subscriptionItems.add(new SubscriptionItem(service.getServiceId(), BASE_CHANNEL_URL + id, subscription.getString("title", "")));
    }
    if (foundInvalidSubscription && subscriptionItems.isEmpty()) {
        throw new InvalidSourceException("Found only invalid channel ids");
    }
    return subscriptionItems;
}
Also used : JsonArray(com.grack.nanojson.JsonArray) SubscriptionItem(org.schabi.newpipe.extractor.subscription.SubscriptionItem) JsonParserException(com.grack.nanojson.JsonParserException) ArrayList(java.util.ArrayList) JsonObject(com.grack.nanojson.JsonObject) JsonObject(com.grack.nanojson.JsonObject)

Example 22 with JsonParserException

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

the class SoundcloudParsingHelper method resolveIdWithWidgetApi.

/**
 * Fetch the widget API with the url and return the id (like the id from the json API).
 *
 * @return the resolved id
 */
public static String resolveIdWithWidgetApi(String urlString) throws IOException, ParsingException {
    // Remove the tailing slash from URLs due to issues with the SoundCloud API
    if (urlString.charAt(urlString.length() - 1) == '/')
        urlString = urlString.substring(0, urlString.length() - 1);
    // Make URL lower case and remove m. and www. if it exists.
    // Without doing this, the widget API does not recognize the URL.
    urlString = Utils.removeMAndWWWFromUrl(urlString.toLowerCase());
    final URL url;
    try {
        url = Utils.stringToURL(urlString);
    } catch (final MalformedURLException e) {
        throw new IllegalArgumentException("The given URL is not valid");
    }
    try {
        final String widgetUrl = "https://api-widget.soundcloud.com/resolve?url=" + URLEncoder.encode(url.toString(), UTF_8) + "&format=json&client_id=" + SoundcloudParsingHelper.clientId();
        final String response = NewPipe.getDownloader().get(widgetUrl, SoundCloud.getLocalization()).responseBody();
        final JsonObject o = JsonParser.object().from(response);
        return String.valueOf(JsonUtils.getValue(o, "id"));
    } catch (final JsonParserException e) {
        throw new ParsingException("Could not parse JSON response", e);
    } catch (final ExtractionException e) {
        throw new ParsingException("Could not resolve id with embedded player. ClientId not extracted", e);
    }
}
Also used : ExtractionException(org.schabi.newpipe.extractor.exceptions.ExtractionException) MalformedURLException(java.net.MalformedURLException) JsonParserException(com.grack.nanojson.JsonParserException) ParsingException(org.schabi.newpipe.extractor.exceptions.ParsingException) JsonObject(com.grack.nanojson.JsonObject) URL(java.net.URL)

Example 23 with JsonParserException

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

the class SoundcloudParsingHelper method getUsersFromApi.

/**
 * Fetch the user items from the given API and commit each of them to the collector.
 *
 * @return the next streams url, empty if don't have
 */
public static String getUsersFromApi(final ChannelInfoItemsCollector collector, final String apiUrl) throws IOException, ReCaptchaException, ParsingException {
    final String response = NewPipe.getDownloader().get(apiUrl, SoundCloud.getLocalization()).responseBody();
    final JsonObject responseObject;
    try {
        responseObject = JsonParser.object().from(response);
    } catch (final JsonParserException e) {
        throw new ParsingException("Could not parse json response", e);
    }
    final JsonArray responseCollection = responseObject.getArray("collection");
    for (final Object o : responseCollection) {
        if (o instanceof JsonObject) {
            final JsonObject object = (JsonObject) o;
            collector.commit(new SoundcloudChannelInfoItemExtractor(object));
        }
    }
    String nextPageUrl;
    try {
        nextPageUrl = responseObject.getString("next_href");
        if (!nextPageUrl.contains("client_id="))
            nextPageUrl += "&client_id=" + SoundcloudParsingHelper.clientId();
    } catch (final Exception ignored) {
        nextPageUrl = "";
    }
    return nextPageUrl;
}
Also used : JsonArray(com.grack.nanojson.JsonArray) JsonParserException(com.grack.nanojson.JsonParserException) ParsingException(org.schabi.newpipe.extractor.exceptions.ParsingException) JsonObject(com.grack.nanojson.JsonObject) JsonObject(com.grack.nanojson.JsonObject) SoundcloudChannelInfoItemExtractor(org.schabi.newpipe.extractor.services.soundcloud.extractors.SoundcloudChannelInfoItemExtractor) ReCaptchaException(org.schabi.newpipe.extractor.exceptions.ReCaptchaException) JsonParserException(com.grack.nanojson.JsonParserException) ParsingException(org.schabi.newpipe.extractor.exceptions.ParsingException) ExtractionException(org.schabi.newpipe.extractor.exceptions.ExtractionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) DateTimeParseException(java.time.format.DateTimeParseException) RegexException(org.schabi.newpipe.extractor.utils.Parser.RegexException)

Example 24 with JsonParserException

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

the class SoundcloudCommentsExtractor method getInitialPage.

@Nonnull
@Override
public InfoItemsPage<CommentsInfoItem> getInitialPage() throws ExtractionException, IOException {
    final Downloader downloader = NewPipe.getDownloader();
    final Response response = downloader.get(getUrl());
    final JsonObject json;
    try {
        json = JsonParser.object().from(response.responseBody());
    } catch (final JsonParserException e) {
        throw new ParsingException("Could not parse json", e);
    }
    final CommentsInfoItemsCollector collector = new CommentsInfoItemsCollector(getServiceId());
    collectStreamsFrom(collector, json.getArray("collection"));
    return new InfoItemsPage<>(collector, new Page(json.getString("next_href")));
}
Also used : Response(org.schabi.newpipe.extractor.downloader.Response) JsonParserException(com.grack.nanojson.JsonParserException) ParsingException(org.schabi.newpipe.extractor.exceptions.ParsingException) Downloader(org.schabi.newpipe.extractor.downloader.Downloader) JsonObject(com.grack.nanojson.JsonObject) CommentsInfoItemsCollector(org.schabi.newpipe.extractor.comments.CommentsInfoItemsCollector) Page(org.schabi.newpipe.extractor.Page) Nonnull(javax.annotation.Nonnull)

Example 25 with JsonParserException

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

the class PeertubeStreamExtractor method getDescription.

@Nonnull
@Override
public Description getDescription() throws ParsingException {
    String text;
    try {
        text = JsonUtils.getString(json, "description");
    } catch (ParsingException e) {
        return Description.emptyDescription;
    }
    if (text.length() == 250 && text.substring(247).equals("...")) {
        // if description is shortened, get full description
        final Downloader dl = NewPipe.getDownloader();
        try {
            final Response response = dl.get(baseUrl + PeertubeStreamLinkHandlerFactory.VIDEO_API_ENDPOINT + getId() + "/description");
            final JsonObject jsonObject = JsonParser.object().from(response.responseBody());
            text = JsonUtils.getString(jsonObject, "description");
        } catch (ReCaptchaException | IOException | JsonParserException e) {
            e.printStackTrace();
        }
    }
    return new Description(text, Description.MARKDOWN);
}
Also used : Response(org.schabi.newpipe.extractor.downloader.Response) ParsingException(org.schabi.newpipe.extractor.exceptions.ParsingException) JsonParserException(com.grack.nanojson.JsonParserException) Downloader(org.schabi.newpipe.extractor.downloader.Downloader) JsonObject(com.grack.nanojson.JsonObject) IOException(java.io.IOException) ReCaptchaException(org.schabi.newpipe.extractor.exceptions.ReCaptchaException) Nonnull(javax.annotation.Nonnull)

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