Skip to content

Commit

Permalink
refactor: add Utils.isNullOrEmpty()
Browse files Browse the repository at this point in the history
  • Loading branch information
B0pol committed Apr 15, 2020
1 parent 1b52230 commit aa2eada
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 44 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 @@ -18,6 +18,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 @@ -28,6 +29,7 @@

import static java.util.Collections.singletonList;
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps;

public class SoundcloudParsingHelper {
Expand All @@ -38,7 +40,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 @@ -60,7 +62,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 @@ -20,6 +20,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 @@ -75,7 +77,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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import javax.annotation.Nonnull;

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

public class SoundcloudStreamExtractor extends StreamExtractor {
private JsonObject track;

Expand Down Expand Up @@ -170,7 +172,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 @@ -21,6 +21,7 @@

import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.*;
import static org.schabi.newpipe.extractor.utils.JsonUtils.*;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

/*
* Created by Christian Schabesberger on 25.07.16.
Expand Down Expand Up @@ -138,7 +139,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
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 Expand Up @@ -60,7 +62,7 @@ public String getTextualPublishedTime() throws ParsingException {
@Override
public DateWrapper getPublishedTime() throws ParsingException {
String textualPublishedTime = getTextualPublishedTime();
if (timeAgoParser != null && textualPublishedTime != null && !textualPublishedTime.isEmpty()) {
if (timeAgoParser != null && !isNullOrEmpty(textualPublishedTime)) {
return timeAgoParser.parse(textualPublishedTime);
} else {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_PLAYLISTS;
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.Utils.isNullOrEmpty;

public class YoutubeMusicSearchExtractor extends SearchExtractor {
private JsonObject initialData;
Expand Down Expand Up @@ -248,7 +249,7 @@ private void collectMusicStreamsFrom(final InfoItemsSearchCollector collector, f
@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");
Expand All @@ -258,7 +259,7 @@ public String getUrl() throws ParsingException {
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");
Expand All @@ -268,7 +269,7 @@ public String getName() throws ParsingException {
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");
Expand All @@ -278,7 +279,7 @@ public long getDuration() throws ParsingException {
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");
Expand Down Expand Up @@ -307,7 +308,7 @@ public String getUploaderUrl() throws ParsingException {

final String url = getUrlFromNavigationEndpoint(navigationEndpoint);

if (url != null && !url.isEmpty()) {
if (!isNullOrEmpty(url)) {
return url;
}
throw new ParsingException("Could not get uploader url");
Expand All @@ -331,7 +332,7 @@ public long getViewCount() 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 view count");
Expand Down Expand Up @@ -371,7 +372,7 @@ public String getThumbnailUrl() throws ParsingException {
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");
Expand All @@ -380,7 +381,7 @@ public String getName() throws ParsingException {
@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");
Expand All @@ -390,7 +391,7 @@ public String getUrl() throws ParsingException {
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");
Expand Down Expand Up @@ -426,7 +427,7 @@ public String getThumbnailUrl() throws ParsingException {
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");
Expand All @@ -435,7 +436,7 @@ public String getName() throws ParsingException {
@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");
Expand All @@ -451,7 +452,7 @@ public String getUploaderName() throws ParsingException {
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");
Expand All @@ -464,7 +465,7 @@ public long getStreamCount() throws ParsingException {
}
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getJsonResponse;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

/*
* Created by Christian Schabesberger on 06.08.15.
Expand Down Expand Up @@ -142,10 +143,10 @@ public String getTextualUploadDate() throws ParsingException {

try {
JsonObject micro = playerResponse.getObject("microformat").getObject("playerMicroformatRenderer");
if (micro.getString("uploadDate") != null && !micro.getString("uploadDate").isEmpty()) {
if (!isNullOrEmpty(micro.getString("uploadDate"))) {
return micro.getString("uploadDate");
}
if (micro.getString("publishDate") != null && !micro.getString("publishDate").isEmpty()) {
if (!isNullOrEmpty(micro.getString("publishDate"))) {
return micro.getString("publishDate");
}
} catch (Exception ignored) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Date;

import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.*;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

/*
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
Expand Down Expand Up @@ -98,7 +99,7 @@ public String getUrl() throws ParsingException {
@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");
}

Expand Down Expand Up @@ -215,7 +216,7 @@ public DateWrapper getUploadDate() throws ParsingException {
}

final String textualUploadDate = getTextualUploadDate();
if (timeAgoParser != null && textualUploadDate != null && !textualUploadDate.isEmpty()) {
if (timeAgoParser != null && !isNullOrEmpty(textualUploadDate)) {
try {
return timeAgoParser.parse(textualUploadDate);
} catch (ParsingException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.utils.Utils;

import java.io.IOException;

import javax.annotation.Nonnull;

import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getJsonResponse;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

public class YoutubeTrendingExtractor extends KioskExtractor<StreamInfoItem> {
private JsonObject initialData;
Expand Down Expand Up @@ -78,7 +80,7 @@ public String getName() throws ParsingException {
} catch (Exception e) {
throw new ParsingException("Could not get Trending name", e);
}
if (name != null && !name.isEmpty()) {
if (!isNullOrEmpty(name)) {
return name;
}
throw new ParsingException("Could not get Trending name");
Expand Down
Loading

0 comments on commit aa2eada

Please sign in to comment.