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;
}
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);
}
}
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;
}
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")));
}
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);
}
Aggregations