Skip to content

Commit ce76885

Browse files
committed
removed generics
1 parent 8e27801 commit ce76885

5 files changed

Lines changed: 105 additions & 38 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ private String getNextPageUrl(JsonObject ajaxJson) throws IOException, ParsingEx
6161

6262
JsonArray arr;
6363
try {
64-
arr = JsonUtils.getValue(ajaxJson, "response.continuationContents.itemSectionContinuation.continuations");
65-
} catch (ParsingException e) {
64+
arr = (JsonArray) JsonUtils.getValue(ajaxJson, "response.continuationContents.itemSectionContinuation.continuations");
65+
} catch (Exception e) {
6666
return "";
6767
}
68-
if(null == arr || arr.isEmpty()) {
68+
if(arr.isEmpty()) {
6969
return "";
7070
}
7171
String continuation;
7272
try {
73-
continuation = JsonUtils.getValue(arr.getObject(0), "nextContinuationData.continuation");
74-
} catch (ParsingException e) {
73+
continuation = (String) JsonUtils.getValue(arr.getObject(0), "nextContinuationData.continuation");
74+
} catch (Exception e) {
7575
return "";
7676
}
7777
return getNextPageUrl(continuation);
@@ -109,22 +109,34 @@ public InfoItemsPage<CommentsInfoItem> getPage(String pageUrl) throws IOExceptio
109109

110110
private void collectCommentsFrom(CommentsInfoItemsCollector collector, JsonObject ajaxJson, String pageUrl) throws ParsingException {
111111

112-
113-
JsonArray contents = JsonUtils.getValue(ajaxJson, "response.continuationContents.itemSectionContinuation.contents");
112+
JsonArray contents;
113+
try {
114+
contents = (JsonArray) JsonUtils.getValue(ajaxJson, "response.continuationContents.itemSectionContinuation.contents");
115+
}catch(Exception e) {
116+
throw new ParsingException("unable to get parse youtube comments", e);
117+
}
114118
fetchTitle(contents);
115-
List<JsonObject> comments = JsonUtils.getValues(contents, "commentThreadRenderer.comment.commentRenderer");
119+
List<Object> comments;
120+
try {
121+
comments = JsonUtils.getValues(contents, "commentThreadRenderer.comment.commentRenderer");
122+
}catch(Exception e) {
123+
throw new ParsingException("unable to get parse youtube comments", e);
124+
}
125+
116126

117-
for(JsonObject c: comments) {
118-
CommentsInfoItemExtractor extractor = new YoutubeCommentsInfoItemExtractor(c, pageUrl);
119-
collector.commit(extractor);
127+
for(Object c: comments) {
128+
if(c instanceof JsonObject) {
129+
CommentsInfoItemExtractor extractor = new YoutubeCommentsInfoItemExtractor((JsonObject) c, pageUrl);
130+
collector.commit(extractor);
131+
}
120132
}
121133

122134
}
123135

124136
private void fetchTitle(JsonArray contents) {
125137
if(null == title) {
126138
try {
127-
title = JsonUtils.getValue(contents.getObject(0), "commentThreadRenderer.commentTargetTitle.simpleText");
139+
title = (String) JsonUtils.getValue(contents.getObject(0), "commentThreadRenderer.commentTargetTitle.simpleText");
128140
} catch (Exception e) {
129141
title = "Youtube Comments";
130142
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
import com.grack.nanojson.JsonArray;
88
import com.grack.nanojson.JsonObject;
99

10-
public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor{
11-
10+
public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
11+
1212
private final JsonObject json;
1313
private final String url;
14-
14+
1515
public YoutubeCommentsInfoItemExtractor(JsonObject json, String url) {
1616
this.json = json;
1717
this.url = url;
@@ -24,55 +24,92 @@ public String getUrl() throws ParsingException {
2424

2525
@Override
2626
public String getThumbnailUrl() throws ParsingException {
27-
JsonArray arr = JsonUtils.getValue(json, "authorThumbnail.thumbnails");
28-
return JsonUtils.getValue(arr.getObject(2), "url");
27+
try {
28+
JsonArray arr = (JsonArray) JsonUtils.getValue(json, "authorThumbnail.thumbnails");
29+
return (String) JsonUtils.getValue(arr.getObject(2), "url");
30+
} catch (Exception e) {
31+
throw new ParsingException("Could not get thumbnail url", e);
32+
}
2933
}
3034

3135
@Override
3236
public String getName() throws ParsingException {
33-
return JsonUtils.getValue(json, "authorText.simpleText");
37+
try {
38+
return (String) JsonUtils.getValue(json, "authorText.simpleText");
39+
} catch (Exception e) {
40+
throw new ParsingException("Could not get author name", e);
41+
}
3442
}
3543

3644
@Override
3745
public String getPublishedTime() throws ParsingException {
38-
JsonArray arr = JsonUtils.getValue(json, "publishedTimeText.runs");
39-
return JsonUtils.getValue(arr.getObject(0), "text");
46+
try {
47+
JsonArray arr = (JsonArray) JsonUtils.getValue(json, "publishedTimeText.runs");
48+
return (String) JsonUtils.getValue(arr.getObject(0), "text");
49+
} catch (Exception e) {
50+
throw new ParsingException("Could not get publishedTimeText", e);
51+
}
4052
}
4153

4254
@Override
4355
public Integer getLikeCount() throws ParsingException {
44-
return JsonUtils.getValue(json, "likeCount");
56+
try {
57+
return (Integer) JsonUtils.getValue(json, "likeCount");
58+
} catch (Exception e) {
59+
throw new ParsingException("Could not get like count", e);
60+
}
4561
}
4662

4763
@Override
4864
public String getCommentText() throws ParsingException {
4965
try {
50-
return JsonUtils.getValue(json, "contentText.simpleText");
51-
} catch (Exception e) {
52-
JsonArray arr = JsonUtils.getValue(json, "contentText.runs");
53-
return JsonUtils.getValue(arr.getObject(0), "text");
66+
return (String) JsonUtils.getValue(json, "contentText.simpleText");
67+
} catch (Exception e1) {
68+
try {
69+
JsonArray arr = (JsonArray) JsonUtils.getValue(json, "contentText.runs");
70+
return (String) JsonUtils.getValue(arr.getObject(0), "text");
71+
} catch (Exception e2) {
72+
throw new ParsingException("Could not get comment text", e2);
73+
}
5474
}
5575
}
5676

5777
@Override
5878
public String getCommentId() throws ParsingException {
59-
return JsonUtils.getValue(json, "commentId");
79+
try {
80+
return (String) JsonUtils.getValue(json, "commentId");
81+
} catch (Exception e) {
82+
throw new ParsingException("Could not get comment id", e);
83+
}
6084
}
6185

6286
@Override
6387
public String getAuthorThumbnail() throws ParsingException {
64-
JsonArray arr = JsonUtils.getValue(json, "authorThumbnail.thumbnails");
65-
return JsonUtils.getValue(arr.getObject(2), "url");
88+
try {
89+
JsonArray arr = (JsonArray) JsonUtils.getValue(json, "authorThumbnail.thumbnails");
90+
return (String) JsonUtils.getValue(arr.getObject(2), "url");
91+
} catch (Exception e) {
92+
throw new ParsingException("Could not get author thumbnail", e);
93+
}
6694
}
6795

6896
@Override
6997
public String getAuthorName() throws ParsingException {
70-
return JsonUtils.getValue(json, "authorText.simpleText");
98+
try {
99+
return (String) JsonUtils.getValue(json, "authorText.simpleText");
100+
} catch (Exception e) {
101+
throw new ParsingException("Could not get author name", e);
102+
}
71103
}
72104

73105
@Override
74106
public String getAuthorEndpoint() throws ParsingException {
75-
return "https://youtube.com" + JsonUtils.getValue(json, "authorEndpoint.browseEndpoint.canonicalBaseUrl");
107+
try {
108+
return "https://youtube.com"
109+
+ (String) JsonUtils.getValue(json, "authorEndpoint.browseEndpoint.canonicalBaseUrl");
110+
} catch (Exception e) {
111+
throw new ParsingException("Could not get author endpoint", e);
112+
}
76113
}
77114

78115
}

extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ private JsonUtils() {
1818
}
1919

2020
@Nonnull
21-
public static <T> T getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
21+
public static Object getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
2222

2323
List<String> keys = Arrays.asList(path.split("\\."));
2424
object = getObject(object, keys.subList(0, keys.size() - 1));
2525
if (null == object) throw new ParsingException("Unable to get " + path);
26-
T result = (T) object.get(keys.get(keys.size() - 1));
26+
Object result = object.get(keys.get(keys.size() - 1));
2727
if(null == result) throw new ParsingException("Unable to get " + path);
2828
return result;
2929
}
3030

3131

3232
@Nonnull
33-
public static <T> List<T> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException {
33+
public static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException {
3434

35-
List<T> result = new ArrayList<>();
35+
List<Object> result = new ArrayList<>();
3636
for (int i = 0; i < array.size(); i++) {
3737
JsonObject obj = array.getObject(i);
38-
result.add((T)getValue(obj, path));
38+
result.add(getValue(obj, path));
3939
}
4040
return result;
4141
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.schabi.newpipe.extractor.services.youtube;
22

3+
import static org.junit.Assert.assertFalse;
34
import static org.junit.Assert.assertTrue;
45
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
56

@@ -58,6 +59,23 @@ public void testGetCommentsFromCommentsInfo() throws IOException, ExtractionExce
5859

5960
assertTrue(result);
6061
}
62+
63+
@Test
64+
public void testGetCommentsAllData() throws IOException, ExtractionException {
65+
InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();
66+
for(CommentsInfoItem c: comments.getItems()) {
67+
assertFalse(StringUtil.isBlank(c.getAuthorEndpoint()));
68+
assertFalse(StringUtil.isBlank(c.getAuthorName()));
69+
assertFalse(StringUtil.isBlank(c.getAuthorThumbnail()));
70+
assertFalse(StringUtil.isBlank(c.getCommentId()));
71+
assertFalse(StringUtil.isBlank(c.getCommentText()));
72+
assertFalse(StringUtil.isBlank(c.getName()));
73+
assertFalse(StringUtil.isBlank(c.getPublishedTime()));
74+
assertFalse(StringUtil.isBlank(c.getThumbnailUrl()));
75+
assertFalse(StringUtil.isBlank(c.getUrl()));
76+
assertFalse(c.getLikeCount() == null);
77+
}
78+
}
6179

6280
private boolean findInComments(InfoItemsPage<CommentsInfoItem> comments, String comment) {
6381
return findInComments(comments.getItems(), comment);

extractor/src/test/java/org/schabi/newpipe/extractor/utils/JsonUtilsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ public void testGetValueNested() throws JsonParserException, ParsingException {
3131
@Test
3232
public void testGetArray() throws JsonParserException, ParsingException {
3333
JsonObject obj = JsonParser.object().from("{\"id\":\"0001\",\"type\":\"donut\",\"name\":\"Cake\",\"ppu\":0.55,\"batters\":{\"batter\":[{\"id\":\"1001\",\"type\":\"Regular\"},{\"id\":\"1002\",\"type\":\"Chocolate\"},{\"id\":\"1003\",\"type\":\"Blueberry\"},{\"id\":\"1004\",\"type\":\"Devil's Food\"}]},\"topping\":[{\"id\":\"5001\",\"type\":\"None\"},{\"id\":\"5002\",\"type\":\"Glazed\"},{\"id\":\"5005\",\"type\":\"Sugar\"},{\"id\":\"5007\",\"type\":\"Powdered Sugar\"},{\"id\":\"5006\",\"type\":\"Chocolate with Sprinkles\"},{\"id\":\"5003\",\"type\":\"Chocolate\"},{\"id\":\"5004\",\"type\":\"Maple\"}]}");
34-
JsonArray arr = JsonUtils.getValue(obj, "batters.batter");
34+
JsonArray arr = (JsonArray) JsonUtils.getValue(obj, "batters.batter");
3535
assertTrue(!arr.isEmpty());
3636
}
3737

3838
@Test
3939
public void testGetValues() throws JsonParserException, ParsingException {
4040
JsonObject obj = JsonParser.object().from("{\"id\":\"0001\",\"type\":\"donut\",\"name\":\"Cake\",\"ppu\":0.55,\"batters\":{\"batter\":[{\"id\":\"1001\",\"type\":\"Regular\"},{\"id\":\"1002\",\"type\":\"Chocolate\"},{\"id\":\"1003\",\"type\":\"Blueberry\"},{\"id\":\"1004\",\"type\":\"Devil's Food\"}]},\"topping\":[{\"id\":\"5001\",\"type\":\"None\"},{\"id\":\"5002\",\"type\":\"Glazed\"},{\"id\":\"5005\",\"type\":\"Sugar\"},{\"id\":\"5007\",\"type\":\"Powdered Sugar\"},{\"id\":\"5006\",\"type\":\"Chocolate with Sprinkles\"},{\"id\":\"5003\",\"type\":\"Chocolate\"},{\"id\":\"5004\",\"type\":\"Maple\"}]}");
41-
JsonArray arr = JsonUtils.getValue(obj, "topping");
42-
List<String> types = JsonUtils.getValues(arr, "type");
41+
JsonArray arr = (JsonArray) JsonUtils.getValue(obj, "topping");
42+
List<Object> types = JsonUtils.getValues(arr, "type");
4343
assertTrue(types.contains("Chocolate with Sprinkles"));
4444

4545
}

0 commit comments

Comments
 (0)