Skip to content

Commit

Permalink
Merge pull request #578 from TeamNewPipe/code_improvements
Browse files Browse the repository at this point in the history
Code improvements
  • Loading branch information
XiangRongLin authored Mar 30, 2021
2 parents af21838 + 657b00c commit 564d74c
Show file tree
Hide file tree
Showing 25 changed files with 40 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.schabi.newpipe.extractor.InfoItemsCollector;
import org.schabi.newpipe.extractor.exceptions.ParsingException;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

Expand Down Expand Up @@ -95,7 +96,7 @@ public void commit(CommentsInfoItemExtractor extractor) {
}

public List<CommentsInfoItem> getCommentsInfoItemList() {
List<CommentsInfoItem> siiList = new Vector<>();
List<CommentsInfoItem> siiList = new ArrayList<>();
for (InfoItem ii : super.getItems()) {
if (ii instanceof CommentsInfoItem) {
siiList.add((CommentsInfoItem) ii);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ public String latestUrl() {
public String getHeader(String name) {
for (Map.Entry<String, List<String>> headerEntry : responseHeaders.entrySet()) {
final String key = headerEntry.getKey();
if (key != null && key.equalsIgnoreCase(name)) {
if (headerEntry.getValue().size() > 0) {
return headerEntry.getValue().get(0);
}
if (key != null && key.equalsIgnoreCase(name) && !headerEntry.getValue().isEmpty()) {
return headerEntry.getValue().get(0);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/

public class ReCaptchaException extends ExtractionException {
private String url;
private final String url;

public ReCaptchaException(final String message, final String url) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public SearchQueryHandler fromQuery(String query,
}

public SearchQueryHandler fromQuery(String query) throws ParsingException {
return fromQuery(query, new ArrayList<String>(0), EMPTY_STRING);
return fromQuery(query, new ArrayList<>(0), EMPTY_STRING);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static PlaylistInfo getInfo(PlaylistExtractor extractor) throws Extractio
info.addError(e);
}
// do not fail if everything but the uploader infos could be collected
if (uploaderParsingErrors.size() > 0 &&
if (!uploaderParsingErrors.isEmpty() &&
(!info.getErrors().isEmpty() || uploaderParsingErrors.size() < 3)) {
info.addAllErrors(uploaderParsingErrors);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException, Extr

// Count pages
final Elements pageLists = d.getElementsByClass("pagelist");
if (pageLists.size() == 0)
if (pageLists.isEmpty())
return new InfoItemsPage<>(collector, null);

final Elements pages = pageLists.first().getElementsByTag("li");
Expand All @@ -96,7 +96,7 @@ public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException, Extr
int currentPage = -1;
for (int i = 0; i < pages.size(); i++) {
final Element pageElement = pages.get(i);
if (pageElement.getElementsByTag("span").size() > 0) {
if (!pageElement.getElementsByTag("span").isEmpty()) {
currentPage = i + 1;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public List<VideoStream> getVideoStreams() throws IOException, ExtractionExcepti
}

@Override
public List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionException {
public List<VideoStream> getVideoOnlyStreams() {
return null;
}

Expand Down Expand Up @@ -251,8 +251,8 @@ public String getErrorMessage() {

@Nonnull
@Override
public String getHost() throws ParsingException {
return null;
public String getHost() {
return "";
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader;
Expand All @@ -18,7 +16,7 @@
import java.io.IOException;

public class MediaCCCLiveStreamKiosk extends KioskExtractor<StreamInfoItem> {
public JsonArray doc;
private JsonArray doc;

public MediaCCCLiveStreamKiosk(StreamingService streamingService, ListLinkHandler linkHandler, String kioskId) {
super(streamingService, linkHandler, kioskId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeParseException;
import java.util.Locale;
import java.util.regex.Pattern;

public final class MediaCCCParsingHelper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ListLinkHandlerFactory getCommentsLHFactory() {
public SearchExtractor getSearchExtractor(SearchQueryHandler queryHandler) {
final List<String> contentFilters = queryHandler.getContentFilters();
boolean external = false;
if (contentFilters.size() > 0 && contentFilters.get(0).startsWith("sepia_")) {
if (!contentFilters.isEmpty() && contentFilters.get(0).startsWith("sepia_")) {
external = true;
}
return new PeertubeSearchExtractor(this, queryHandler, external);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void onFetchPage(@Nonnull final Downloader downloader)
}

final Response response = downloader.get(accountUrl);
if (response != null && response.responseBody() != null) {
if (response != null) {
setInitialData(response.responseBody());
} else {
throw new ExtractionException("Unable to extract PeerTube account data");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException
public void onFetchPage(final Downloader downloader) throws IOException, ExtractionException {
final Response response = downloader.get(
baseUrl + PeertubeChannelLinkHandlerFactory.API_ENDPOINT + getId());
if (response != null && response.responseBody() != null) {
if (response != null ) {
setInitialData(response.responseBody());
} else {
throw new ExtractionException("Unable to extract PeerTube channel data");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;

import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.comments.CommentsExtractor;
Expand All @@ -15,14 +14,11 @@
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
import org.schabi.newpipe.extractor.utils.JsonUtils;
import org.schabi.newpipe.extractor.utils.Utils;

import java.io.IOException;

import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.ITEMS_PER_PAGE;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.*;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

public class PeertubeCommentsExtractor extends CommentsExtractor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Locale;

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

public class PeertubeStreamExtractor extends StreamExtractor {
private final String baseUrl;
Expand Down Expand Up @@ -364,7 +365,7 @@ public String getErrorMessage() {
@Override
public void onFetchPage(final Downloader downloader) throws IOException, ExtractionException {
final Response response = downloader.get(baseUrl + PeertubeStreamLinkHandlerFactory.VIDEO_API_ENDPOINT + getId());
if (response != null && response.responseBody() != null) {
if (response != null) {
setInitialData(response.responseBody());
} else {
throw new ExtractionException("Unable to extract PeerTube channel data");
Expand Down Expand Up @@ -400,7 +401,7 @@ private void loadSubtitles() {
final String languageCode = JsonUtils.getString(caption, "language.id");
final String ext = url.substring(url.lastIndexOf(".") + 1);
final MediaFormat fmt = MediaFormat.getFromSuffix(ext);
if (fmt != null && languageCode != null)
if (fmt != null && !isNullOrEmpty(languageCode))
subtitles.add(new SubtitlesStream(fmt, languageCode, url, false));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static PeertubeSearchQueryHandlerFactory getInstance() {
@Override
public String getUrl(String searchString, List<String> contentFilters, String sortFilter) throws ParsingException {
String baseUrl;
if (contentFilters.size() > 0 && contentFilters.get(0).startsWith("sepia_")) {
if (!contentFilters.isEmpty() && contentFilters.get(0).startsWith("sepia_")) {
baseUrl = SEPIA_BASE_URL;
} else {
baseUrl = ServiceList.PeerTube.getBaseUrl();
Expand All @@ -35,10 +35,7 @@ public String getUrl(String searchString, List<String> contentFilters, String so
@Override
public String getUrl(String searchString, List<String> contentFilters, String sortFilter, String baseUrl) throws ParsingException {
try {
final String url = baseUrl + SEARCH_ENDPOINT
+ "?search=" + URLEncoder.encode(searchString, UTF_8);

return url;
return baseUrl + SEARCH_ENDPOINT + "?search=" + URLEncoder.encode(searchString, UTF_8);
} catch (UnsupportedEncodingException e) {
throw new ParsingException("Could not encode query", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class SoundcloudParsingHelper {
private SoundcloudParsingHelper() {
}

public synchronized static String clientId() throws ExtractionException, IOException {
public static synchronized String clientId() throws ExtractionException, IOException {
if (!isNullOrEmpty(clientId)) return clientId;

Downloader dl = NewPipe.getDownloader();
Expand Down Expand Up @@ -89,7 +89,7 @@ static boolean checkIfHardcodedClientIdIsValid() {
SoundcloudStreamExtractor e = (SoundcloudStreamExtractor) SoundCloud
.getStreamExtractor("https://soundcloud.com/liluzivert/do-what-i-want-produced-by-maaly-raw-don-cannon");
e.fetchPage();
return e.getAudioStreams().size() >= 1;
return !e.getAudioStreams().isEmpty();
} catch (Exception ignored) {
// No need to throw an exception here. If something went wrong, the client_id is wrong
return false;
Expand Down Expand Up @@ -131,7 +131,7 @@ public static JsonObject resolveFor(Downloader downloader, String url) throws IO
*
* @return the url resolved
*/
public static String resolveUrlWithEmbedPlayer(String apiUrl) throws IOException, ReCaptchaException, ParsingException {
public static String resolveUrlWithEmbedPlayer(String apiUrl) throws IOException, ReCaptchaException {

String response = NewPipe.getDownloader().get("https://w.soundcloud.com/player/?url="
+ URLEncoder.encode(apiUrl, UTF_8), SoundCloud.getLocalization()).responseBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void onFetchPage(@Nonnull Downloader downloader) throws IOException, Extr
throw new ParsingException("Could not parse json response", e);
}

if (searchCollection.size() == 0) {
if (searchCollection.isEmpty()) {
throw new SearchExtractor.NothingFoundException("Nothing found");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class SoundcloudChannelLinkHandlerFactory extends ListLinkHandlerFactory {
private static final SoundcloudChannelLinkHandlerFactory instance = new SoundcloudChannelLinkHandlerFactory();
private final String URL_PATTERN = "^https?://(www\\.|m\\.)?soundcloud.com/[0-9a-z_-]+" +
private static final String URL_PATTERN = "^https?://(www\\.|m\\.)?soundcloud.com/[0-9a-z_-]+" +
"(/((tracks|albums|sets|reposts|followers|following)/?)?)?([#?].*)?$";

public static SoundcloudChannelLinkHandlerFactory getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import java.util.List;

public class SoundcloudChartsLinkHandlerFactory extends ListLinkHandlerFactory {
private final String TOP_URL_PATTERN = "^https?://(www\\.|m\\.)?soundcloud.com/charts(/top)?/?([#?].*)?$";
private final String URL_PATTERN = "^https?://(www\\.|m\\.)?soundcloud.com/charts(/top|/new)?/?([#?].*)?$";
private static final String TOP_URL_PATTERN = "^https?://(www\\.|m\\.)?soundcloud.com/charts(/top)?/?([#?].*)?$";
private static final String URL_PATTERN = "^https?://(www\\.|m\\.)?soundcloud.com/charts(/top|/new)?/?([#?].*)?$";


@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class SoundcloudPlaylistLinkHandlerFactory extends ListLinkHandlerFactory {
private static final SoundcloudPlaylistLinkHandlerFactory instance = new SoundcloudPlaylistLinkHandlerFactory();
private final String URL_PATTERN = "^https?://(www\\.|m\\.)?soundcloud.com/[0-9a-z_-]+" +
private static final String URL_PATTERN = "^https?://(www\\.|m\\.)?soundcloud.com/[0-9a-z_-]+" +
"/sets/[0-9a-z_-]+/?([#?].*)?$";

public static SoundcloudPlaylistLinkHandlerFactory getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class SoundcloudStreamLinkHandlerFactory extends LinkHandlerFactory {
private static final SoundcloudStreamLinkHandlerFactory instance = new SoundcloudStreamLinkHandlerFactory();
private final String URL_PATTERN = "^https?://(www\\.|m\\.)?soundcloud.com/[0-9a-z_-]+" +
private static final String URL_PATTERN = "^https?://(www\\.|m\\.)?soundcloud.com/[0-9a-z_-]+" +
"/(?!(tracks|albums|sets|reposts|followers|following)/?$)[0-9a-z_-]+/?([#?].*)?$";

private SoundcloudStreamLinkHandlerFactory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public PlaylistExtractor getPlaylistExtractor(final ListLinkHandler linkHandler)
public SearchExtractor getSearchExtractor(SearchQueryHandler query) {
final List<String> contentFilters = query.getContentFilters();

if (contentFilters.size() > 0 && contentFilters.get(0).startsWith("music_")) {
if (!contentFilters.isEmpty() && contentFilters.get(0).startsWith("music_")) {
return new YoutubeMusicSearchExtractor(this, query);
} else {
return new YoutubeSearchExtractor(this, query);
Expand Down Expand Up @@ -221,6 +221,7 @@ public List<Localization> getSupportedLocalizations() {
return SUPPORTED_LANGUAGES;
}

@Override
public List<ContentCountry> getSupportedCountries() {
return SUPPORTED_COUNTRIES;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static YoutubeSearchQueryHandlerFactory getInstance() {
@Override
public String getUrl(String searchString, List<String> contentFilters, String sortFilter) throws ParsingException {
try {
if (contentFilters.size() > 0) {
if (!contentFilters.isEmpty()) {
switch (contentFilters.get(0)) {
case ALL:
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

Expand Down Expand Up @@ -153,11 +154,11 @@ private static StreamInfo extractStreams(StreamInfo streamInfo, StreamExtractor

// Lists can be null if a exception was thrown during extraction
if (streamInfo.getVideoStreams() == null)
streamInfo.setVideoStreams(new ArrayList<VideoStream>());
streamInfo.setVideoStreams(Collections.emptyList());
if (streamInfo.getVideoOnlyStreams() == null)
streamInfo.setVideoOnlyStreams(new ArrayList<VideoStream>());
streamInfo.setVideoOnlyStreams(Collections.emptyList());
if (streamInfo.getAudioStreams() == null)
streamInfo.setAudioStreams(new ArrayList<AudioStream>());
streamInfo.setAudioStreams(Collections.emptyList());

Exception dashMpdError = null;
if (!isNullOrEmpty(streamInfo.getDashMpdUrl())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.schabi.newpipe.extractor.exceptions.FoundAdException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;
Expand Down Expand Up @@ -111,7 +112,7 @@ public void commit(StreamInfoItemExtractor extractor) {
}

public List<StreamInfoItem> getStreamInfoItemList() {
List<StreamInfoItem> siiList = new Vector<>();
List<StreamInfoItem> siiList = new ArrayList<>();
for (InfoItem ii : super.getItems()) {
if (ii instanceof StreamInfoItem) {
siiList.add((StreamInfoItem) ii);
Expand Down

0 comments on commit 564d74c

Please sign in to comment.