Skip to content

Commit 2780e71

Browse files
authored
Merge pull request #321 from wb9688/fix-nanojson
Return null instead of "" in getTextFromObject()
2 parents 49157fc + a1eabc7 commit 2780e71

8 files changed

Lines changed: 65 additions & 41 deletions

File tree

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.grack.nanojson.JsonArray;
44
import com.grack.nanojson.JsonObject;
5+
56
import org.schabi.newpipe.extractor.StreamingService;
67
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
78
import org.schabi.newpipe.extractor.downloader.Downloader;
@@ -16,11 +17,14 @@
1617
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
1718
import org.schabi.newpipe.extractor.utils.Utils;
1819

19-
import javax.annotation.Nonnull;
2020
import java.io.IOException;
2121

22-
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.*;
23-
import static org.schabi.newpipe.extractor.utils.JsonUtils.*;
22+
import javax.annotation.Nonnull;
23+
24+
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
25+
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getJsonResponse;
26+
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
27+
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
2428

2529
/*
2630
* Created by Christian Schabesberger on 25.07.16.
@@ -306,10 +310,12 @@ private JsonObject getVideoTab() throws ParsingException {
306310
throw new ContentNotSupportedException("This channel has no Videos tab");
307311
}
308312

309-
if (getTextFromObject(videoTab.getObject("content").getObject("sectionListRenderer")
310-
.getArray("contents").getObject(0).getObject("itemSectionRenderer")
311-
.getArray("contents").getObject(0).getObject("messageRenderer")
312-
.getObject("text")).equals("This channel has no videos.")) {
313+
final String messageRendererText = getTextFromObject(videoTab.getObject("content")
314+
.getObject("sectionListRenderer").getArray("contents").getObject(0)
315+
.getObject("itemSectionRenderer").getArray("contents").getObject(0)
316+
.getObject("messageRenderer").getObject("text"));
317+
if (messageRendererText != null
318+
&& messageRendererText.equals("This channel has no videos.")) {
313319
return null;
314320
}
315321

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.grack.nanojson.JsonParser;
66
import com.grack.nanojson.JsonParserException;
77
import com.grack.nanojson.JsonWriter;
8+
89
import org.schabi.newpipe.extractor.InfoItem;
910
import org.schabi.newpipe.extractor.StreamingService;
1011
import org.schabi.newpipe.extractor.downloader.Downloader;
@@ -19,13 +20,14 @@
1920
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
2021
import org.schabi.newpipe.extractor.utils.Utils;
2122

22-
import javax.annotation.Nonnull;
2323
import java.io.IOException;
2424
import java.util.Collections;
2525
import java.util.HashMap;
2626
import java.util.List;
2727
import java.util.Map;
2828

29+
import javax.annotation.Nonnull;
30+
2931
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
3032
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
3133
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
@@ -249,7 +251,7 @@ public String getUrl() throws ParsingException {
249251
public String getName() throws ParsingException {
250252
final String name = getTextFromObject(info.getArray("flexColumns").getObject(0)
251253
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
252-
if (!name.isEmpty()) {
254+
if (name != null && !name.isEmpty()) {
253255
return name;
254256
}
255257
throw new ParsingException("Could not get name");
@@ -259,7 +261,7 @@ public String getName() throws ParsingException {
259261
public long getDuration() throws ParsingException {
260262
final String duration = getTextFromObject(info.getArray("flexColumns").getObject(3)
261263
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
262-
if (!duration.isEmpty()) {
264+
if (duration != null && !duration.isEmpty()) {
263265
return YoutubeParsingHelper.parseDurationString(duration);
264266
}
265267
throw new ParsingException("Could not get duration");
@@ -269,7 +271,7 @@ public long getDuration() throws ParsingException {
269271
public String getUploaderName() throws ParsingException {
270272
final String name = getTextFromObject(info.getArray("flexColumns").getObject(1)
271273
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
272-
if (!name.isEmpty()) {
274+
if (name != null && !name.isEmpty()) {
273275
return name;
274276
}
275277
throw new ParsingException("Could not get uploader name");
@@ -288,11 +290,13 @@ public String getUploaderUrl() throws ParsingException {
288290

289291
return null;
290292
} else {
291-
final JsonObject navigationEndpoint = info.getArray("flexColumns")
293+
final JsonObject navigationEndpointHolder = info.getArray("flexColumns")
292294
.getObject(1).getObject("musicResponsiveListItemFlexColumnRenderer")
293-
.getObject("text").getArray("runs").getObject(0).getObject("navigationEndpoint");
295+
.getObject("text").getArray("runs").getObject(0);
296+
297+
if (!navigationEndpointHolder.has("navigationEndpoint")) return null;
294298

295-
final String url = getUrlFromNavigationEndpoint(navigationEndpoint);
299+
final String url = getUrlFromNavigationEndpoint(navigationEndpointHolder.getObject("navigationEndpoint"));
296300

297301
if (url != null && !url.isEmpty()) {
298302
return url;
@@ -319,7 +323,7 @@ public long getViewCount() throws ParsingException {
319323
}
320324
final String viewCount = getTextFromObject(info.getArray("flexColumns").getObject(2)
321325
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
322-
if (!viewCount.isEmpty()) {
326+
if (viewCount != null && !viewCount.isEmpty()) {
323327
return Utils.mixedNumberWordToLong(viewCount);
324328
}
325329
throw new ParsingException("Could not get view count");
@@ -359,7 +363,7 @@ public String getThumbnailUrl() throws ParsingException {
359363
public String getName() throws ParsingException {
360364
final String name = getTextFromObject(info.getArray("flexColumns").getObject(0)
361365
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
362-
if (!name.isEmpty()) {
366+
if (name != null && !name.isEmpty()) {
363367
return name;
364368
}
365369
throw new ParsingException("Could not get name");
@@ -378,7 +382,7 @@ public String getUrl() throws ParsingException {
378382
public long getSubscriberCount() throws ParsingException {
379383
final String viewCount = getTextFromObject(info.getArray("flexColumns").getObject(2)
380384
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
381-
if (!viewCount.isEmpty()) {
385+
if (viewCount != null && !viewCount.isEmpty()) {
382386
return Utils.mixedNumberWordToLong(viewCount);
383387
}
384388
throw new ParsingException("Could not get subscriber count");
@@ -414,7 +418,7 @@ public String getThumbnailUrl() throws ParsingException {
414418
public String getName() throws ParsingException {
415419
final String name = getTextFromObject(info.getArray("flexColumns").getObject(0)
416420
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
417-
if (!name.isEmpty()) {
421+
if (name != null && !name.isEmpty()) {
418422
return name;
419423
}
420424
throw new ParsingException("Could not get name");
@@ -439,7 +443,7 @@ public String getUploaderName() throws ParsingException {
439443
name = getTextFromObject(info.getArray("flexColumns").getObject(1)
440444
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
441445
}
442-
if (!name.isEmpty()) {
446+
if (name != null && !name.isEmpty()) {
443447
return name;
444448
}
445449
throw new ParsingException("Could not get uploader name");
@@ -452,7 +456,7 @@ public long getStreamCount() throws ParsingException {
452456
}
453457
final String count = getTextFromObject(info.getArray("flexColumns").getObject(2)
454458
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
455-
if (!count.isEmpty()) {
459+
if (count != null && !count.isEmpty()) {
456460
if (count.contains("100+")) {
457461
return ITEM_COUNT_MORE_THAN_100;
458462
} else {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.grack.nanojson.JsonArray;
44
import com.grack.nanojson.JsonObject;
5+
56
import org.schabi.newpipe.extractor.StreamingService;
67
import org.schabi.newpipe.extractor.downloader.Downloader;
78
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
@@ -14,9 +15,10 @@
1415
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
1516
import org.schabi.newpipe.extractor.utils.Utils;
1617

17-
import javax.annotation.Nonnull;
1818
import java.io.IOException;
1919

20+
import javax.annotation.Nonnull;
21+
2022
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
2123
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getJsonResponse;
2224
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
@@ -81,7 +83,7 @@ public String getNextPageUrl() {
8183
@Override
8284
public String getName() throws ParsingException {
8385
String name = getTextFromObject(playlistInfo.getObject("title"));
84-
if (!name.isEmpty()) return name;
86+
if (name != null && !name.isEmpty()) return name;
8587

8688
return initialData.getObject("microformat").getObject("microformatDataRenderer").getString("title");
8789
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.grack.nanojson.JsonArray;
44
import com.grack.nanojson.JsonObject;
55
import com.grack.nanojson.JsonParser;
6+
67
import org.mozilla.javascript.Context;
78
import org.mozilla.javascript.Function;
89
import org.mozilla.javascript.ScriptableObject;
@@ -35,8 +36,6 @@
3536
import org.schabi.newpipe.extractor.utils.Parser;
3637
import org.schabi.newpipe.extractor.utils.Utils;
3738

38-
import javax.annotation.Nonnull;
39-
import javax.annotation.Nullable;
4039
import java.io.IOException;
4140
import java.io.UnsupportedEncodingException;
4241
import java.text.SimpleDateFormat;
@@ -50,6 +49,9 @@
5049
import java.util.Locale;
5150
import java.util.Map;
5251

52+
import javax.annotation.Nonnull;
53+
import javax.annotation.Nullable;
54+
5355
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
5456
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getJsonResponse;
5557
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
@@ -117,7 +119,7 @@ public String getName() throws ParsingException {
117119
assertPageFetched();
118120
String title = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("title"));
119121

120-
if (title.isEmpty()) {
122+
if (title == null || title.isEmpty()) {
121123
title = playerResponse.getObject("videoDetails").getString("title");
122124

123125
if (title == null || title.isEmpty()) throw new ParsingException("Could not get name");
@@ -197,7 +199,7 @@ public Description getDescription() throws ParsingException {
197199
assertPageFetched();
198200
// description with more info on links
199201
String description = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("description"), true);
200-
if (!description.isEmpty()) return new Description(description, Description.HTML);
202+
if (description != null && !description.isEmpty()) return new Description(description, Description.HTML);
201203

202204
// raw non-html description
203205
return new Description(playerResponse.getObject("videoDetails").getString("shortDescription"), Description.PLAIN_TEXT);
@@ -249,7 +251,7 @@ public long getViewCount() throws ParsingException {
249251
String views = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("viewCount")
250252
.getObject("videoViewCountRenderer").getObject("viewCount"));
251253

252-
if (views.isEmpty()) {
254+
if (views == null || views.isEmpty()) {
253255
views = playerResponse.getObject("videoDetails").getString("viewCount");
254256

255257
if (views == null || views.isEmpty()) throw new ParsingException("Could not get view count");
@@ -330,7 +332,7 @@ public String getUploaderName() throws ParsingException {
330332
String uploaderName = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("owner")
331333
.getObject("videoOwnerRenderer").getObject("title"));
332334

333-
if (uploaderName.isEmpty()) {
335+
if (uploaderName == null || uploaderName.isEmpty()) {
334336
uploaderName = playerResponse.getObject("videoDetails").getString("author");
335337

336338
if (uploaderName == null || uploaderName.isEmpty()) throw new ParsingException("Could not get uploader name");

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.grack.nanojson.JsonArray;
44
import com.grack.nanojson.JsonObject;
5+
56
import org.schabi.newpipe.extractor.exceptions.ParsingException;
67
import org.schabi.newpipe.extractor.localization.DateWrapper;
78
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
@@ -11,12 +12,15 @@
1112
import org.schabi.newpipe.extractor.stream.StreamType;
1213
import org.schabi.newpipe.extractor.utils.Utils;
1314

14-
import javax.annotation.Nullable;
1515
import java.text.SimpleDateFormat;
1616
import java.util.Calendar;
1717
import java.util.Date;
1818

19-
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.*;
19+
import javax.annotation.Nullable;
20+
21+
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
22+
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
23+
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
2024
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
2125

2226
/*
@@ -93,7 +97,7 @@ public String getUrl() throws ParsingException {
9397
@Override
9498
public String getName() throws ParsingException {
9599
String name = getTextFromObject(videoInfo.getObject("title"));
96-
if (!name.isEmpty()) return name;
100+
if (name != null && !name.isEmpty()) return name;
97101
throw new ParsingException("Could not get name");
98102
}
99103

@@ -105,15 +109,15 @@ public long getDuration() throws ParsingException {
105109

106110
String duration = getTextFromObject(videoInfo.getObject("lengthText"));
107111

108-
if (duration.isEmpty()) {
112+
if (duration == null || duration.isEmpty()) {
109113
for (Object thumbnailOverlay : videoInfo.getArray("thumbnailOverlays")) {
110114
if (((JsonObject) thumbnailOverlay).has("thumbnailOverlayTimeStatusRenderer")) {
111115
duration = getTextFromObject(((JsonObject) thumbnailOverlay)
112116
.getObject("thumbnailOverlayTimeStatusRenderer").getObject("text"));
113117
}
114118
}
115119

116-
if (duration.isEmpty()) throw new ParsingException("Could not get duration");
120+
if (duration == null || duration.isEmpty()) throw new ParsingException("Could not get duration");
117121
}
118122

119123
return YoutubeParsingHelper.parseDurationString(duration);
@@ -123,13 +127,13 @@ public long getDuration() throws ParsingException {
123127
public String getUploaderName() throws ParsingException {
124128
String name = getTextFromObject(videoInfo.getObject("longBylineText"));
125129

126-
if (name.isEmpty()) {
130+
if (name == null || name.isEmpty()) {
127131
name = getTextFromObject(videoInfo.getObject("ownerText"));
128132

129-
if (name.isEmpty()) {
133+
if (name == null || name.isEmpty()) {
130134
name = getTextFromObject(videoInfo.getObject("shortBylineText"));
131135

132-
if (name.isEmpty()) throw new ParsingException("Could not get uploader name");
136+
if (name == null || name.isEmpty()) throw new ParsingException("Could not get uploader name");
133137
}
134138
}
135139

@@ -169,7 +173,7 @@ public String getTextualUploadDate() throws ParsingException {
169173
}
170174

171175
final String publishedTimeText = getTextFromObject(videoInfo.getObject("publishedTimeText"));
172-
if (!publishedTimeText.isEmpty()) return publishedTimeText;
176+
if (publishedTimeText != null && !publishedTimeText.isEmpty()) return publishedTimeText;
173177

174178
return null;
175179
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.grack.nanojson.JsonArray;
2424
import com.grack.nanojson.JsonObject;
25+
2526
import org.schabi.newpipe.extractor.StreamingService;
2627
import org.schabi.newpipe.extractor.downloader.Downloader;
2728
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
@@ -32,9 +33,10 @@
3233
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
3334
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
3435

35-
import javax.annotation.Nonnull;
3636
import java.io.IOException;
3737

38+
import javax.annotation.Nonnull;
39+
3840
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getJsonResponse;
3941
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
4042

@@ -71,7 +73,7 @@ public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) {
7173
@Override
7274
public String getName() throws ParsingException {
7375
String name = getTextFromObject(initialData.getObject("header").getObject("feedTabbedHeaderRenderer").getObject("title"));
74-
if (!name.isEmpty()) {
76+
if (name != null && !name.isEmpty()) {
7577
return name;
7678
}
7779
throw new ParsingException("Could not get Trending name");

0 commit comments

Comments
 (0)