Merge pull request #315 from B0pol/isnullorempty

refactor: add Utils.isNullOrEmpty()
This commit is contained in:
B0pol 2020-05-11 11:04:56 +00:00 committed by GitHub
commit 3a858e6640
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 124 additions and 78 deletions

View File

@ -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<R extends InfoItem> extends Extractor {
public abstract InfoItemsPage<R> 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<R extends InfoItem> extends Extractor {
}
public boolean hasNextPage() {
return nextPageUrl != null && !nextPageUrl.isEmpty();
return !isNullOrEmpty(nextPageUrl);
}
public List<T> getItems() {

View File

@ -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<T extends InfoItem> extends Info {
private List<T> relatedItems;
private String nextPageUrl = null;
@ -37,7 +39,7 @@ public abstract class ListInfo<T extends InfoItem> extends Info {
}
public boolean hasNextPage() {
return nextPageUrl != null && !nextPageUrl.isEmpty();
return !isNullOrEmpty(nextPageUrl);
}
public String getNextPageUrl() {

View File

@ -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) {

View File

@ -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<StreamInfoItem> 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"));
}

View File

@ -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<StreamInfoItem> {
private StreamInfoItemsCollector collector = null;
@ -36,7 +37,7 @@ public class SoundcloudChartsExtractor extends KioskExtractor<StreamInfoItem> {
@Override
public InfoItemsPage<StreamInfoItem> 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"));
}

View File

@ -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) {
}
@ -160,7 +162,7 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
@Override
public InfoItemsPage<StreamInfoItem> 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"));
}

View File

@ -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?

View File

@ -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;
}
@ -392,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");
@ -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("<a href=\"").append(url).append("\">").append(text).append("</a>");
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);

View File

@ -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");
@ -244,7 +245,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
@Override
public InfoItemsPage<StreamInfoItem> 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"));
}
@ -265,7 +266,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
private String getNextPageUrlFrom(JsonArray continuations) {
if (continuations == null || continuations.isEmpty()) {
if (isNullOrEmpty(continuations)) {
return "";
}

View File

@ -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<CommentsInfoItem> 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);

View File

@ -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;

View File

@ -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;
@ -166,7 +167,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor {
@Override
public InfoItemsPage<InfoItem> 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"));
}
@ -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 {
@ -469,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 "";
}

View File

@ -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<StreamInfoItem> 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 "";
}

View File

@ -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<InfoItem> 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 "";
}

View File

@ -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;

View File

@ -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 <chris.schabesberger@mailbox.org>
@ -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");
}
@ -105,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)
@ -113,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);
@ -123,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");
}
}
@ -141,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");
}
}
@ -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) {

View File

@ -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<StreamInfoItem> {
private JsonObject initialData;
@ -73,7 +74,7 @@ public class YoutubeTrendingExtractor extends KioskExtractor<StreamInfoItem> {
@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");

View File

@ -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<? extends Stream> streamList) {
if (stream == null || streamList == null) return false;
if (isNullOrEmpty(streamList)) return false;
for (Stream cmpStream : streamList) {
if (stream.equalStats(cmpStream)) return true;
}

View File

@ -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.
*
@ -103,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.");
}
@ -159,7 +161,7 @@ public class StreamInfo extends Info {
streamInfo.setAudioStreams(new ArrayList<AudioStream>());
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());

View File

@ -6,7 +6,9 @@ import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class Utils {
@ -48,7 +50,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()) {
@ -70,7 +73,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");
}
@ -186,4 +189,18 @@ public class Utils {
}
return uri.getProtocol() + "://" + uri.getAuthority();
}
public static boolean isNullOrEmpty(final String str) {
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();
}
}

View File

@ -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<SearchExtractor>
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());

View File

@ -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<? extends InfoItem> itemsList, List<Throwable> 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()));
@ -83,7 +86,7 @@ public final class DefaultTests {
public static <T extends InfoItem> void assertNoMoreItems(ListExtractor<T> 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,

View File

@ -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));
}
}