Commit a8a4eaf8 authored by Mauricio Colli's avatar Mauricio Colli
Browse files

Change Json dependency again

The other had some incompatibilities with android.
parent aff595c4
apply plugin: 'java-library'
allprojects {
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
repositories {
jcenter()
}
dependencies {
implementation 'com.github.openjson:openjson:1.0.8'
implementation 'com.grack:nanojson:1.1'
implementation 'org.jsoup:jsoup:1.9.2'
implementation 'org.mozilla:rhino:1.7.7.1'
testImplementation 'junit:junit:4.12'
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
task sourcesJar(type: Jar, dependsOn: classes) {
......
......@@ -3,7 +3,6 @@ package org.schabi.newpipe.extractor.search;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import java.io.IOException;
import java.util.EnumSet;
/*
* Created by Christian Schabesberger on 10.08.15.
......@@ -27,7 +26,7 @@ import java.util.EnumSet;
public abstract class SearchEngine {
public enum Filter {
STREAM, CHANNEL, PLAYLIST
ANY, STREAM, CHANNEL, PLAYLIST
}
public static class NothingFoundException extends ExtractionException {
......@@ -46,8 +45,6 @@ public abstract class SearchEngine {
return collector;
}
//Result search(String query, int page);
public abstract InfoItemSearchCollector search(
String query, int page, String contentCountry, EnumSet<Filter> filter)
public abstract InfoItemSearchCollector search(String query, int page, String contentCountry, Filter filter)
throws IOException, ExtractionException;
}
......@@ -5,7 +5,6 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
/*
......@@ -29,8 +28,7 @@ import java.util.List;
*/
public class SearchResult {
public static SearchResult getSearchResult(SearchEngine engine, String query,
int page, String languageCode, EnumSet<SearchEngine.Filter> filter)
public static SearchResult getSearchResult(SearchEngine engine, String query, int page, String languageCode, SearchEngine.Filter filter)
throws IOException, ExtractionException {
SearchResult result = engine
......
package org.schabi.newpipe.extractor.services.soundcloud;
import com.github.openjson.JSONObject;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import org.schabi.newpipe.extractor.Downloader;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
......@@ -14,7 +16,7 @@ import java.io.IOException;
@SuppressWarnings("WeakerAccess")
public class SoundcloudChannelExtractor extends ChannelExtractor {
private String userId;
private JSONObject user;
private JsonObject user;
public SoundcloudChannelExtractor(StreamingService service, String url, String nextStreamsUrl) throws IOException, ExtractionException {
super(service, url, nextStreamsUrl);
......@@ -29,16 +31,16 @@ public class SoundcloudChannelExtractor extends ChannelExtractor {
"?client_id=" + SoundcloudParsingHelper.clientId();
String response = dl.download(apiUrl);
user = new JSONObject(response);
try {
user = JsonParser.object().from(response);
} catch (JsonParserException e) {
throw new ParsingException("Could not parse json response", e);
}
}
@Override
public String getCleanUrl() {
try {
return user.getString("permalink_url");
} catch (Exception e) {
return getOriginalUrl();
}
return user.isString("permalink_url") ? user.getString("permalink_url") : getOriginalUrl();
}
@Override
......@@ -53,16 +55,12 @@ public class SoundcloudChannelExtractor extends ChannelExtractor {
@Override
public String getAvatarUrl() {
return user.optString("avatar_url");
return user.getString("avatar_url");
}
@Override
public String getBannerUrl() throws ParsingException {
try {
return user.getJSONObject("visuals").getJSONArray("visuals").getJSONObject(0).getString("visual_url");
} catch (Exception e) {
throw new ParsingException("Could not get Banner", e);
}
return user.getObject("visuals").getArray("visuals").getObject(0).getString("visual_url", "");
}
@Override
......@@ -72,12 +70,12 @@ public class SoundcloudChannelExtractor extends ChannelExtractor {
@Override
public long getSubscriberCount() {
return user.optLong("followers_count");
return user.getNumber("followers_count", 0).longValue();
}
@Override
public String getDescription() throws ParsingException {
return user.optString("description");
return user.getString("description", "");
}
@Override
......
package org.schabi.newpipe.extractor.services.soundcloud;
import com.github.openjson.JSONObject;
import com.grack.nanojson.JsonObject;
import org.schabi.newpipe.extractor.channel.ChannelInfoItemExtractor;
public class SoundcloudChannelInfoItemExtractor implements ChannelInfoItemExtractor {
private JSONObject searchResult;
private JsonObject searchResult;
public SoundcloudChannelInfoItemExtractor(JSONObject searchResult) {
public SoundcloudChannelInfoItemExtractor(JsonObject searchResult) {
this.searchResult = searchResult;
}
......@@ -22,21 +22,21 @@ public class SoundcloudChannelInfoItemExtractor implements ChannelInfoItemExtrac
@Override
public String getThumbnailUrl() {
return searchResult.optString("avatar_url");
return searchResult.getString("avatar_url", "");
}
@Override
public long getSubscriberCount() {
return searchResult.optLong("followers_count");
return searchResult.getNumber("followers_count", 0).longValue();
}
@Override
public long getStreamCount() {
return searchResult.optLong("track_count");
return searchResult.getNumber("track_count", 0).longValue();
}
@Override
public String getDescription() {
return searchResult.optString("description");
return searchResult.getString("description", "");
}
}
package org.schabi.newpipe.extractor.services.soundcloud;
import com.github.openjson.JSONArray;
import com.github.openjson.JSONObject;
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
......@@ -65,12 +67,16 @@ public class SoundcloudParsingHelper {
* Call the endpoint "/resolve" of the api.<br/>
* See https://developers.soundcloud.com/docs/api/reference#resolve
*/
public static JSONObject resolveFor(String url) throws IOException, ReCaptchaException, ParsingException {
public static JsonObject resolveFor(String url) throws IOException, ReCaptchaException, ParsingException {
String apiUrl = "https://api.soundcloud.com/resolve"
+ "?url=" + URLEncoder.encode(url, "UTF-8")
+ "&client_id=" + clientId();
return new JSONObject(NewPipe.getDownloader().download(apiUrl));
try {
return JsonParser.object().from(NewPipe.getDownloader().download(apiUrl));
} catch (JsonParserException e) {
throw new ParsingException("Could not parse json response", e);
}
}
/**
......@@ -123,11 +129,16 @@ public class SoundcloudParsingHelper {
*/
public static String getStreamsFromApi(StreamInfoItemCollector collector, String apiUrl) throws IOException, ReCaptchaException, ParsingException {
String response = NewPipe.getDownloader().download(apiUrl);
JSONObject responseObject = new JSONObject(response);
JsonObject responseObject;
try {
responseObject = JsonParser.object().from(response);
} catch (JsonParserException e) {
throw new ParsingException("Could not parse json response", e);
}
JSONArray responseCollection = responseObject.getJSONArray("collection");
for (int i = 0; i < responseCollection.length(); i++) {
collector.commit(new SoundcloudStreamInfoItemExtractor(responseCollection.getJSONObject(i)));
JsonArray responseCollection = responseObject.getArray("collection");
for (Object o : responseCollection) {
if (o instanceof JsonObject) collector.commit(new SoundcloudStreamInfoItemExtractor((JsonObject) o));
}
String nextStreamsUrl;
......
package org.schabi.newpipe.extractor.services.soundcloud;
import com.github.openjson.JSONObject;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import org.schabi.newpipe.extractor.Downloader;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
......@@ -13,7 +16,7 @@ import java.io.IOException;
@SuppressWarnings("WeakerAccess")
public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
private String playlistId;
private JSONObject playlist;
private JsonObject playlist;
public SoundcloudPlaylistExtractor(StreamingService service, String url, String nextStreamsUrl) throws IOException, ExtractionException {
super(service, url, nextStreamsUrl);
......@@ -29,16 +32,16 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
"&representation=compact";
String response = dl.download(apiUrl);
playlist = new JSONObject(response);
try {
playlist = JsonParser.object().from(response);
} catch (JsonParserException e) {
throw new ParsingException("Could not parse json response", e);
}
}
@Override
public String getCleanUrl() {
try {
return playlist.getString("permalink_url");
} catch (Exception e) {
return getOriginalUrl();
}
return playlist.isString("permalink_url") ? playlist.getString("permalink_url") : getOriginalUrl();
}
@Override
......@@ -48,12 +51,12 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
@Override
public String getName() {
return playlist.optString("title");
return playlist.getString("title");
}
@Override
public String getThumbnailUrl() {
return playlist.optString("artwork_url");
return playlist.getString("artwork_url");
}
@Override
......@@ -63,22 +66,22 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
@Override
public String getUploaderUrl() {
return playlist.getJSONObject("user").getString("permalink_url");
return playlist.getObject("user").getString("permalink_url", "");
}
@Override
public String getUploaderName() {
return playlist.getJSONObject("user").getString("username");
return playlist.getObject("user").getString("username", "");
}
@Override
public String getUploaderAvatarUrl() {
return playlist.getJSONObject("user").getString("avatar_url");
return playlist.getObject("user", new JsonObject()).getString("avatar_url", "");
}
@Override
public long getStreamCount() {
return playlist.getLong("track_count");
return playlist.getNumber("track_count", 0).longValue();
}
@Override
......
package org.schabi.newpipe.extractor.services.soundcloud;
import com.github.openjson.JSONArray;
import com.github.openjson.JSONObject;
import com.grack.nanojson.JsonObject;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemExtractor;
......@@ -9,60 +8,54 @@ public class SoundcloudPlaylistInfoItemExtractor implements PlaylistInfoItemExtr
private static final String USER_KEY = "user";
private static final String AVATAR_URL_KEY = "avatar_url";
private static final String ARTWORK_URL_KEY = "artwork_url";
private static final String NULL_VALUE = "null";
private JSONObject searchResult;
private JsonObject searchResult;
public SoundcloudPlaylistInfoItemExtractor(JSONObject searchResult) {
public SoundcloudPlaylistInfoItemExtractor(JsonObject searchResult) {
this.searchResult = searchResult;
}
@Override
public String getName() throws ParsingException {
try {
return searchResult.getString("title");
} catch (Exception e) {
throw new ParsingException("Failed to extract playlist name", e);
}
return searchResult.getString("title");
}
@Override
public String getUrl() throws ParsingException {
try {
return searchResult.getString("permalink_url");
} catch (Exception e) {
throw new ParsingException("Failed to extract playlist name", e);
}
return searchResult.getString("permalink_url");
}
@Override
public String getThumbnailUrl() throws ParsingException {
// Over-engineering at its finest
try {
final String artworkUrl = searchResult.optString(ARTWORK_URL_KEY);
if (!artworkUrl.isEmpty() && !artworkUrl.equals(NULL_VALUE)) return artworkUrl;
if (searchResult.isString(ARTWORK_URL_KEY)) {
final String artworkUrl = searchResult.getString(ARTWORK_URL_KEY, "");
if (!artworkUrl.isEmpty()) return artworkUrl;
}
try {
// Look for artwork url inside the track list
final JSONArray tracks = searchResult.optJSONArray("tracks");
if (tracks == null) return null;
for (int i = 0; i < tracks.length(); i++) {
if (tracks.isNull(i)) continue;
final JSONObject track = tracks.optJSONObject(i);
if (track == null) continue;
for (Object track : searchResult.getArray("tracks")) {
final JsonObject trackObject = (JsonObject) track;
// First look for track artwork url
final String url = track.optString(ARTWORK_URL_KEY);
if (!url.isEmpty() && !url.equals(NULL_VALUE)) return url;
if (trackObject.isString(ARTWORK_URL_KEY)) {
final String url = trackObject.getString(ARTWORK_URL_KEY, "");
if (!url.isEmpty()) return url;
}
// Then look for track creator avatar url
final JSONObject creator = track.getJSONObject(USER_KEY);
final String creatorAvatar = creator.optString(AVATAR_URL_KEY);
if (!creatorAvatar.isEmpty() && !creatorAvatar.equals(NULL_VALUE)) return creatorAvatar;
final JsonObject creator = trackObject.getObject(USER_KEY, new JsonObject());
final String creatorAvatar = creator.getString(AVATAR_URL_KEY, "");
if (!creatorAvatar.isEmpty()) return creatorAvatar;
}
} catch (Exception ignored) {
// Try other method
}
try {
// Last resort, use user avatar url. If still not found, then throw exception.
final JSONObject user = searchResult.getJSONObject(USER_KEY);
return user.getString(AVATAR_URL_KEY);
return searchResult.getObject(USER_KEY).getString(AVATAR_URL_KEY, "");
} catch (Exception e) {
throw new ParsingException("Failed to extract playlist thumbnail url", e);
}
......@@ -71,8 +64,7 @@ public class SoundcloudPlaylistInfoItemExtractor implements PlaylistInfoItemExtr
@Override
public String getUploaderName() throws ParsingException {
try {
final JSONObject user = searchResult.getJSONObject(USER_KEY);
return user.optString("username");
return searchResult.getObject(USER_KEY).getString("username");
} catch (Exception e) {
throw new ParsingException("Failed to extract playlist uploader", e);
}
......@@ -80,10 +72,6 @@ public class SoundcloudPlaylistInfoItemExtractor implements PlaylistInfoItemExtr
@Override
public long getStreamCount() throws ParsingException {
try {
return Long.parseLong(searchResult.optString("track_count"));
} catch (Exception e) {
throw new ParsingException("Failed to extract playlist stream count", e);
}
return searchResult.getNumber("track_count", 0).longValue();
}
}
package org.schabi.newpipe.extractor.services.soundcloud;
import com.github.openjson.JSONArray;
import com.github.openjson.JSONObject;
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import org.schabi.newpipe.extractor.Downloader;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.search.InfoItemSearchCollector;
import org.schabi.newpipe.extractor.search.SearchEngine;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.EnumSet;
public class SoundcloudSearchEngine extends SearchEngine {
public static final String CHARSET_UTF_8 = "UTF-8";
......@@ -20,19 +22,27 @@ public class SoundcloudSearchEngine extends SearchEngine {
}
@Override
public InfoItemSearchCollector search(String query, int page, String languageCode, EnumSet<Filter> filter) throws IOException, ExtractionException {
public InfoItemSearchCollector search(String query, int page, String languageCode, Filter filter) throws IOException, ExtractionException {
InfoItemSearchCollector collector = getInfoItemSearchCollector();
Downloader downloader = NewPipe.getDownloader();
Downloader dl = NewPipe.getDownloader();
String url = "https://api-v2.soundcloud.com/search";
if (filter.contains(Filter.STREAM) && filter.size() == 1) {
url += "/tracks";
} else if (filter.contains(Filter.CHANNEL) && filter.size() == 1) {
url += "/users";
} else if (filter.contains(Filter.PLAYLIST) && filter.size() == 1) {
url += "/playlists";
switch (filter) {
case STREAM:
url += "/tracks";
break;
case CHANNEL:
url += "/users";
break;
case PLAYLIST:
url += "/playlists";
break;
case ANY:
// Don't append any parameter to search for everything
default:
break;
}
url += "?q=" + URLEncoder.encode(query, CHARSET_UTF_8)
......@@ -40,23 +50,32 @@ public class SoundcloudSearchEngine extends SearchEngine {
+ "&limit=10"
+ "&offset=" + Integer.toString(page * 10);
String searchJson = downloader.download(url);
JSONObject search = new JSONObject(searchJson);
JSONArray searchCollection = search.getJSONArray("collection");
JsonArray searchCollection;
try {
searchCollection = JsonParser.object().from(dl.download(url)).getArray("collection");
} catch (JsonParserException e) {
throw new ParsingException("Could not parse json response", e);
}
if (searchCollection.length() == 0) {
if (searchCollection.size() == 0) {
throw new NothingFoundException("Nothing found");
}
for (int i = 0; i < searchCollection.length(); i++) {
JSONObject searchResult = searchCollection.getJSONObject(i);
String kind = searchResult.getString("kind");
if (kind.equals("user")) {
collector.commit(new SoundcloudChannelInfoItemExtractor(searchResult));
} else if (kind.equals("track")) {
collector.commit(new SoundcloudStreamInfoItemExtractor(searchResult));
} else if (kind.equals("playlist")) {
collector.commit(new SoundcloudPlaylistInfoItemExtractor(searchResult));
for (Object result : searchCollection) {
if (!(result instanceof JsonObject)) continue;
//noinspection ConstantConditions
JsonObject searchResult = (JsonObject) result;
String kind = searchResult.getString("kind", "");
switch (kind) {
case "user":
collector.commit(new SoundcloudChannelInfoItemExtractor(searchResult));
break;
case "track":
collector.commit(new SoundcloudStreamInfoItemExtractor(searchResult));
break;
case "playlist":
collector.commit(new SoundcloudPlaylistInfoItemExtractor(searchResult));
break;
}
}
......
package org.schabi.newpipe.extractor.services.soundcloud;
import com.github.openjson.JSONObject;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import org.schabi.newpipe.extractor.Downloader;
import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.NewPipe;
......@@ -16,7 +18,7 @@ import java.util.ArrayList;
import java.util.List;
public class SoundcloudStreamExtractor extends StreamExtractor {
private JSONObject track;
private JsonObject track;
public SoundcloudStreamExtractor(StreamingService service, String url) throws IOException, ExtractionException {
super(service, url);
......@@ -26,7 +28,7 @@ public class SoundcloudStreamExtractor extends StreamExtractor {
public void fetchPage() throws IOException, ExtractionException {
track = SoundcloudParsingHelper.resolveFor(getOriginalUrl());