Skip to content

Commit a8a4eaf

Browse files
committed
Change Json dependency again
The other had some incompatibilities with android.
1 parent aff595c commit a8a4eaf

29 files changed

Lines changed: 288 additions & 257 deletions

build.gradle

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
apply plugin: 'java-library'
22

3+
allprojects {
4+
sourceCompatibility = 1.7
5+
targetCompatibility = 1.7
6+
}
7+
38
repositories {
49
jcenter()
510
}
611

712
dependencies {
8-
implementation 'com.github.openjson:openjson:1.0.8'
13+
implementation 'com.grack:nanojson:1.1'
914
implementation 'org.jsoup:jsoup:1.9.2'
1015
implementation 'org.mozilla:rhino:1.7.7.1'
1116

1217
testImplementation 'junit:junit:4.12'
13-
14-
sourceCompatibility = 1.7
15-
targetCompatibility = 1.7
1618
}
1719

1820
task sourcesJar(type: Jar, dependsOn: classes) {

src/main/java/org/schabi/newpipe/extractor/search/SearchEngine.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
44

55
import java.io.IOException;
6-
import java.util.EnumSet;
76

87
/*
98
* Created by Christian Schabesberger on 10.08.15.
@@ -27,7 +26,7 @@
2726

2827
public abstract class SearchEngine {
2928
public enum Filter {
30-
STREAM, CHANNEL, PLAYLIST
29+
ANY, STREAM, CHANNEL, PLAYLIST
3130
}
3231

3332
public static class NothingFoundException extends ExtractionException {
@@ -46,8 +45,6 @@ protected InfoItemSearchCollector getInfoItemSearchCollector() {
4645
return collector;
4746
}
4847

49-
//Result search(String query, int page);
50-
public abstract InfoItemSearchCollector search(
51-
String query, int page, String contentCountry, EnumSet<Filter> filter)
48+
public abstract InfoItemSearchCollector search(String query, int page, String contentCountry, Filter filter)
5249
throws IOException, ExtractionException;
5350
}

src/main/java/org/schabi/newpipe/extractor/search/SearchResult.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import java.io.IOException;
77
import java.util.ArrayList;
8-
import java.util.EnumSet;
98
import java.util.List;
109

1110
/*
@@ -29,8 +28,7 @@
2928
*/
3029

3130
public class SearchResult {
32-
public static SearchResult getSearchResult(SearchEngine engine, String query,
33-
int page, String languageCode, EnumSet<SearchEngine.Filter> filter)
31+
public static SearchResult getSearchResult(SearchEngine engine, String query, int page, String languageCode, SearchEngine.Filter filter)
3432
throws IOException, ExtractionException {
3533

3634
SearchResult result = engine

src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChannelExtractor.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.schabi.newpipe.extractor.services.soundcloud;
22

3-
import com.github.openjson.JSONObject;
3+
import com.grack.nanojson.JsonObject;
4+
import com.grack.nanojson.JsonParser;
5+
import com.grack.nanojson.JsonParserException;
46
import org.schabi.newpipe.extractor.Downloader;
57
import org.schabi.newpipe.extractor.NewPipe;
68
import org.schabi.newpipe.extractor.StreamingService;
@@ -14,7 +16,7 @@
1416
@SuppressWarnings("WeakerAccess")
1517
public class SoundcloudChannelExtractor extends ChannelExtractor {
1618
private String userId;
17-
private JSONObject user;
19+
private JsonObject user;
1820

1921
public SoundcloudChannelExtractor(StreamingService service, String url, String nextStreamsUrl) throws IOException, ExtractionException {
2022
super(service, url, nextStreamsUrl);
@@ -29,16 +31,16 @@ public void fetchPage() throws IOException, ExtractionException {
2931
"?client_id=" + SoundcloudParsingHelper.clientId();
3032

3133
String response = dl.download(apiUrl);
32-
user = new JSONObject(response);
34+
try {
35+
user = JsonParser.object().from(response);
36+
} catch (JsonParserException e) {
37+
throw new ParsingException("Could not parse json response", e);
38+
}
3339
}
3440

3541
@Override
3642
public String getCleanUrl() {
37-
try {
38-
return user.getString("permalink_url");
39-
} catch (Exception e) {
40-
return getOriginalUrl();
41-
}
43+
return user.isString("permalink_url") ? user.getString("permalink_url") : getOriginalUrl();
4244
}
4345

4446
@Override
@@ -53,16 +55,12 @@ public String getName() {
5355

5456
@Override
5557
public String getAvatarUrl() {
56-
return user.optString("avatar_url");
58+
return user.getString("avatar_url");
5759
}
5860

5961
@Override
6062
public String getBannerUrl() throws ParsingException {
61-
try {
62-
return user.getJSONObject("visuals").getJSONArray("visuals").getJSONObject(0).getString("visual_url");
63-
} catch (Exception e) {
64-
throw new ParsingException("Could not get Banner", e);
65-
}
63+
return user.getObject("visuals").getArray("visuals").getObject(0).getString("visual_url", "");
6664
}
6765

6866
@Override
@@ -72,12 +70,12 @@ public String getFeedUrl() throws ParsingException {
7270

7371
@Override
7472
public long getSubscriberCount() {
75-
return user.optLong("followers_count");
73+
return user.getNumber("followers_count", 0).longValue();
7674
}
7775

7876
@Override
7977
public String getDescription() throws ParsingException {
80-
return user.optString("description");
78+
return user.getString("description", "");
8179
}
8280

8381
@Override

src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChannelInfoItemExtractor.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package org.schabi.newpipe.extractor.services.soundcloud;
22

3-
import com.github.openjson.JSONObject;
3+
import com.grack.nanojson.JsonObject;
44
import org.schabi.newpipe.extractor.channel.ChannelInfoItemExtractor;
55

66
public class SoundcloudChannelInfoItemExtractor implements ChannelInfoItemExtractor {
7-
private JSONObject searchResult;
7+
private JsonObject searchResult;
88

9-
public SoundcloudChannelInfoItemExtractor(JSONObject searchResult) {
9+
public SoundcloudChannelInfoItemExtractor(JsonObject searchResult) {
1010
this.searchResult = searchResult;
1111
}
1212

@@ -22,21 +22,21 @@ public String getUrl() {
2222

2323
@Override
2424
public String getThumbnailUrl() {
25-
return searchResult.optString("avatar_url");
25+
return searchResult.getString("avatar_url", "");
2626
}
2727

2828
@Override
2929
public long getSubscriberCount() {
30-
return searchResult.optLong("followers_count");
30+
return searchResult.getNumber("followers_count", 0).longValue();
3131
}
3232

3333
@Override
3434
public long getStreamCount() {
35-
return searchResult.optLong("track_count");
35+
return searchResult.getNumber("track_count", 0).longValue();
3636
}
3737

3838
@Override
3939
public String getDescription() {
40-
return searchResult.optString("description");
40+
return searchResult.getString("description", "");
4141
}
4242
}

src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.schabi.newpipe.extractor.services.soundcloud;
22

3-
import com.github.openjson.JSONArray;
4-
import com.github.openjson.JSONObject;
3+
import com.grack.nanojson.JsonArray;
4+
import com.grack.nanojson.JsonObject;
5+
import com.grack.nanojson.JsonParser;
6+
import com.grack.nanojson.JsonParserException;
57
import org.jsoup.Jsoup;
68
import org.jsoup.nodes.Document;
79
import org.jsoup.nodes.Element;
@@ -65,12 +67,16 @@ public static String toDateString(String time) throws ParsingException {
6567
* Call the endpoint "/resolve" of the api.<br/>
6668
* See https://developers.soundcloud.com/docs/api/reference#resolve
6769
*/
68-
public static JSONObject resolveFor(String url) throws IOException, ReCaptchaException, ParsingException {
70+
public static JsonObject resolveFor(String url) throws IOException, ReCaptchaException, ParsingException {
6971
String apiUrl = "https://api.soundcloud.com/resolve"
7072
+ "?url=" + URLEncoder.encode(url, "UTF-8")
7173
+ "&client_id=" + clientId();
7274

73-
return new JSONObject(NewPipe.getDownloader().download(apiUrl));
75+
try {
76+
return JsonParser.object().from(NewPipe.getDownloader().download(apiUrl));
77+
} catch (JsonParserException e) {
78+
throw new ParsingException("Could not parse json response", e);
79+
}
7480
}
7581

7682
/**
@@ -123,11 +129,16 @@ public static String getStreamsFromApiMinItems(int minItems, StreamInfoItemColle
123129
*/
124130
public static String getStreamsFromApi(StreamInfoItemCollector collector, String apiUrl) throws IOException, ReCaptchaException, ParsingException {
125131
String response = NewPipe.getDownloader().download(apiUrl);
126-
JSONObject responseObject = new JSONObject(response);
132+
JsonObject responseObject;
133+
try {
134+
responseObject = JsonParser.object().from(response);
135+
} catch (JsonParserException e) {
136+
throw new ParsingException("Could not parse json response", e);
137+
}
127138

128-
JSONArray responseCollection = responseObject.getJSONArray("collection");
129-
for (int i = 0; i < responseCollection.length(); i++) {
130-
collector.commit(new SoundcloudStreamInfoItemExtractor(responseCollection.getJSONObject(i)));
139+
JsonArray responseCollection = responseObject.getArray("collection");
140+
for (Object o : responseCollection) {
141+
if (o instanceof JsonObject) collector.commit(new SoundcloudStreamInfoItemExtractor((JsonObject) o));
131142
}
132143

133144
String nextStreamsUrl;

src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudPlaylistExtractor.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package org.schabi.newpipe.extractor.services.soundcloud;
22

3-
import com.github.openjson.JSONObject;
3+
import com.grack.nanojson.JsonObject;
4+
import com.grack.nanojson.JsonParser;
5+
import com.grack.nanojson.JsonParserException;
46
import org.schabi.newpipe.extractor.Downloader;
57
import org.schabi.newpipe.extractor.NewPipe;
68
import org.schabi.newpipe.extractor.StreamingService;
79
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
10+
import org.schabi.newpipe.extractor.exceptions.ParsingException;
811
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
912
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
1013

@@ -13,7 +16,7 @@
1316
@SuppressWarnings("WeakerAccess")
1417
public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
1518
private String playlistId;
16-
private JSONObject playlist;
19+
private JsonObject playlist;
1720

1821
public SoundcloudPlaylistExtractor(StreamingService service, String url, String nextStreamsUrl) throws IOException, ExtractionException {
1922
super(service, url, nextStreamsUrl);
@@ -29,16 +32,16 @@ public void fetchPage() throws IOException, ExtractionException {
2932
"&representation=compact";
3033

3134
String response = dl.download(apiUrl);
32-
playlist = new JSONObject(response);
35+
try {
36+
playlist = JsonParser.object().from(response);
37+
} catch (JsonParserException e) {
38+
throw new ParsingException("Could not parse json response", e);
39+
}
3340
}
3441

3542
@Override
3643
public String getCleanUrl() {
37-
try {
38-
return playlist.getString("permalink_url");
39-
} catch (Exception e) {
40-
return getOriginalUrl();
41-
}
44+
return playlist.isString("permalink_url") ? playlist.getString("permalink_url") : getOriginalUrl();
4245
}
4346

4447
@Override
@@ -48,12 +51,12 @@ public String getId() {
4851

4952
@Override
5053
public String getName() {
51-
return playlist.optString("title");
54+
return playlist.getString("title");
5255
}
5356

5457
@Override
5558
public String getThumbnailUrl() {
56-
return playlist.optString("artwork_url");
59+
return playlist.getString("artwork_url");
5760
}
5861

5962
@Override
@@ -63,22 +66,22 @@ public String getBannerUrl() {
6366

6467
@Override
6568
public String getUploaderUrl() {
66-
return playlist.getJSONObject("user").getString("permalink_url");
69+
return playlist.getObject("user").getString("permalink_url", "");
6770
}
6871

6972
@Override
7073
public String getUploaderName() {
71-
return playlist.getJSONObject("user").getString("username");
74+
return playlist.getObject("user").getString("username", "");
7275
}
7376

7477
@Override
7578
public String getUploaderAvatarUrl() {
76-
return playlist.getJSONObject("user").getString("avatar_url");
79+
return playlist.getObject("user", new JsonObject()).getString("avatar_url", "");
7780
}
7881

7982
@Override
8083
public long getStreamCount() {
81-
return playlist.getLong("track_count");
84+
return playlist.getNumber("track_count", 0).longValue();
8285
}
8386

8487
@Override

0 commit comments

Comments
 (0)