Skip to content

Commit b51699a

Browse files
committed
Implement @TobiGr's suggestions
1 parent 979c5a7 commit b51699a

7 files changed

Lines changed: 44 additions & 45 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,9 @@ private JsonObject getVideoTab() throws ParsingException {
309309
if (getTextFromObject(videoTab.getObject("content").getObject("sectionListRenderer")
310310
.getArray("contents").getObject(0).getObject("itemSectionRenderer")
311311
.getArray("contents").getObject(0).getObject("messageRenderer")
312-
.getObject("text")).equals("This channel has no videos."))
312+
.getObject("text")).equals("This channel has no videos.")) {
313313
return null;
314+
}
314315

315316
this.videoTab = videoTab;
316317
return videoTab;

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.grack.nanojson.JsonParser;
66
import com.grack.nanojson.JsonParserException;
77
import com.grack.nanojson.JsonWriter;
8-
98
import org.schabi.newpipe.extractor.InfoItem;
109
import org.schabi.newpipe.extractor.StreamingService;
1110
import org.schabi.newpipe.extractor.downloader.Downloader;
@@ -20,18 +19,17 @@
2019
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
2120
import org.schabi.newpipe.extractor.utils.Utils;
2221

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-
3129
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
32-
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getValidJsonResponseBody;
3330
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
3431
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
32+
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getValidJsonResponseBody;
3533
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ALBUMS;
3634
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ARTISTS;
3735
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_PLAYLISTS;
@@ -251,7 +249,7 @@ public String getUrl() throws ParsingException {
251249
public String getName() throws ParsingException {
252250
final String name = getTextFromObject(info.getArray("flexColumns").getObject(0)
253251
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
254-
if (name != null && !name.isEmpty()) {
252+
if (!name.isEmpty()) {
255253
return name;
256254
}
257255
throw new ParsingException("Could not get name");
@@ -261,7 +259,7 @@ public String getName() throws ParsingException {
261259
public long getDuration() throws ParsingException {
262260
final String duration = getTextFromObject(info.getArray("flexColumns").getObject(3)
263261
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
264-
if (duration != null && !duration.isEmpty()) {
262+
if (!duration.isEmpty()) {
265263
return YoutubeParsingHelper.parseDurationString(duration);
266264
}
267265
throw new ParsingException("Could not get duration");
@@ -271,7 +269,7 @@ public long getDuration() throws ParsingException {
271269
public String getUploaderName() throws ParsingException {
272270
final String name = getTextFromObject(info.getArray("flexColumns").getObject(1)
273271
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
274-
if (name != null && !name.isEmpty()) {
272+
if (!name.isEmpty()) {
275273
return name;
276274
}
277275
throw new ParsingException("Could not get uploader name");
@@ -294,7 +292,13 @@ public String getUploaderUrl() throws ParsingException {
294292
.getObject(1).getObject("musicResponsiveListItemFlexColumnRenderer")
295293
.getObject("text").getArray("runs").getObject(0).getObject("navigationEndpoint");
296294

297-
return getUrlFromNavigationEndpoint(navigationEndpoint);
295+
final String url = getUrlFromNavigationEndpoint(navigationEndpoint);
296+
297+
if (url != null && !url.isEmpty()) {
298+
return url;
299+
}
300+
301+
throw new ParsingException("Could not get uploader URL");
298302
}
299303
}
300304

@@ -315,7 +319,7 @@ public long getViewCount() throws ParsingException {
315319
}
316320
final String viewCount = getTextFromObject(info.getArray("flexColumns").getObject(2)
317321
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
318-
if (viewCount != null && !viewCount.isEmpty()) {
322+
if (!viewCount.isEmpty()) {
319323
return Utils.mixedNumberWordToLong(viewCount);
320324
}
321325
throw new ParsingException("Could not get view count");
@@ -355,7 +359,7 @@ public String getThumbnailUrl() throws ParsingException {
355359
public String getName() throws ParsingException {
356360
final String name = getTextFromObject(info.getArray("flexColumns").getObject(0)
357361
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
358-
if (name != null && !name.isEmpty()) {
362+
if (!name.isEmpty()) {
359363
return name;
360364
}
361365
throw new ParsingException("Could not get name");
@@ -374,7 +378,7 @@ public String getUrl() throws ParsingException {
374378
public long getSubscriberCount() throws ParsingException {
375379
final String viewCount = getTextFromObject(info.getArray("flexColumns").getObject(2)
376380
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
377-
if (viewCount != null && !viewCount.isEmpty()) {
381+
if (!viewCount.isEmpty()) {
378382
return Utils.mixedNumberWordToLong(viewCount);
379383
}
380384
throw new ParsingException("Could not get subscriber count");
@@ -410,7 +414,7 @@ public String getThumbnailUrl() throws ParsingException {
410414
public String getName() throws ParsingException {
411415
final String name = getTextFromObject(info.getArray("flexColumns").getObject(0)
412416
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
413-
if (name != null && !name.isEmpty()) {
417+
if (!name.isEmpty()) {
414418
return name;
415419
}
416420
throw new ParsingException("Could not get name");
@@ -435,7 +439,7 @@ public String getUploaderName() throws ParsingException {
435439
name = getTextFromObject(info.getArray("flexColumns").getObject(1)
436440
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
437441
}
438-
if (name != null && !name.isEmpty()) {
442+
if (!name.isEmpty()) {
439443
return name;
440444
}
441445
throw new ParsingException("Could not get uploader name");
@@ -448,7 +452,7 @@ public long getStreamCount() throws ParsingException {
448452
}
449453
final String count = getTextFromObject(info.getArray("flexColumns").getObject(2)
450454
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
451-
if (count != null && !count.isEmpty()) {
455+
if (!count.isEmpty()) {
452456
if (count.contains("100+")) {
453457
return ITEM_COUNT_MORE_THAN_100;
454458
} else {

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

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

33
import com.grack.nanojson.JsonArray;
44
import com.grack.nanojson.JsonObject;
5-
65
import org.schabi.newpipe.extractor.StreamingService;
76
import org.schabi.newpipe.extractor.downloader.Downloader;
87
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
@@ -15,9 +14,8 @@
1514
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
1615
import org.schabi.newpipe.extractor.utils.Utils;
1716

18-
import java.io.IOException;
19-
2017
import javax.annotation.Nonnull;
18+
import java.io.IOException;
2119

2220
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
2321
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getJsonResponse;
@@ -83,7 +81,7 @@ public String getNextPageUrl() {
8381
@Override
8482
public String getName() throws ParsingException {
8583
String name = getTextFromObject(playlistInfo.getObject("title"));
86-
if (name == null || !name.isEmpty()) return name;
84+
if (!name.isEmpty()) return name;
8785

8886
return initialData.getObject("microformat").getObject("microformatDataRenderer").getString("title");
8987
}

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.grack.nanojson.JsonArray;
44
import com.grack.nanojson.JsonObject;
55
import com.grack.nanojson.JsonParser;
6-
76
import org.mozilla.javascript.Context;
87
import org.mozilla.javascript.Function;
98
import org.mozilla.javascript.ScriptableObject;
@@ -33,10 +32,11 @@
3332
import org.schabi.newpipe.extractor.stream.StreamType;
3433
import org.schabi.newpipe.extractor.stream.SubtitlesStream;
3534
import org.schabi.newpipe.extractor.stream.VideoStream;
36-
import org.schabi.newpipe.extractor.utils.JsonUtils;
3735
import org.schabi.newpipe.extractor.utils.Parser;
3836
import org.schabi.newpipe.extractor.utils.Utils;
3937

38+
import javax.annotation.Nonnull;
39+
import javax.annotation.Nullable;
4040
import java.io.IOException;
4141
import java.io.UnsupportedEncodingException;
4242
import java.text.SimpleDateFormat;
@@ -50,9 +50,6 @@
5050
import java.util.Locale;
5151
import java.util.Map;
5252

53-
import javax.annotation.Nonnull;
54-
import javax.annotation.Nullable;
55-
5653
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
5754
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getJsonResponse;
5855
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
@@ -120,7 +117,7 @@ public String getName() throws ParsingException {
120117
assertPageFetched();
121118
String title = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("title"));
122119

123-
if (title == null || title.isEmpty()) {
120+
if (title.isEmpty()) {
124121
title = playerResponse.getObject("videoDetails").getString("title");
125122

126123
if (title == null || title.isEmpty()) throw new ParsingException("Could not get name");
@@ -200,7 +197,7 @@ public Description getDescription() throws ParsingException {
200197
assertPageFetched();
201198
// description with more info on links
202199
String description = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("description"), true);
203-
if (description != null && !description.isEmpty()) return new Description(description, Description.HTML);
200+
if (!description.isEmpty()) return new Description(description, Description.HTML);
204201

205202
// raw non-html description
206203
return new Description(playerResponse.getObject("videoDetails").getString("shortDescription"), Description.PLAIN_TEXT);
@@ -252,7 +249,7 @@ public long getViewCount() throws ParsingException {
252249
String views = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("viewCount")
253250
.getObject("videoViewCountRenderer").getObject("viewCount"));
254251

255-
if (views == null || views.isEmpty()) {
252+
if (views.isEmpty()) {
256253
views = playerResponse.getObject("videoDetails").getString("viewCount");
257254

258255
if (views == null || views.isEmpty()) throw new ParsingException("Could not get view count");
@@ -333,7 +330,7 @@ public String getUploaderName() throws ParsingException {
333330
String uploaderName = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("owner")
334331
.getObject("videoOwnerRenderer").getObject("title"));
335332

336-
if (uploaderName == null || uploaderName.isEmpty()) {
333+
if (uploaderName.isEmpty()) {
337334
uploaderName = playerResponse.getObject("videoDetails").getString("author");
338335

339336
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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public String getUrl() throws ParsingException {
9393
@Override
9494
public String getName() throws ParsingException {
9595
String name = getTextFromObject(videoInfo.getObject("title"));
96-
if (name == null || !name.isEmpty()) return name;
96+
if (!name.isEmpty()) return name;
9797
throw new ParsingException("Could not get name");
9898
}
9999

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

106106
String duration = getTextFromObject(videoInfo.getObject("lengthText"));
107107

108-
if (duration == null || duration.isEmpty()) {
108+
if (duration.isEmpty()) {
109109
for (Object thumbnailOverlay : videoInfo.getArray("thumbnailOverlays")) {
110110
if (((JsonObject) thumbnailOverlay).has("thumbnailOverlayTimeStatusRenderer")) {
111111
duration = getTextFromObject(((JsonObject) thumbnailOverlay)
112112
.getObject("thumbnailOverlayTimeStatusRenderer").getObject("text"));
113113
}
114114
}
115115

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

119119
return YoutubeParsingHelper.parseDurationString(duration);
@@ -123,13 +123,13 @@ public long getDuration() throws ParsingException {
123123
public String getUploaderName() throws ParsingException {
124124
String name = getTextFromObject(videoInfo.getObject("longBylineText"));
125125

126-
if (name == null || name.isEmpty()) {
126+
if (name.isEmpty()) {
127127
name = getTextFromObject(videoInfo.getObject("ownerText"));
128128

129-
if (name == null || name.isEmpty()) {
129+
if (name.isEmpty()) {
130130
name = getTextFromObject(videoInfo.getObject("shortBylineText"));
131131

132-
if (name == null || name.isEmpty()) throw new ParsingException("Could not get uploader name");
132+
if (name.isEmpty()) throw new ParsingException("Could not get uploader name");
133133
}
134134
}
135135

@@ -169,7 +169,7 @@ public String getTextualUploadDate() throws ParsingException {
169169
}
170170

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

174174
return null;
175175
}

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

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

2323
import com.grack.nanojson.JsonArray;
2424
import com.grack.nanojson.JsonObject;
25-
2625
import org.schabi.newpipe.extractor.StreamingService;
2726
import org.schabi.newpipe.extractor.downloader.Downloader;
2827
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
@@ -33,9 +32,8 @@
3332
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
3433
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
3534

36-
import java.io.IOException;
37-
3835
import javax.annotation.Nonnull;
36+
import java.io.IOException;
3937

4038
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getJsonResponse;
4139
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
@@ -72,13 +70,8 @@ public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) {
7270
@Nonnull
7371
@Override
7472
public String getName() throws ParsingException {
75-
String name;
76-
try {
77-
name = getTextFromObject(initialData.getObject("header").getObject("feedTabbedHeaderRenderer").getObject("title"));
78-
} catch (Exception e) {
79-
throw new ParsingException("Could not get Trending name", e);
80-
}
81-
if (name != null && !name.isEmpty()) {
73+
String name = getTextFromObject(initialData.getObject("header").getObject("feedTabbedHeaderRenderer").getObject("title"));
74+
if (!name.isEmpty()) {
8275
return name;
8376
}
8477
throw new ParsingException("Could not get Trending name");

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeParsingHelper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,12 @@ public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint)
384384
return null;
385385
}
386386

387+
/**
388+
* Get the text from a JSON object that has either a simpleText or a runs array.
389+
* @param textObject JSON object to get the text from
390+
* @param html whether to return HTML, by parsing the navigationEndpoint
391+
* @return text in the JSON object or an empty string
392+
*/
387393
public static String getTextFromObject(JsonObject textObject, boolean html) throws ParsingException {
388394
if (textObject.has("simpleText")) return textObject.getString("simpleText");
389395

0 commit comments

Comments
 (0)