Skip to content

Commit

Permalink
Merge pull request #315 from B0pol/isnullorempty
Browse files Browse the repository at this point in the history
refactor: add Utils.isNullOrEmpty()
  • Loading branch information
B0pol authored May 11, 2020
2 parents 3cae32b + adaf196 commit 3a858e6
Show file tree
Hide file tree
Showing 23 changed files with 124 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

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;
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).
*/
Expand Down Expand Up @@ -63,8 +66,7 @@ public ListExtractor(StreamingService service, ListLinkHandler linkHandler) {
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
Expand Down Expand Up @@ -123,7 +125,7 @@ public InfoItemsPage(List<T> itemsList, String nextPageUrl, List<Throwable> erro
}

public boolean hasNextPage() {
return nextPageUrl != null && !nextPageUrl.isEmpty();
return !isNullOrEmpty(nextPageUrl);
}

public List<T> getItems() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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;
Expand Down Expand Up @@ -37,7 +39,7 @@ public void setRelatedItems(List<T> relatedItems) {
}

public boolean hasNextPage() {
return nextPageUrl != null && !nextPageUrl.isEmpty();
return !isNullOrEmpty(nextPageUrl);
}

public String getNextPageUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand All @@ -32,6 +33,7 @@
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 {
Expand All @@ -42,7 +44,7 @@ private 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;
Expand All @@ -64,7 +66,7 @@ public static String clientId() throws ExtractionException, IOException {

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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 {
Expand Down Expand Up @@ -132,7 +133,7 @@ private void computeNextPageAndGetStreams() throws ExtractionException {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand All @@ -36,7 +37,7 @@ public String getName() {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
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;
Expand Down Expand Up @@ -76,7 +78,7 @@ public String getThumbnailUrl() {

for (StreamInfoItem item : infoItems.getItems()) {
artworkUrl = item.getThumbnailUrl();
if (artworkUrl != null && !artworkUrl.isEmpty()) break;
if (!isNullOrEmpty(artworkUrl)) break;
}
} catch (Exception ignored) {
}
Expand Down Expand Up @@ -160,7 +162,7 @@ public String getNextPageUrl() throws IOException, ExtractionException {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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;
Expand Down Expand Up @@ -191,7 +192,7 @@ public List<AudioStream> getAudioStreams() throws IOException, ExtractionExcepti
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?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@

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.
Expand Down Expand Up @@ -202,7 +201,7 @@ public static boolean isHardcodedClientVersionValid() throws IOException, Extrac
* @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";
Expand Down Expand Up @@ -245,7 +244,7 @@ public static String getClientVersion() throws IOException, ExtractionException
for (String pattern : patterns) {
try {
contextClientVersion = Parser.matchGroup1(pattern, html);
if (contextClientVersion != null && !contextClientVersion.isEmpty()) {
if (!isNullOrEmpty(contextClientVersion)) {
return clientVersion = contextClientVersion;
}
} catch (Exception ignored) {
Expand Down Expand Up @@ -365,7 +364,7 @@ public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint)
return "https://www.youtube.com/channel/" + browseId;
}

if (canonicalBaseUrl != null && !canonicalBaseUrl.isEmpty()) {
if (!isNullOrEmpty(canonicalBaseUrl)) {
return "https://www.youtube.com" + canonicalBaseUrl;
}

Expand All @@ -392,7 +391,7 @@ public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint)
* @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");

Expand All @@ -403,7 +402,7 @@ public static String getTextFromObject(JsonObject textObject, boolean html) thro
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;
}
Expand Down Expand Up @@ -497,7 +496,7 @@ public static JsonArray getJsonResponse(final String url, final Localization loc
*/
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

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.
Expand Down Expand Up @@ -130,7 +131,7 @@ public String getId() throws ParsingException {

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");
Expand Down Expand Up @@ -244,7 +245,7 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException

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

Expand All @@ -265,7 +266,7 @@ public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException,


private String getNextPageUrlFrom(JsonArray continuations) {
if (continuations == null || continuations.isEmpty()) {
if (isNullOrEmpty(continuations)) {
return "";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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 {
Expand Down Expand Up @@ -91,7 +92,7 @@ private String getNextPageUrl(String continuation) throws ParsingException {

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import javax.annotation.Nullable;

import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor {

private final JsonObject json;
Expand Down
Loading

0 comments on commit 3a858e6

Please sign in to comment.