From 202a73516c55f76c6ef6967ddbfa32ee09e3a5e4 Mon Sep 17 00:00:00 2001 From: bopol Date: Wed, 15 Apr 2020 18:49:58 +0200 Subject: [PATCH 1/3] refactor: add Utils.isNullOrEmpty() --- .../newpipe/extractor/ListExtractor.java | 8 +++--- .../schabi/newpipe/extractor/ListInfo.java | 4 ++- .../soundcloud/SoundcloudParsingHelper.java | 6 +++-- .../SoundcloudPlaylistExtractor.java | 4 ++- .../extractors/SoundcloudStreamExtractor.java | 3 ++- .../youtube/YoutubeParsingHelper.java | 13 +++++---- .../extractors/YoutubeChannelExtractor.java | 3 ++- .../YoutubeCommentsInfoItemExtractor.java | 2 ++ .../YoutubeMusicSearchExtractor.java | 27 ++++++++++--------- .../YoutubeStreamInfoItemExtractor.java | 5 ++-- .../extractors/YoutubeTrendingExtractor.java | 3 ++- .../newpipe/extractor/stream/StreamInfo.java | 4 ++- .../schabi/newpipe/extractor/utils/Utils.java | 12 ++++++++- .../extractor/services/DefaultTests.java | 17 +++++++----- 14 files changed, 70 insertions(+), 41 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java index 0e21f04b8..aae591eb6 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java @@ -2,6 +2,7 @@ package org.schabi.newpipe.extractor; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; +import org.schabi.newpipe.extractor.utils.Utils; import java.io.IOException; import java.util.Collections; @@ -9,6 +10,8 @@ import java.util.List; import javax.annotation.Nonnull; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; + /** * Base class to extractors that have a list (e.g. playlists, users). */ @@ -63,8 +66,7 @@ public abstract class ListExtractor extends Extractor { public abstract InfoItemsPage getPage(final String pageUrl) throws IOException, ExtractionException; public boolean hasNextPage() throws IOException, ExtractionException { - final String nextPageUrl = getNextPageUrl(); - return nextPageUrl != null && !nextPageUrl.isEmpty(); + return !isNullOrEmpty(getNextPageUrl()); } @Override @@ -123,7 +125,7 @@ public abstract class ListExtractor extends Extractor { } public boolean hasNextPage() { - return nextPageUrl != null && !nextPageUrl.isEmpty(); + return !isNullOrEmpty(nextPageUrl); } public List getItems() { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java index 1cb42e5da..38177e1e4 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java @@ -4,6 +4,8 @@ import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; import java.util.List; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; + public abstract class ListInfo extends Info { private List relatedItems; private String nextPageUrl = null; @@ -37,7 +39,7 @@ public abstract class ListInfo extends Info { } public boolean hasNextPage() { - return nextPageUrl != null && !nextPageUrl.isEmpty(); + return !isNullOrEmpty(nextPageUrl); } public String getNextPageUrl() { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java index b24d4bf85..b8dafd834 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java @@ -21,6 +21,7 @@ import org.schabi.newpipe.extractor.services.soundcloud.extractors.SoundcloudStr import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; import org.schabi.newpipe.extractor.utils.Parser; import org.schabi.newpipe.extractor.utils.Parser.RegexException; +import org.schabi.newpipe.extractor.utils.Utils; import javax.annotation.Nonnull; import java.io.IOException; @@ -32,6 +33,7 @@ import java.util.*; import static java.util.Collections.singletonList; import static org.schabi.newpipe.extractor.ServiceList.SoundCloud; import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps; public class SoundcloudParsingHelper { @@ -42,7 +44,7 @@ public class SoundcloudParsingHelper { } public static String clientId() throws ExtractionException, IOException { - if (clientId != null && !clientId.isEmpty()) return clientId; + if (!isNullOrEmpty(clientId)) return clientId; Downloader dl = NewPipe.getDownloader(); clientId = HARDCODED_CLIENT_ID; @@ -64,7 +66,7 @@ public class SoundcloudParsingHelper { for (Element element : possibleScripts) { final String srcUrl = element.attr("src"); - if (srcUrl != null && !srcUrl.isEmpty()) { + if (!isNullOrEmpty(srcUrl)) { try { return clientId = Parser.matchGroup1(clientIdPattern, dl.get(srcUrl, headers).responseBody()); } catch (RegexException ignored) { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java index a529556ee..fc3910a83 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java @@ -21,6 +21,8 @@ import java.io.IOException; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; + @SuppressWarnings("WeakerAccess") public class SoundcloudPlaylistExtractor extends PlaylistExtractor { private static final int streamsPerRequestedPage = 15; @@ -76,7 +78,7 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor { for (StreamInfoItem item : infoItems.getItems()) { artworkUrl = item.getThumbnailUrl(); - if (artworkUrl != null && !artworkUrl.isEmpty()) break; + if (!isNullOrEmpty(artworkUrl)) break; } } catch (Exception ignored) { } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java index beee2c3ef..431baff94 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java @@ -36,6 +36,7 @@ import java.util.Locale; import javax.annotation.Nonnull; import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class SoundcloudStreamExtractor extends StreamExtractor { private JsonObject track; @@ -191,7 +192,7 @@ public class SoundcloudStreamExtractor extends StreamExtractor { JsonObject t = (JsonObject) transcoding; String url = t.getString("url"); - if (url != null && !url.isEmpty()) { + if (!isNullOrEmpty(url)) { // We can only play the mp3 format, but not handle m3u playlists / streams. // what about Opus? diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java index a630ae517..02a528fbe 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java @@ -29,8 +29,7 @@ import java.util.*; import static org.schabi.newpipe.extractor.NewPipe.getDownloader; import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING; -import static org.schabi.newpipe.extractor.utils.Utils.HTTP; -import static org.schabi.newpipe.extractor.utils.Utils.HTTPS; +import static org.schabi.newpipe.extractor.utils.Utils.*; /* * Created by Christian Schabesberger on 02.03.16. @@ -202,7 +201,7 @@ public class YoutubeParsingHelper { * @throws ParsingException */ public static String getClientVersion() throws IOException, ExtractionException { - if (clientVersion != null && !clientVersion.isEmpty()) return clientVersion; + if (!isNullOrEmpty(clientVersion)) return clientVersion; if (isHardcodedClientVersionValid()) return clientVersion = HARDCODED_CLIENT_VERSION; final String url = "https://www.youtube.com/results?search_query=test"; @@ -245,7 +244,7 @@ public class YoutubeParsingHelper { for (String pattern : patterns) { try { contextClientVersion = Parser.matchGroup1(pattern, html); - if (contextClientVersion != null && !contextClientVersion.isEmpty()) { + if (!isNullOrEmpty(contextClientVersion)) { return clientVersion = contextClientVersion; } } catch (Exception ignored) { @@ -365,7 +364,7 @@ public class YoutubeParsingHelper { return "https://www.youtube.com/channel/" + browseId; } - if (canonicalBaseUrl != null && !canonicalBaseUrl.isEmpty()) { + if (!isNullOrEmpty(canonicalBaseUrl)) { return "https://www.youtube.com" + canonicalBaseUrl; } @@ -403,7 +402,7 @@ public class YoutubeParsingHelper { String text = ((JsonObject) textPart).getString("text"); if (html && ((JsonObject) textPart).has("navigationEndpoint")) { String url = getUrlFromNavigationEndpoint(((JsonObject) textPart).getObject("navigationEndpoint")); - if (url != null && !url.isEmpty()) { + if (!isNullOrEmpty(url)) { textBuilder.append("").append(text).append(""); continue; } @@ -497,7 +496,7 @@ public class YoutubeParsingHelper { */ public static void defaultAlertsCheck(final JsonObject initialData) throws ParsingException { final JsonArray alerts = initialData.getArray("alerts"); - if (!alerts.isEmpty()) { + if (!isNullOrEmpty(alerts)) { final JsonObject alertRenderer = alerts.getObject(0).getObject("alertRenderer"); final String alertText = getTextFromObject(alertRenderer.getObject("text")); final String alertType = alertRenderer.getString("type", EMPTY_STRING); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java index 0e5329425..d9771240f 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java @@ -21,6 +21,7 @@ import java.io.IOException; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*; import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; /* * Created by Christian Schabesberger on 25.07.16. @@ -130,7 +131,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor { if (!channelId.isEmpty()) { return channelId; - } else if (redirectedChannelId != null && !redirectedChannelId.isEmpty()) { + } else if (!isNullOrEmpty(redirectedChannelId)) { return redirectedChannelId; } else { throw new ParsingException("Could not get channel id"); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java index b012be7cc..9d659f5d3 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java @@ -11,6 +11,8 @@ import org.schabi.newpipe.extractor.utils.Utils; import javax.annotation.Nullable; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; + public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor { private final JsonObject json; diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMusicSearchExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMusicSearchExtractor.java index 7e952c94b..6159ab43a 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMusicSearchExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMusicSearchExtractor.java @@ -35,6 +35,7 @@ import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeS import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_SONGS; import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_VIDEOS; import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class YoutubeMusicSearchExtractor extends SearchExtractor { private JsonObject initialData; @@ -238,7 +239,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { @Override public String getUrl() throws ParsingException { final String url = getUrlFromNavigationEndpoint(info.getObject("doubleTapCommand")); - if (url != null && !url.isEmpty()) { + if (!isNullOrEmpty(url)) { return url; } throw new ParsingException("Could not get url"); @@ -248,7 +249,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { public String getName() throws ParsingException { final String name = getTextFromObject(info.getArray("flexColumns").getObject(0) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (name != null && !name.isEmpty()) { + if (!isNullOrEmpty(name)) { return name; } throw new ParsingException("Could not get name"); @@ -258,7 +259,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { public long getDuration() throws ParsingException { final String duration = getTextFromObject(info.getArray("flexColumns").getObject(3) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (duration != null && !duration.isEmpty()) { + if (!isNullOrEmpty(duration)) { return YoutubeParsingHelper.parseDurationString(duration); } throw new ParsingException("Could not get duration"); @@ -268,7 +269,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { public String getUploaderName() throws ParsingException { final String name = getTextFromObject(info.getArray("flexColumns").getObject(1) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (name != null && !name.isEmpty()) { + if (!isNullOrEmpty(name)) { return name; } throw new ParsingException("Could not get uploader name"); @@ -295,7 +296,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { final String url = getUrlFromNavigationEndpoint(navigationEndpointHolder.getObject("navigationEndpoint")); - if (url != null && !url.isEmpty()) { + if (!isNullOrEmpty(url)) { return url; } @@ -320,7 +321,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { } final String viewCount = getTextFromObject(info.getArray("flexColumns").getObject(2) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (viewCount != null && !viewCount.isEmpty()) { + if (!isNullOrEmpty(viewCount)) { return Utils.mixedNumberWordToLong(viewCount); } throw new ParsingException("Could not get view count"); @@ -360,7 +361,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { public String getName() throws ParsingException { final String name = getTextFromObject(info.getArray("flexColumns").getObject(0) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (name != null && !name.isEmpty()) { + if (!isNullOrEmpty(name)) { return name; } throw new ParsingException("Could not get name"); @@ -369,7 +370,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { @Override public String getUrl() throws ParsingException { final String url = getUrlFromNavigationEndpoint(info.getObject("navigationEndpoint")); - if (url != null && !url.isEmpty()) { + if (!isNullOrEmpty(url)) { return url; } throw new ParsingException("Could not get url"); @@ -379,7 +380,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { public long getSubscriberCount() throws ParsingException { final String viewCount = getTextFromObject(info.getArray("flexColumns").getObject(2) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (viewCount != null && !viewCount.isEmpty()) { + if (!isNullOrEmpty(viewCount)) { return Utils.mixedNumberWordToLong(viewCount); } throw new ParsingException("Could not get subscriber count"); @@ -415,7 +416,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { public String getName() throws ParsingException { final String name = getTextFromObject(info.getArray("flexColumns").getObject(0) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (name != null && !name.isEmpty()) { + if (!isNullOrEmpty(name)) { return name; } throw new ParsingException("Could not get name"); @@ -424,7 +425,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { @Override public String getUrl() throws ParsingException { final String url = getUrlFromNavigationEndpoint(info.getObject("doubleTapCommand")); - if (url != null && !url.isEmpty()) { + if (!isNullOrEmpty(url)) { return url; } throw new ParsingException("Could not get url"); @@ -440,7 +441,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { name = getTextFromObject(info.getArray("flexColumns").getObject(1) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); } - if (name != null && !name.isEmpty()) { + if (!isNullOrEmpty(name)) { return name; } throw new ParsingException("Could not get uploader name"); @@ -453,7 +454,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { } final String count = getTextFromObject(info.getArray("flexColumns").getObject(2) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (count != null && !count.isEmpty()) { + if (!isNullOrEmpty(count)) { if (count.contains("100+")) { return ITEM_COUNT_MORE_THAN_100; } else { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java index a2d294bb7..d115fdeed 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java @@ -18,6 +18,7 @@ import java.util.Date; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*; import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; /* * Copyright (C) Christian Schabesberger 2016 @@ -93,7 +94,7 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor { @Override public String getName() throws ParsingException { String name = getTextFromObject(videoInfo.getObject("title")); - if (name != null && !name.isEmpty()) return name; + if (!isNullOrEmpty(name)) return name; throw new ParsingException("Could not get name"); } @@ -186,7 +187,7 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor { } final String textualUploadDate = getTextualUploadDate(); - if (timeAgoParser != null && textualUploadDate != null && !textualUploadDate.isEmpty()) { + if (timeAgoParser != null && !isNullOrEmpty(textualUploadDate)) { try { return timeAgoParser.parse(textualUploadDate); } catch (ParsingException e) { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeTrendingExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeTrendingExtractor.java index 87b5561c3..ddd108e87 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeTrendingExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeTrendingExtractor.java @@ -39,6 +39,7 @@ import javax.annotation.Nonnull; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonResponse; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class YoutubeTrendingExtractor extends KioskExtractor { private JsonObject initialData; @@ -73,7 +74,7 @@ public class YoutubeTrendingExtractor extends KioskExtractor { @Override public String getName() throws ParsingException { String name = getTextFromObject(initialData.getObject("header").getObject("feedTabbedHeaderRenderer").getObject("title")); - if (name != null && !name.isEmpty()) { + if (!isNullOrEmpty(name)) { return name; } throw new ParsingException("Could not get Trending name"); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java index aa1001b44..6ee60ce8e 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java @@ -16,6 +16,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; + /* * Created by Christian Schabesberger on 26.08.15. * @@ -159,7 +161,7 @@ public class StreamInfo extends Info { streamInfo.setAudioStreams(new ArrayList()); Exception dashMpdError = null; - if (streamInfo.getDashMpdUrl() != null && !streamInfo.getDashMpdUrl().isEmpty()) { + if (!isNullOrEmpty(streamInfo.getDashMpdUrl())) { try { DashMpdParser.ParserResult result = DashMpdParser.getStreams(streamInfo); streamInfo.getVideoOnlyStreams().addAll(result.getVideoOnlyStreams()); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java index 76aa2944f..3029fdd25 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java @@ -6,6 +6,7 @@ import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; +import java.util.Collection; import java.util.List; public class Utils { @@ -48,7 +49,8 @@ public class Utils { String multiplier = ""; try { multiplier = Parser.matchGroup("[\\d]+([\\.,][\\d]+)?([KMBkmb])+", numberWord, 2); - } catch(ParsingException ignored) {} + } catch (ParsingException ignored) { + } double count = Double.parseDouble(Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord) .replace(",", ".")); switch (multiplier.toUpperCase()) { @@ -186,4 +188,12 @@ public class Utils { } return uri.getProtocol() + "://" + uri.getAuthority(); } + + public static boolean isNullOrEmpty(final String str) { + return str == null || str.isEmpty(); + } + + public static boolean isNullOrEmpty(final Collection collection) { + return collection == null || collection.isEmpty(); + } } \ No newline at end of file diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java index a52698415..eaff6acba 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java @@ -18,6 +18,7 @@ import static junit.framework.TestCase.assertFalse; import static org.junit.Assert.*; import static org.schabi.newpipe.extractor.ExtractorAsserts.*; import static org.schabi.newpipe.extractor.StreamingService.LinkType; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public final class DefaultTests { public static void defaultTestListOfItems(StreamingService expectedService, List itemsList, List errors) throws ParsingException { @@ -27,8 +28,10 @@ public final class DefaultTests { for (InfoItem item : itemsList) { assertIsSecureUrl(item.getUrl()); - if (item.getThumbnailUrl() != null && !item.getThumbnailUrl().isEmpty()) { - assertIsSecureUrl(item.getThumbnailUrl()); + + final String thumbnailUrl = item.getThumbnailUrl(); + if (!isNullOrEmpty(thumbnailUrl)) { + assertIsSecureUrl(thumbnailUrl); } assertNotNull("InfoItem type not set: " + item, item.getInfoType()); assertEquals("Unexpected item service id", expectedService.getServiceId(), item.getServiceId()); @@ -39,15 +42,15 @@ public final class DefaultTests { assertNotEmpty("Uploader name not set: " + item, streamInfoItem.getUploaderName()); // assertNotEmpty("Uploader url not set: " + item, streamInfoItem.getUploaderUrl()); - if (streamInfoItem.getUploaderUrl() != null && !streamInfoItem.getUploaderUrl().isEmpty()) { - assertIsSecureUrl(streamInfoItem.getUploaderUrl()); - assertExpectedLinkType(expectedService, streamInfoItem.getUploaderUrl(), LinkType.CHANNEL); + final String uploaderUrl = streamInfoItem.getUploaderUrl(); + if (!isNullOrEmpty(uploaderUrl)) { + assertIsSecureUrl(uploaderUrl); + assertExpectedLinkType(expectedService, uploaderUrl, LinkType.CHANNEL); } assertExpectedLinkType(expectedService, streamInfoItem.getUrl(), LinkType.STREAM); - final String textualUploadDate = streamInfoItem.getTextualUploadDate(); - if (textualUploadDate != null && !textualUploadDate.isEmpty()) { + if (!isNullOrEmpty(streamInfoItem.getTextualUploadDate())) { final DateWrapper uploadDate = streamInfoItem.getUploadDate(); assertNotNull("No parsed upload date", uploadDate); assertTrue("Upload date not in the past", uploadDate.date().before(Calendar.getInstance())); From de26e00079adc7fcec49af6e4fc7656e1e01b91d Mon Sep 17 00:00:00 2001 From: bopol Date: Mon, 11 May 2020 11:40:24 +0200 Subject: [PATCH 2/3] changed all == null || isEmpty() to isNullOrEmpty() --- .../extractors/SoundcloudChannelExtractor.java | 3 ++- .../extractors/SoundcloudChartsExtractor.java | 3 ++- .../extractors/SoundcloudPlaylistExtractor.java | 2 +- .../services/youtube/YoutubeParsingHelper.java | 2 +- .../extractors/YoutubeChannelExtractor.java | 4 ++-- .../extractors/YoutubeCommentsExtractor.java | 3 ++- .../extractors/YoutubeMusicSearchExtractor.java | 4 ++-- .../extractors/YoutubePlaylistExtractor.java | 9 +++++---- .../extractors/YoutubeSearchExtractor.java | 5 +++-- .../extractors/YoutubeStreamExtractor.java | 17 +++++++++-------- .../YoutubeStreamInfoItemExtractor.java | 16 ++++++++-------- .../schabi/newpipe/extractor/stream/Stream.java | 4 +++- .../newpipe/extractor/stream/StreamInfo.java | 2 +- .../schabi/newpipe/extractor/utils/Utils.java | 10 +++++++++- .../services/DefaultSearchExtractorTest.java | 3 ++- .../extractor/services/DefaultTests.java | 2 +- .../search/YoutubeSearchExtractorTest.java | 3 ++- 17 files changed, 55 insertions(+), 37 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChannelExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChannelExtractor.java index 1068b36f1..295824259 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChannelExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChannelExtractor.java @@ -18,6 +18,7 @@ import javax.annotation.Nonnull; import java.io.IOException; import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; @SuppressWarnings("WeakerAccess") public class SoundcloudChannelExtractor extends ChannelExtractor { @@ -132,7 +133,7 @@ public class SoundcloudChannelExtractor extends ChannelExtractor { @Override public InfoItemsPage getPage(final String pageUrl) throws IOException, ExtractionException { - if (pageUrl == null || pageUrl.isEmpty()) { + if (isNullOrEmpty(pageUrl)) { throw new ExtractionException(new IllegalArgumentException("Page url is empty or null")); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChartsExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChartsExtractor.java index 042af5130..0c15d9c6b 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChartsExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChartsExtractor.java @@ -13,6 +13,7 @@ import javax.annotation.Nonnull; import java.io.IOException; import static org.schabi.newpipe.extractor.ServiceList.SoundCloud; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class SoundcloudChartsExtractor extends KioskExtractor { private StreamInfoItemsCollector collector = null; @@ -36,7 +37,7 @@ public class SoundcloudChartsExtractor extends KioskExtractor { @Override public InfoItemsPage getPage(String pageUrl) throws IOException, ExtractionException { - if (pageUrl == null || pageUrl.isEmpty()) { + if (isNullOrEmpty(pageUrl)) { throw new ExtractionException(new IllegalArgumentException("Page url is empty or null")); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java index fc3910a83..4cff6bb67 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java @@ -162,7 +162,7 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor { @Override public InfoItemsPage getPage(String pageUrl) throws IOException, ExtractionException { - if (pageUrl == null || pageUrl.isEmpty()) { + if (isNullOrEmpty(pageUrl)) { throw new ExtractionException(new IllegalArgumentException("Page url is empty or null")); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java index 02a528fbe..e78168abb 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java @@ -391,7 +391,7 @@ public class YoutubeParsingHelper { * @return text in the JSON object or {@code null} */ public static String getTextFromObject(JsonObject textObject, boolean html) throws ParsingException { - if (textObject == null || textObject.isEmpty()) return null; + if (isNullOrEmpty(textObject)) return null; if (textObject.has("simpleText")) return textObject.getString("simpleText"); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java index d9771240f..f5cfbc243 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java @@ -245,7 +245,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor { @Override public InfoItemsPage getPage(String pageUrl) throws IOException, ExtractionException { - if (pageUrl == null || pageUrl.isEmpty()) { + if (isNullOrEmpty(pageUrl)) { throw new ExtractionException(new IllegalArgumentException("Page url is empty or null")); } @@ -266,7 +266,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor { private String getNextPageUrlFrom(JsonArray continuations) { - if (continuations == null || continuations.isEmpty()) { + if (isNullOrEmpty(continuations)) { return ""; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java index 5a68501ed..1dea2952b 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java @@ -27,6 +27,7 @@ import java.util.Map; import java.util.regex.Pattern; import static java.util.Collections.singletonList; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class YoutubeCommentsExtractor extends CommentsExtractor { @@ -91,7 +92,7 @@ public class YoutubeCommentsExtractor extends CommentsExtractor { @Override public InfoItemsPage getPage(String pageUrl) throws IOException, ExtractionException { - if (pageUrl == null || pageUrl.isEmpty()) { + if (isNullOrEmpty(pageUrl)) { throw new ExtractionException(new IllegalArgumentException("Page url is empty or null")); } String ajaxResponse = makeAjaxRequest(pageUrl); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMusicSearchExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMusicSearchExtractor.java index 6159ab43a..20fe8a087 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMusicSearchExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMusicSearchExtractor.java @@ -167,7 +167,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { @Override public InfoItemsPage getPage(final String pageUrl) throws IOException, ExtractionException { - if (pageUrl == null || pageUrl.isEmpty()) { + if (isNullOrEmpty(pageUrl)) { throw new ExtractionException(new IllegalArgumentException("Page url is empty or null")); } @@ -470,7 +470,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { } private String getNextPageUrlFrom(final JsonArray continuations) throws ParsingException, IOException, ReCaptchaException { - if (continuations == null || continuations.isEmpty()) { + if (isNullOrEmpty(continuations)) { return ""; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistExtractor.java index de2a0e714..b7c6d6c22 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistExtractor.java @@ -23,6 +23,7 @@ import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonResponse; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; @SuppressWarnings("WeakerAccess") public class YoutubePlaylistExtractor extends PlaylistExtractor { @@ -93,11 +94,11 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor { String url = playlistInfo.getObject("thumbnailRenderer").getObject("playlistVideoThumbnailRenderer") .getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url"); - if (url == null || url.isEmpty()) { + if (isNullOrEmpty(url)) { url = initialData.getObject("microformat").getObject("microformatDataRenderer").getObject("thumbnail") .getArray("thumbnails").getObject(0).getString("url"); - if (url == null || url.isEmpty()) throw new ParsingException("Could not get playlist thumbnail"); + if (isNullOrEmpty(url)) throw new ParsingException("Could not get playlist thumbnail"); } return fixThumbnailUrl(url); @@ -166,7 +167,7 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor { @Override public InfoItemsPage getPage(final String pageUrl) throws IOException, ExtractionException { - if (pageUrl == null || pageUrl.isEmpty()) { + if (isNullOrEmpty(pageUrl)) { throw new ExtractionException(new IllegalArgumentException("Page url is empty or null")); } @@ -182,7 +183,7 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor { } private String getNextPageUrlFrom(JsonArray continuations) { - if (continuations == null || continuations.isEmpty()) { + if (isNullOrEmpty(continuations)) { return ""; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java index 732ef09ad..1ef19b820 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java @@ -19,6 +19,7 @@ import javax.annotation.Nonnull; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonResponse; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; /* * Created by Christian Schabesberger on 22.07.2018 @@ -99,7 +100,7 @@ public class YoutubeSearchExtractor extends SearchExtractor { @Override public InfoItemsPage getPage(final String pageUrl) throws IOException, ExtractionException { - if (pageUrl == null || pageUrl.isEmpty()) { + if (isNullOrEmpty(pageUrl)) { throw new ExtractionException(new IllegalArgumentException("Page url is empty or null")); } @@ -133,7 +134,7 @@ public class YoutubeSearchExtractor extends SearchExtractor { } private String getNextPageUrlFrom(final JsonArray continuations) throws ParsingException { - if (continuations == null || continuations.isEmpty()) { + if (isNullOrEmpty(continuations)) { return ""; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java index b5cdd21db..b543c14f3 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java @@ -54,6 +54,7 @@ import javax.annotation.Nullable; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*; import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; /* * Created by Christian Schabesberger on 06.08.15. @@ -116,10 +117,10 @@ public class YoutubeStreamExtractor extends StreamExtractor { assertPageFetched(); String title = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("title")); - if (title == null || title.isEmpty()) { + if (isNullOrEmpty(title)) { title = playerResponse.getObject("videoDetails").getString("title"); - if (title == null || title.isEmpty()) throw new ParsingException("Could not get name"); + if (isNullOrEmpty(title)) throw new ParsingException("Could not get name"); } return title; @@ -167,7 +168,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { public DateWrapper getUploadDate() throws ParsingException { final String textualUploadDate = getTextualUploadDate(); - if (textualUploadDate == null || textualUploadDate.isEmpty()) { + if (isNullOrEmpty(textualUploadDate)) { return null; } @@ -204,7 +205,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { @Override public int getAgeLimit() { - if (initialData == null || initialData.isEmpty()) throw new IllegalStateException("initialData is not parsed yet"); + if (isNullOrEmpty(initialData)) throw new IllegalStateException("initialData is not parsed yet"); return ageLimit; } @@ -248,10 +249,10 @@ public class YoutubeStreamExtractor extends StreamExtractor { String views = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("viewCount") .getObject("videoViewCountRenderer").getObject("viewCount")); - if (views == null || views.isEmpty()) { + if (isNullOrEmpty(views)) { views = playerResponse.getObject("videoDetails").getString("viewCount"); - if (views == null || views.isEmpty()) throw new ParsingException("Could not get view count"); + if (isNullOrEmpty(views)) throw new ParsingException("Could not get view count"); } if (views.toLowerCase().contains("no views")) return 0; @@ -329,10 +330,10 @@ public class YoutubeStreamExtractor extends StreamExtractor { String uploaderName = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("owner") .getObject("videoOwnerRenderer").getObject("title")); - if (uploaderName == null || uploaderName.isEmpty()) { + if (isNullOrEmpty(uploaderName)) { uploaderName = playerResponse.getObject("videoDetails").getString("author"); - if (uploaderName == null || uploaderName.isEmpty()) throw new ParsingException("Could not get uploader name"); + if (isNullOrEmpty(uploaderName)) throw new ParsingException("Could not get uploader name"); } return uploaderName; diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java index d115fdeed..0757208ee 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java @@ -106,7 +106,7 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor { String duration = getTextFromObject(videoInfo.getObject("lengthText")); - if (duration == null || duration.isEmpty()) { + if (isNullOrEmpty(duration)) { for (Object thumbnailOverlay : videoInfo.getArray("thumbnailOverlays")) { if (((JsonObject) thumbnailOverlay).has("thumbnailOverlayTimeStatusRenderer")) { duration = getTextFromObject(((JsonObject) thumbnailOverlay) @@ -114,7 +114,7 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor { } } - if (duration == null || duration.isEmpty()) throw new ParsingException("Could not get duration"); + if (isNullOrEmpty(duration)) throw new ParsingException("Could not get duration"); } return YoutubeParsingHelper.parseDurationString(duration); @@ -124,13 +124,13 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor { public String getUploaderName() throws ParsingException { String name = getTextFromObject(videoInfo.getObject("longBylineText")); - if (name == null || name.isEmpty()) { + if (isNullOrEmpty(name)) { name = getTextFromObject(videoInfo.getObject("ownerText")); - if (name == null || name.isEmpty()) { + if (isNullOrEmpty(name)) { name = getTextFromObject(videoInfo.getObject("shortBylineText")); - if (name == null || name.isEmpty()) throw new ParsingException("Could not get uploader name"); + if (isNullOrEmpty(name)) throw new ParsingException("Could not get uploader name"); } } @@ -142,15 +142,15 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor { String url = getUrlFromNavigationEndpoint(videoInfo.getObject("longBylineText") .getArray("runs").getObject(0).getObject("navigationEndpoint")); - if (url == null || url.isEmpty()) { + if (isNullOrEmpty(url)) { url = getUrlFromNavigationEndpoint(videoInfo.getObject("ownerText") .getArray("runs").getObject(0).getObject("navigationEndpoint")); - if (url == null || url.isEmpty()) { + if (isNullOrEmpty(url)) { url = getUrlFromNavigationEndpoint(videoInfo.getObject("shortBylineText") .getArray("runs").getObject(0).getObject("navigationEndpoint")); - if (url == null || url.isEmpty()) throw new ParsingException("Could not get uploader url"); + if (isNullOrEmpty(url)) throw new ParsingException("Could not get uploader url"); } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Stream.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Stream.java index b48eeab29..7e9908167 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Stream.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Stream.java @@ -5,6 +5,8 @@ import org.schabi.newpipe.extractor.MediaFormat; import java.io.Serializable; import java.util.List; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; + /** * Creates a stream object from url, format and optional torrent url */ @@ -61,7 +63,7 @@ public abstract class Stream implements Serializable { * Check if the list already contains one stream with equals stats */ public static boolean containSimilarStream(Stream stream, List streamList) { - if (stream == null || streamList == null) return false; + if (isNullOrEmpty(streamList)) return false; for (Stream cmpStream : streamList) { if (stream.equalStats(cmpStream)) return true; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java index 6ee60ce8e..3878e593a 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java @@ -105,7 +105,7 @@ public class StreamInfo extends Info { String name = extractor.getName(); int ageLimit = extractor.getAgeLimit(); - if ((streamType == StreamType.NONE) || (url == null || url.isEmpty()) || (id == null || id.isEmpty()) + if ((streamType == StreamType.NONE) || isNullOrEmpty(url) || (isNullOrEmpty(id)) || (name == null /* streamInfo.title can be empty of course */) || (ageLimit == -1)) { throw new ExtractionException("Some important stream information was not given."); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java index 3029fdd25..b1a9ab343 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java @@ -1,5 +1,6 @@ package org.schabi.newpipe.extractor.utils; +import com.grack.nanojson.JsonObject; import org.schabi.newpipe.extractor.exceptions.ParsingException; import java.io.UnsupportedEncodingException; @@ -8,6 +9,7 @@ import java.net.URL; import java.net.URLDecoder; import java.util.Collection; import java.util.List; +import java.util.Map; public class Utils { @@ -72,7 +74,7 @@ public class Utils { * @param url the url to be tested */ public static void checkUrl(String pattern, String url) throws ParsingException { - if (url == null || url.isEmpty()) { + if (isNullOrEmpty(url)) { throw new IllegalArgumentException("Url can't be null or empty"); } @@ -193,7 +195,13 @@ public class Utils { return str == null || str.isEmpty(); } + // can be used for JsonArrays public static boolean isNullOrEmpty(final Collection collection) { return collection == null || collection.isEmpty(); } + + // can be used for JsonObjects + public static boolean isNullOrEmpty(final Map map) { + return map == null || map.isEmpty(); + } } \ No newline at end of file diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultSearchExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultSearchExtractorTest.java index 93c4eac70..250df78f8 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultSearchExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultSearchExtractorTest.java @@ -8,6 +8,7 @@ import javax.annotation.Nullable; import static org.junit.Assert.assertEquals; import static org.schabi.newpipe.extractor.ExtractorAsserts.assertEmpty; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public abstract class DefaultSearchExtractorTest extends DefaultListExtractorTest implements BaseSearchExtractorTest { @@ -25,7 +26,7 @@ public abstract class DefaultSearchExtractorTest extends DefaultListExtractorTes @Override public void testSearchSuggestion() throws Exception { final String expectedSearchSuggestion = expectedSearchSuggestion(); - if (expectedSearchSuggestion == null || expectedSearchSuggestion.isEmpty()) { + if (isNullOrEmpty(expectedSearchSuggestion)) { assertEmpty("Suggestion was expected to be empty", extractor().getSearchSuggestion()); } else { assertEquals(expectedSearchSuggestion, extractor().getSearchSuggestion()); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java index eaff6acba..82f75298a 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java @@ -86,7 +86,7 @@ public final class DefaultTests { public static void assertNoMoreItems(ListExtractor extractor) throws Exception { assertFalse("More items available when it shouldn't", extractor.hasNextPage()); final String nextPageUrl = extractor.getNextPageUrl(); - assertTrue("Next page is not empty or null", nextPageUrl == null || nextPageUrl.isEmpty()); + assertTrue("Next page is not empty or null", isNullOrEmpty(nextPageUrl)); } public static void assertNoDuplicatedItems(StreamingService expectedService, diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorTest.java index d5f2f1af2..29fab054d 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorTest.java @@ -20,6 +20,7 @@ import static org.schabi.newpipe.extractor.ExtractorAsserts.assertEmptyErrors; import static org.schabi.newpipe.extractor.ServiceList.YouTube; import static org.schabi.newpipe.extractor.services.DefaultTests.assertNoDuplicatedItems; import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.*; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class YoutubeSearchExtractorTest { public static class All extends DefaultSearchExtractorTest { @@ -170,7 +171,7 @@ public class YoutubeSearchExtractorTest { assertFalse("More items available when it shouldn't", nextEmptyPage.hasNextPage()); final String nextPageUrl = nextEmptyPage.getNextPageUrl(); - assertTrue("Next page is not empty or null", nextPageUrl == null || nextPageUrl.isEmpty()); + assertTrue("Next page is not empty or null", isNullOrEmpty(nextPageUrl)); } } From adaf196c99548200beefe8ca77dda538bd8f721c Mon Sep 17 00:00:00 2001 From: bopol Date: Mon, 11 May 2020 12:54:33 +0200 Subject: [PATCH 3/3] remove unused import --- .../src/main/java/org/schabi/newpipe/extractor/utils/Utils.java | 1 - 1 file changed, 1 deletion(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java index b1a9ab343..fcb0d5cb4 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java @@ -1,6 +1,5 @@ package org.schabi.newpipe.extractor.utils; -import com.grack.nanojson.JsonObject; import org.schabi.newpipe.extractor.exceptions.ParsingException; import java.io.UnsupportedEncodingException;