From 37690058d260f5673876f78e6ec75364cfd3e59c Mon Sep 17 00:00:00 2001 From: Stypox Date: Wed, 16 Mar 2022 22:53:21 +0100 Subject: [PATCH 01/23] Add checkstyle to extractor gradle project With respect to NewPipe's checkstyle.xml, checkstyle is disabled for javadoc comments. There is no need for strict rules over comments here in the extractor, as sometimes javadocs are just needed to clarify a small thing and having empty/meaningless @param or @throws is useless. --- build.gradle | 1 + checkstyle/checkstyle.xml | 189 ++++++++++++++++++++++++++++++++++++++ extractor/build.gradle | 20 +++- 3 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 checkstyle/checkstyle.xml diff --git a/build.gradle b/build.gradle index 3cc6fa0c4a..ebac572105 100644 --- a/build.gradle +++ b/build.gradle @@ -30,6 +30,7 @@ allprojects { nanojsonVersion = "1d9e1aea9049fc9f85e68b43ba39fe7be1c1f751" spotbugsVersion = "4.6.0" junitVersion = "5.8.2" + checkstyleVersion = "9.3" // do not use latest version (10.0) as it requires compile SDK 11 } } diff --git a/checkstyle/checkstyle.xml b/checkstyle/checkstyle.xml new file mode 100644 index 0000000000..4a82f6be31 --- /dev/null +++ b/checkstyle/checkstyle.xml @@ -0,0 +1,189 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/extractor/build.gradle b/extractor/build.gradle index a9b5f7f3eb..70b51a3380 100644 --- a/extractor/build.gradle +++ b/extractor/build.gradle @@ -1,9 +1,25 @@ +plugins { + id 'checkstyle' +} + test { -// Pass on downloader type to tests for different CI jobs. See DownloaderFactory.java and ci.yml + // Pass on downloader type to tests for different CI jobs. See DownloaderFactory.java and ci.yml if (System.properties.containsKey('downloader')) { systemProperty('downloader', System.getProperty('downloader')) } useJUnitPlatform() + dependsOn checkstyleMain // run checkstyle when testing +} + +checkstyle { + getConfigDirectory().set(rootProject.file("checkstyle")) + ignoreFailures false + showViolations true + toolVersion checkstyleVersion +} + +checkstyleTest { + enabled false // do not checkstyle test files } dependencies { @@ -15,6 +31,8 @@ dependencies { implementation "com.github.spotbugs:spotbugs-annotations:$spotbugsVersion" implementation 'org.nibor.autolink:autolink:0.10.0' + checkstyle "com.puppycrawl.tools:checkstyle:$checkstyleVersion" + testImplementation platform("org.junit:junit-bom:$junitVersion") testImplementation 'org.junit.jupiter:junit-jupiter-api' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' From e4951a0623d3a344802b5d8d12d27f2365db441d Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 17 Mar 2022 16:19:06 +0100 Subject: [PATCH 02/23] Refactor code handling http headers in downloader.Request --- .../schabi/newpipe/extractor/downloader/Request.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Request.java b/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Request.java index 4bae8fc277..34f2a089de 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Request.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Request.java @@ -26,15 +26,15 @@ public Request(String httpMethod, String url, Map> headers, this.dataToSend = dataToSend; this.localization = localization; - Map> headersToSet = null; - if (headers == null) headers = Collections.emptyMap(); - + final Map> actualHeaders = new LinkedHashMap<>(); + if (headers != null) { + actualHeaders.putAll(headers); + } if (automaticLocalizationHeader && localization != null) { - headersToSet = new LinkedHashMap<>(headersFromLocalization(localization)); - headersToSet.putAll(headers); + actualHeaders.putAll(headersFromLocalization(localization)); } - this.headers = Collections.unmodifiableMap(headersToSet == null ? headers : headersToSet); + this.headers = Collections.unmodifiableMap(actualHeaders); } private Request(Builder builder) { From 8aba2b47b0995cab55883895c40fb35d91eb49a1 Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 17 Mar 2022 14:47:08 +0100 Subject: [PATCH 03/23] Fix checkstyle issues in subpackages with abstract classes --- .../extractor/MultiInfoItemsCollector.java | 4 +- .../extractor/channel/ChannelExtractor.java | 2 +- .../extractor/channel/ChannelInfo.java | 71 ++--- .../extractor/channel/ChannelInfoItem.java | 14 +- .../channel/ChannelInfoItemsCollector.java | 27 +- .../extractor/comments/CommentsInfo.java | 9 +- .../extractor/comments/CommentsInfoItem.java | 35 +-- .../comments/CommentsInfoItemExtractor.java | 3 +- .../comments/CommentsInfoItemsCollector.java | 50 ++-- .../extractor/downloader/Downloader.java | 33 ++- .../newpipe/extractor/downloader/Request.java | 128 +++++---- .../extractor/downloader/Response.java | 16 +- .../newpipe/extractor/feed/FeedExtractor.java | 2 +- .../newpipe/extractor/feed/FeedInfo.java | 22 +- .../extractor/kiosk/KioskExtractor.java | 7 +- .../newpipe/extractor/kiosk/KioskInfo.java | 27 +- .../newpipe/extractor/kiosk/KioskList.java | 57 ++-- .../extractor/linkhandler/LinkHandler.java | 4 +- .../linkhandler/LinkHandlerFactory.java | 36 +-- .../linkhandler/ListLinkHandler.java | 18 +- .../linkhandler/ListLinkHandlerFactory.java | 63 +++-- .../linkhandler/SearchQueryHandler.java | 12 +- .../SearchQueryHandlerFactory.java | 20 +- .../localization/ContentCountry.java | 28 +- .../extractor/localization/DateWrapper.java | 17 +- .../extractor/localization/Localization.java | 52 ++-- .../extractor/localization/TimeAgoParser.java | 38 +-- .../localization/TimeAgoPatternsManager.java | 12 +- .../extractor/playlist/PlaylistInfo.java | 75 +++--- .../extractor/playlist/PlaylistInfoItem.java | 10 +- .../playlist/PlaylistInfoItemExtractor.java | 2 - .../playlist/PlaylistInfoItemsCollector.java | 22 +- .../extractor/search/SearchExtractor.java | 9 +- .../newpipe/extractor/search/SearchInfo.java | 47 ++-- .../extractors/PeertubeStreamExtractor.java | 2 +- .../newpipe/extractor/stream/AudioStream.java | 18 +- .../newpipe/extractor/stream/Description.java | 20 +- .../newpipe/extractor/stream/Frameset.java | 12 +- .../newpipe/extractor/stream/Stream.java | 25 +- .../extractor/stream/StreamExtractor.java | 171 +++++------- .../newpipe/extractor/stream/StreamInfo.java | 252 +++++++++--------- .../extractor/stream/StreamInfoItem.java | 57 ++-- .../stream/StreamInfoItemExtractor.java | 5 +- .../stream/StreamInfoItemsCollector.java | 60 ++--- .../extractor/stream/StreamSegment.java | 2 +- .../extractor/stream/SubtitlesStream.java | 18 +- .../newpipe/extractor/stream/VideoStream.java | 28 +- .../subscription/SubscriptionExtractor.java | 21 +- .../subscription/SubscriptionItem.java | 9 +- .../suggestion/SuggestionExtractor.java | 12 +- 50 files changed, 903 insertions(+), 781 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/MultiInfoItemsCollector.java b/extractor/src/main/java/org/schabi/newpipe/extractor/MultiInfoItemsCollector.java index 1ceb1b1393..0c1deb72cc 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/MultiInfoItemsCollector.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/MultiInfoItemsCollector.java @@ -50,7 +50,7 @@ public class MultiInfoItemsCollector extends InfoItemsCollector { public static final long UNKNOWN_SUBSCRIBER_COUNT = -1; - public ChannelExtractor(StreamingService service, ListLinkHandler linkHandler) { + public ChannelExtractor(final StreamingService service, final ListLinkHandler linkHandler) { super(service, linkHandler); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfo.java index 00201358af..7e82ba4d74 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfo.java @@ -34,27 +34,36 @@ public class ChannelInfo extends ListInfo { - public ChannelInfo(int serviceId, String id, String url, String originalUrl, String name, ListLinkHandler listLinkHandler) { - super(serviceId, id, url, originalUrl, name, listLinkHandler.getContentFilters(), listLinkHandler.getSortFilter()); + public ChannelInfo(final int serviceId, + final String id, + final String url, + final String originalUrl, + final String name, + final ListLinkHandler listLinkHandler) { + super(serviceId, id, url, originalUrl, name, listLinkHandler.getContentFilters(), + listLinkHandler.getSortFilter()); } - public static ChannelInfo getInfo(String url) throws IOException, ExtractionException { + public static ChannelInfo getInfo(final String url) throws IOException, ExtractionException { return getInfo(NewPipe.getServiceByUrl(url), url); } - public static ChannelInfo getInfo(StreamingService service, String url) throws IOException, ExtractionException { - ChannelExtractor extractor = service.getChannelExtractor(url); + public static ChannelInfo getInfo(final StreamingService service, final String url) + throws IOException, ExtractionException { + final ChannelExtractor extractor = service.getChannelExtractor(url); extractor.fetchPage(); return getInfo(extractor); } - public static InfoItemsPage getMoreItems(StreamingService service, - String url, - Page page) throws IOException, ExtractionException { + public static InfoItemsPage getMoreItems(final StreamingService service, + final String url, + final Page page) + throws IOException, ExtractionException { return service.getChannelExtractor(url).getPage(page); } - public static ChannelInfo getInfo(ChannelExtractor extractor) throws IOException, ExtractionException { + public static ChannelInfo getInfo(final ChannelExtractor extractor) + throws IOException, ExtractionException { final int serviceId = extractor.getServiceId(); final String id = extractor.getId(); @@ -62,60 +71,62 @@ public static ChannelInfo getInfo(ChannelExtractor extractor) throws IOException final String originalUrl = extractor.getOriginalUrl(); final String name = extractor.getName(); - final ChannelInfo info = new ChannelInfo(serviceId, id, url, originalUrl, name, extractor.getLinkHandler()); + final ChannelInfo info = + new ChannelInfo(serviceId, id, url, originalUrl, name, extractor.getLinkHandler()); try { info.setAvatarUrl(extractor.getAvatarUrl()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } try { info.setBannerUrl(extractor.getBannerUrl()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } try { info.setFeedUrl(extractor.getFeedUrl()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } - final InfoItemsPage itemsPage = ExtractorHelper.getItemsPageOrLogError(info, extractor); + final InfoItemsPage itemsPage = + ExtractorHelper.getItemsPageOrLogError(info, extractor); info.setRelatedItems(itemsPage.getItems()); info.setNextPage(itemsPage.getNextPage()); try { info.setSubscriberCount(extractor.getSubscriberCount()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } try { info.setDescription(extractor.getDescription()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } try { info.setParentChannelName(extractor.getParentChannelName()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } try { info.setParentChannelUrl(extractor.getParentChannelUrl()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } try { info.setParentChannelAvatarUrl(extractor.getParentChannelAvatarUrl()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } try { info.setVerified(extractor.isVerified()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } @@ -137,7 +148,7 @@ public String getParentChannelName() { return parentChannelName; } - public void setParentChannelName(String parentChannelName) { + public void setParentChannelName(final String parentChannelName) { this.parentChannelName = parentChannelName; } @@ -145,7 +156,7 @@ public String getParentChannelUrl() { return parentChannelUrl; } - public void setParentChannelUrl(String parentChannelUrl) { + public void setParentChannelUrl(final String parentChannelUrl) { this.parentChannelUrl = parentChannelUrl; } @@ -153,7 +164,7 @@ public String getParentChannelAvatarUrl() { return parentChannelAvatarUrl; } - public void setParentChannelAvatarUrl(String parentChannelAvatarUrl) { + public void setParentChannelAvatarUrl(final String parentChannelAvatarUrl) { this.parentChannelAvatarUrl = parentChannelAvatarUrl; } @@ -161,7 +172,7 @@ public String getAvatarUrl() { return avatarUrl; } - public void setAvatarUrl(String avatarUrl) { + public void setAvatarUrl(final String avatarUrl) { this.avatarUrl = avatarUrl; } @@ -169,7 +180,7 @@ public String getBannerUrl() { return bannerUrl; } - public void setBannerUrl(String bannerUrl) { + public void setBannerUrl(final String bannerUrl) { this.bannerUrl = bannerUrl; } @@ -177,7 +188,7 @@ public String getFeedUrl() { return feedUrl; } - public void setFeedUrl(String feedUrl) { + public void setFeedUrl(final String feedUrl) { this.feedUrl = feedUrl; } @@ -185,7 +196,7 @@ public long getSubscriberCount() { return subscriberCount; } - public void setSubscriberCount(long subscriberCount) { + public void setSubscriberCount(final long subscriberCount) { this.subscriberCount = subscriberCount; } @@ -193,7 +204,7 @@ public String getDescription() { return description; } - public void setDescription(String description) { + public void setDescription(final String description) { this.description = description; } @@ -201,7 +212,7 @@ public String[] getDonationLinks() { return donationLinks; } - public void setDonationLinks(String[] donationLinks) { + public void setDonationLinks(final String[] donationLinks) { this.donationLinks = donationLinks; } @@ -209,7 +220,7 @@ public boolean isVerified() { return verified; } - public void setVerified(boolean verified) { + public void setVerified(final boolean verified) { this.verified = verified; } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfoItem.java b/extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfoItem.java index 51b9619ad1..ca29436b39 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfoItem.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfoItem.java @@ -29,7 +29,7 @@ public class ChannelInfoItem extends InfoItem { private long streamCount = -1; private boolean verified = false; - public ChannelInfoItem(int serviceId, String url, String name) { + public ChannelInfoItem(final int serviceId, final String url, final String name) { super(InfoType.CHANNEL, serviceId, url, name); } @@ -37,7 +37,7 @@ public String getDescription() { return description; } - public void setDescription(String description) { + public void setDescription(final String description) { this.description = description; } @@ -45,23 +45,23 @@ public long getSubscriberCount() { return subscriberCount; } - public void setSubscriberCount(long subscriber_count) { - this.subscriberCount = subscriber_count; + public void setSubscriberCount(final long subscriberCount) { + this.subscriberCount = subscriberCount; } public long getStreamCount() { return streamCount; } - public void setStreamCount(long stream_count) { - this.streamCount = stream_count; + public void setStreamCount(final long streamCount) { + this.streamCount = streamCount; } public boolean isVerified() { return verified; } - public void setVerified(boolean verified) { + public void setVerified(final boolean verified) { this.verified = verified; } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfoItemsCollector.java b/extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfoItemsCollector.java index 454ba30a95..5b085f8b86 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfoItemsCollector.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfoItemsCollector.java @@ -23,45 +23,42 @@ * along with NewPipe. If not, see . */ -public class ChannelInfoItemsCollector extends InfoItemsCollector { - public ChannelInfoItemsCollector(int serviceId) { +public final class ChannelInfoItemsCollector + extends InfoItemsCollector { + public ChannelInfoItemsCollector(final int serviceId) { super(serviceId); } @Override - public ChannelInfoItem extract(ChannelInfoItemExtractor extractor) throws ParsingException { - // important information - int serviceId = getServiceId(); - String name = extractor.getName(); - String url = extractor.getUrl(); - - ChannelInfoItem resultItem = new ChannelInfoItem(serviceId, url, name); - + public ChannelInfoItem extract(final ChannelInfoItemExtractor extractor) + throws ParsingException { + final ChannelInfoItem resultItem = new ChannelInfoItem( + getServiceId(), extractor.getUrl(), extractor.getName()); // optional information try { resultItem.setSubscriberCount(extractor.getSubscriberCount()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setStreamCount(extractor.getStreamCount()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setThumbnailUrl(extractor.getThumbnailUrl()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setDescription(extractor.getDescription()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setVerified(extractor.isVerified()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfo.java index a280500c80..98ec136e5b 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfo.java @@ -11,7 +11,7 @@ import java.io.IOException; -public class CommentsInfo extends ListInfo { +public final class CommentsInfo extends ListInfo { private CommentsInfo( final int serviceId, @@ -56,7 +56,8 @@ public static CommentsInfo getInfo(final CommentsExtractor commentsExtractor) public static InfoItemsPage getMoreItems( final CommentsInfo commentsInfo, final Page page) throws ExtractionException, IOException { - return getMoreItems(NewPipe.getService(commentsInfo.getServiceId()), commentsInfo.getUrl(), page); + return getMoreItems(NewPipe.getService(commentsInfo.getServiceId()), commentsInfo.getUrl(), + page); } public static InfoItemsPage getMoreItems( @@ -86,7 +87,7 @@ public void setCommentsExtractor(final CommentsExtractor commentsExtractor) { /** * @apiNote Warning: This method is experimental and may get removed in a future release. - * @return true if the comments are disabled otherwise false (default) + * @return {@code true} if the comments are disabled otherwise {@code false} (default) * @see CommentsExtractor#isCommentsDisabled() */ public boolean isCommentsDisabled() { @@ -95,7 +96,7 @@ public boolean isCommentsDisabled() { /** * @apiNote Warning: This method is experimental and may get removed in a future release. - * @param commentsDisabled true if the comments are disabled otherwise false + * @param commentsDisabled {@code true} if the comments are disabled otherwise {@code false} */ public void setCommentsDisabled(final boolean commentsDisabled) { this.commentsDisabled = commentsDisabled; diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItem.java b/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItem.java index 5ed6d68884..0f4cbbc249 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItem.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItem.java @@ -28,7 +28,7 @@ public class CommentsInfoItem extends InfoItem { public static final int NO_LIKE_COUNT = -1; public static final int NO_STREAM_POSITION = -1; - public CommentsInfoItem(int serviceId, String url, String name) { + public CommentsInfoItem(final int serviceId, final String url, final String name) { super(InfoType.COMMENT, serviceId, url, name); } @@ -36,7 +36,7 @@ public String getCommentId() { return commentId; } - public void setCommentId(String commentId) { + public void setCommentId(final String commentId) { this.commentId = commentId; } @@ -44,7 +44,7 @@ public String getCommentText() { return commentText; } - public void setCommentText(String commentText) { + public void setCommentText(final String commentText) { this.commentText = commentText; } @@ -52,7 +52,7 @@ public String getUploaderName() { return uploaderName; } - public void setUploaderName(String uploaderName) { + public void setUploaderName(final String uploaderName) { this.uploaderName = uploaderName; } @@ -60,7 +60,7 @@ public String getUploaderAvatarUrl() { return uploaderAvatarUrl; } - public void setUploaderAvatarUrl(String uploaderAvatarUrl) { + public void setUploaderAvatarUrl(final String uploaderAvatarUrl) { this.uploaderAvatarUrl = uploaderAvatarUrl; } @@ -68,7 +68,7 @@ public String getUploaderUrl() { return uploaderUrl; } - public void setUploaderUrl(String uploaderUrl) { + public void setUploaderUrl(final String uploaderUrl) { this.uploaderUrl = uploaderUrl; } @@ -76,7 +76,7 @@ public String getTextualUploadDate() { return textualUploadDate; } - public void setTextualUploadDate(String textualUploadDate) { + public void setTextualUploadDate(final String textualUploadDate) { this.textualUploadDate = textualUploadDate; } @@ -85,7 +85,7 @@ public DateWrapper getUploadDate() { return uploadDate; } - public void setUploadDate(@Nullable DateWrapper uploadDate) { + public void setUploadDate(@Nullable final DateWrapper uploadDate) { this.uploadDate = uploadDate; } @@ -97,7 +97,7 @@ public int getLikeCount() { return likeCount; } - public void setLikeCount(int likeCount) { + public void setLikeCount(final int likeCount) { this.likeCount = likeCount; } @@ -105,11 +105,11 @@ public String getTextualLikeCount() { return textualLikeCount; } - public void setTextualLikeCount(String textualLikeCount) { + public void setTextualLikeCount(final String textualLikeCount) { this.textualLikeCount = textualLikeCount; } - public void setHeartedByUploader(boolean isHeartedByUploader) { + public void setHeartedByUploader(final boolean isHeartedByUploader) { this.heartedByUploader = isHeartedByUploader; } @@ -121,11 +121,11 @@ public boolean isPinned() { return pinned; } - public void setPinned(boolean pinned) { + public void setPinned(final boolean pinned) { this.pinned = pinned; } - public void setUploaderVerified(boolean uploaderVerified) { + public void setUploaderVerified(final boolean uploaderVerified) { this.uploaderVerified = uploaderVerified; } @@ -146,7 +146,12 @@ public int getStreamPosition() { return streamPosition; } - public void setReplies(@Nullable Page replies) { this.replies = replies; } + public void setReplies(@Nullable final Page replies) { + this.replies = replies; + } - public Page getReplies() { return this.replies; } + @Nullable + public Page getReplies() { + return this.replies; + } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemExtractor.java index 39c0b48b47..40279eb0d1 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemExtractor.java @@ -18,7 +18,8 @@ public interface CommentsInfoItemExtractor extends InfoItemExtractor { * *
* - * NOTE: Currently only implemented for YT {@link YoutubeCommentsInfoItemExtractor#getLikeCount()} + * NOTE: Currently only implemented for YT {@link + * YoutubeCommentsInfoItemExtractor#getLikeCount()} * with limitations (only approximate like count is returned) * * @see StreamExtractor#getLikeCount() diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemsCollector.java b/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemsCollector.java index ea94658096..0a5388d0f8 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemsCollector.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemsCollector.java @@ -1,101 +1,97 @@ package org.schabi.newpipe.extractor.comments; -import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.InfoItemsCollector; import org.schabi.newpipe.extractor.exceptions.ParsingException; import java.util.ArrayList; import java.util.List; -public class CommentsInfoItemsCollector extends InfoItemsCollector { +public final class CommentsInfoItemsCollector + extends InfoItemsCollector { - public CommentsInfoItemsCollector(int serviceId) { + public CommentsInfoItemsCollector(final int serviceId) { super(serviceId); } @Override - public CommentsInfoItem extract(CommentsInfoItemExtractor extractor) throws ParsingException { - - // important information - int serviceId = getServiceId(); - String url = extractor.getUrl(); - String name = extractor.getName(); - - CommentsInfoItem resultItem = new CommentsInfoItem(serviceId, url, name); + public CommentsInfoItem extract(final CommentsInfoItemExtractor extractor) + throws ParsingException { + final CommentsInfoItem resultItem = new CommentsInfoItem( + getServiceId(), extractor.getUrl(), extractor.getName()); // optional information try { resultItem.setCommentId(extractor.getCommentId()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setCommentText(extractor.getCommentText()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setUploaderName(extractor.getUploaderName()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setUploaderAvatarUrl(extractor.getUploaderAvatarUrl()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setUploaderUrl(extractor.getUploaderUrl()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setTextualUploadDate(extractor.getTextualUploadDate()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setUploadDate(extractor.getUploadDate()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setLikeCount(extractor.getLikeCount()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setTextualLikeCount(extractor.getTextualLikeCount()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setThumbnailUrl(extractor.getThumbnailUrl()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setHeartedByUploader(extractor.isHeartedByUploader()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setPinned(extractor.isPinned()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setStreamPosition(extractor.getStreamPosition()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setReplies(extractor.getReplies()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } @@ -103,10 +99,10 @@ public CommentsInfoItem extract(CommentsInfoItemExtractor extractor) throws Pars } @Override - public void commit(CommentsInfoItemExtractor extractor) { + public void commit(final CommentsInfoItemExtractor extractor) { try { addItem(extract(extractor)); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Downloader.java b/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Downloader.java index 70aa169688..75d0bbf805 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Downloader.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Downloader.java @@ -19,13 +19,14 @@ public abstract class Downloader { /** * Do a GET request to get the resource that the url is pointing to.
*
- * This method calls {@link #get(String, Map, Localization)} with the default preferred localization. It should only be - * used when the resource that will be fetched won't be affected by the localization. + * This method calls {@link #get(String, Map, Localization)} with the default preferred + * localization. It should only be used when the resource that will be fetched won't be affected + * by the localization. * * @param url the URL that is pointing to the wanted resource * @return the result of the GET request */ - public Response get(String url) throws IOException, ReCaptchaException { + public Response get(final String url) throws IOException, ReCaptchaException { return get(url, null, NewPipe.getPreferredLocalization()); } @@ -38,7 +39,8 @@ public Response get(String url) throws IOException, ReCaptchaException { * @param localization the source of the value of the {@code Accept-Language} header * @return the result of the GET request */ - public Response get(String url, @Nullable Localization localization) throws IOException, ReCaptchaException { + public Response get(final String url, @Nullable final Localization localization) + throws IOException, ReCaptchaException { return get(url, null, localization); } @@ -50,7 +52,8 @@ public Response get(String url, @Nullable Localization localization) throws IOEx * Any default headers should be overridden by these. * @return the result of the GET request */ - public Response get(String url, @Nullable Map> headers) throws IOException, ReCaptchaException { + public Response get(final String url, @Nullable final Map> headers) + throws IOException, ReCaptchaException { return get(url, headers, NewPipe.getPreferredLocalization()); } @@ -65,7 +68,9 @@ public Response get(String url, @Nullable Map> headers) thr * @param localization the source of the value of the {@code Accept-Language} header * @return the result of the GET request */ - public Response get(String url, @Nullable Map> headers, @Nullable Localization localization) + public Response get(final String url, + @Nullable final Map> headers, + @Nullable final Localization localization) throws IOException, ReCaptchaException { return execute(Request.newBuilder() .get(url) @@ -80,7 +85,7 @@ public Response get(String url, @Nullable Map> headers, @Nu * @param url the URL that is pointing to the wanted resource * @return the result of the HEAD request */ - public Response head(String url) throws IOException, ReCaptchaException { + public Response head(final String url) throws IOException, ReCaptchaException { return head(url, null); } @@ -92,7 +97,7 @@ public Response head(String url) throws IOException, ReCaptchaException { * Any default headers should be overridden by these. * @return the result of the HEAD request */ - public Response head(String url, @Nullable Map> headers) + public Response head(final String url, @Nullable final Map> headers) throws IOException, ReCaptchaException { return execute(Request.newBuilder() .head(url) @@ -109,7 +114,9 @@ public Response head(String url, @Nullable Map> headers) * @param dataToSend byte array that will be sent when doing the request. * @return the result of the GET request */ - public Response post(String url, @Nullable Map> headers, @Nullable byte[] dataToSend) + public Response post(final String url, + @Nullable final Map> headers, + @Nullable final byte[] dataToSend) throws IOException, ReCaptchaException { return post(url, headers, dataToSend, NewPipe.getPreferredLocalization()); } @@ -126,7 +133,10 @@ public Response post(String url, @Nullable Map> headers, @N * @param localization the source of the value of the {@code Accept-Language} header * @return the result of the GET request */ - public Response post(String url, @Nullable Map> headers, @Nullable byte[] dataToSend, @Nullable Localization localization) + public Response post(final String url, + @Nullable final Map> headers, + @Nullable final byte[] dataToSend, + @Nullable final Localization localization) throws IOException, ReCaptchaException { return execute(Request.newBuilder() .post(url, dataToSend) @@ -140,5 +150,6 @@ public Response post(String url, @Nullable Map> headers, @N * * @return the result of the request */ - public abstract Response execute(@Nonnull Request request) throws IOException, ReCaptchaException; + public abstract Response execute(@Nonnull Request request) + throws IOException, ReCaptchaException; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Request.java b/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Request.java index 34f2a089de..05a9927829 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Request.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Request.java @@ -2,24 +2,42 @@ import org.schabi.newpipe.extractor.localization.Localization; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.*; /** - * An object that holds request information used when {@link Downloader#execute(Request) executing} a request. + * An object that holds request information used when {@link Downloader#execute(Request) executing} + * a request. */ public class Request { private final String httpMethod; private final String url; private final Map> headers; - @Nullable private final byte[] dataToSend; - @Nullable private final Localization localization; - - public Request(String httpMethod, String url, Map> headers, @Nullable byte[] dataToSend, - @Nullable Localization localization, boolean automaticLocalizationHeader) { - if (httpMethod == null) throw new IllegalArgumentException("Request's httpMethod is null"); - if (url == null) throw new IllegalArgumentException("Request's url is null"); + @Nullable + private final byte[] dataToSend; + @Nullable + private final Localization localization; + + public Request(final String httpMethod, + final String url, + @Nullable final Map> headers, + @Nullable final byte[] dataToSend, + @Nullable final Localization localization, + final boolean automaticLocalizationHeader) { + if (httpMethod == null) { + throw new IllegalArgumentException("Request's httpMethod is null"); + } + if (url == null) { + throw new IllegalArgumentException("Request's url is null"); + } this.httpMethod = httpMethod; this.url = url; @@ -37,7 +55,7 @@ public Request(String httpMethod, String url, Map> headers, this.headers = Collections.unmodifiableMap(actualHeaders); } - private Request(Builder builder) { + private Request(final Builder builder) { this(builder.httpMethod, builder.url, builder.headers, builder.dataToSend, builder.localization, builder.automaticLocalizationHeader); } @@ -94,7 +112,7 @@ public static Builder newBuilder() { public static final class Builder { private String httpMethod; private String url; - private Map> headers = new LinkedHashMap<>(); + private final Map> headers = new LinkedHashMap<>(); private byte[] dataToSend; private Localization localization; private boolean automaticLocalizationHeader = true; @@ -105,27 +123,28 @@ public Builder() { /** * A http method (i.e. {@code GET, POST, HEAD}). */ - public Builder httpMethod(String httpMethod) { - this.httpMethod = httpMethod; + public Builder httpMethod(final String httpMethodToSet) { + this.httpMethod = httpMethodToSet; return this; } /** * The URL that is pointing to the wanted resource. */ - public Builder url(String url) { - this.url = url; + public Builder url(final String urlToSet) { + this.url = urlToSet; return this; } /** * A list of headers that will be used in the request.
- * Any default headers that the implementation may have, should be overridden by these. + * Any default headers that the implementation may have, should be overridden by + * these. */ - public Builder headers(@Nullable Map> headers) { + public Builder headers(@Nullable final Map> headersToSet) { this.headers.clear(); - if (headers != null) { - this.headers.putAll(headers); + if (headersToSet != null) { + this.headers.putAll(headersToSet); } return this; } @@ -137,8 +156,8 @@ public Builder headers(@Nullable Map> headers) { * The implementation should make note of some recommended headers * (for example, {@code Content-Length} in a post request). */ - public Builder dataToSend(byte[] dataToSend) { - this.dataToSend = dataToSend; + public Builder dataToSend(final byte[] dataToSendToSet) { + this.dataToSend = dataToSendToSet; return this; } @@ -148,16 +167,16 @@ public Builder dataToSend(byte[] dataToSend) { * Usually the {@code Accept-Language} will be set to this value (a helper * method to do this easily: {@link Request#headersFromLocalization(Localization)}). */ - public Builder localization(Localization localization) { - this.localization = localization; + public Builder localization(final Localization localizationToSet) { + this.localization = localizationToSet; return this; } /** * If localization headers should automatically be included in the request. */ - public Builder automaticLocalizationHeader(boolean automaticLocalizationHeader) { - this.automaticLocalizationHeader = automaticLocalizationHeader; + public Builder automaticLocalizationHeader(final boolean automaticLocalizationHeaderToSet) { + this.automaticLocalizationHeader = automaticLocalizationHeaderToSet; return this; } @@ -170,22 +189,22 @@ public Request build() { // Http Methods Utils //////////////////////////////////////////////////////////////////////////*/ - public Builder get(String url) { + public Builder get(final String urlToSet) { this.httpMethod = "GET"; - this.url = url; + this.url = urlToSet; return this; } - public Builder head(String url) { + public Builder head(final String urlToSet) { this.httpMethod = "HEAD"; - this.url = url; + this.url = urlToSet; return this; } - public Builder post(String url, @Nullable byte[] dataToSend) { + public Builder post(final String urlToSet, @Nullable final byte[] dataToSendToSet) { this.httpMethod = "POST"; - this.url = url; - this.dataToSend = dataToSend; + this.url = urlToSet; + this.dataToSend = dataToSendToSet; return this; } @@ -193,13 +212,13 @@ public Builder post(String url, @Nullable byte[] dataToSend) { // Additional Headers Utils //////////////////////////////////////////////////////////////////////////*/ - public Builder setHeaders(String headerName, List headerValueList) { + public Builder setHeaders(final String headerName, final List headerValueList) { this.headers.remove(headerName); this.headers.put(headerName, headerValueList); return this; } - public Builder addHeaders(String headerName, List headerValueList) { + public Builder addHeaders(final String headerName, final List headerValueList) { @Nullable List currentHeaderValueList = this.headers.get(headerName); if (currentHeaderValueList == null) { currentHeaderValueList = new ArrayList<>(); @@ -210,11 +229,11 @@ public Builder addHeaders(String headerName, List headerValueList) { return this; } - public Builder setHeader(String headerName, String headerValue) { + public Builder setHeader(final String headerName, final String headerValue) { return setHeaders(headerName, Collections.singletonList(headerValue)); } - public Builder addHeader(String headerName, String headerValue) { + public Builder addHeader(final String headerName, final String headerValue) { return addHeaders(headerName, Collections.singletonList(headerValue)); } @@ -226,15 +245,20 @@ public Builder addHeader(String headerName, String headerValue) { @SuppressWarnings("WeakerAccess") @Nonnull - public static Map> headersFromLocalization(@Nullable Localization localization) { - if (localization == null) return Collections.emptyMap(); + public static Map> headersFromLocalization( + @Nullable final Localization localization) { + if (localization == null) { + return Collections.emptyMap(); + } final Map> headers = new LinkedHashMap<>(); if (!localization.getCountryCode().isEmpty()) { - headers.put("Accept-Language", Collections.singletonList(localization.getLocalizationCode() + - ", " + localization.getLanguageCode() + ";q=0.9")); + headers.put("Accept-Language", + Collections.singletonList(localization.getLocalizationCode() + + ", " + localization.getLanguageCode() + ";q=0.9")); } else { - headers.put("Accept-Language", Collections.singletonList(localization.getLanguageCode())); + headers.put("Accept-Language", + Collections.singletonList(localization.getLanguageCode())); } return headers; @@ -245,15 +269,19 @@ public static Map> headersFromLocalization(@Nullable Locali //////////////////////////////////////////////////////////////////////////*/ @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Request request = (Request) o; - return httpMethod.equals(request.httpMethod) && - url.equals(request.url) && - headers.equals(request.headers) && - Arrays.equals(dataToSend, request.dataToSend) && - Objects.equals(localization, request.localization); + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + final Request request = (Request) o; + return httpMethod.equals(request.httpMethod) + && url.equals(request.url) + && headers.equals(request.headers) + && Arrays.equals(dataToSend, request.dataToSend) + && Objects.equals(localization, request.localization); } @Override diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java b/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java index c986f72d5f..ac792dc756 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java @@ -17,11 +17,14 @@ public class Response { private final String latestUrl; - public Response(int responseCode, String responseMessage, Map> responseHeaders, - @Nullable String responseBody, @Nullable String latestUrl) { + public Response(final int responseCode, + final String responseMessage, + @Nullable final Map> responseHeaders, + @Nullable final String responseBody, + @Nullable final String latestUrl) { this.responseCode = responseCode; this.responseMessage = responseMessage; - this.responseHeaders = responseHeaders != null ? responseHeaders : Collections.>emptyMap(); + this.responseHeaders = responseHeaders == null ? Collections.emptyMap() : responseHeaders; this.responseBody = responseBody == null ? "" : responseBody; this.latestUrl = latestUrl; @@ -60,14 +63,15 @@ public String latestUrl() { /** * For easy access to some header value that (usually) don't repeat itself. - *

For getting all the values associated to the header, use {@link #responseHeaders()} (e.g. {@code Set-Cookie}). + *

For getting all the values associated to the header, use {@link #responseHeaders()} (e.g. + * {@code Set-Cookie}). * * @param name the name of the header * @return the first value assigned to this header */ @Nullable - public String getHeader(String name) { - for (Map.Entry> headerEntry : responseHeaders.entrySet()) { + public String getHeader(final String name) { + for (final Map.Entry> headerEntry : responseHeaders.entrySet()) { final String key = headerEntry.getKey(); if (key != null && key.equalsIgnoreCase(name) && !headerEntry.getValue().isEmpty()) { return headerEntry.getValue().get(0); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/feed/FeedExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/feed/FeedExtractor.java index df3e8915b2..42b5a0cbfc 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/feed/FeedExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/feed/FeedExtractor.java @@ -11,7 +11,7 @@ * YouTube is an example of a service that has this alternative available. */ public abstract class FeedExtractor extends ListExtractor { - public FeedExtractor(StreamingService service, ListLinkHandler listLinkHandler) { + public FeedExtractor(final StreamingService service, final ListLinkHandler listLinkHandler) { super(service, listLinkHandler); } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/feed/FeedInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/feed/FeedInfo.java index 03fe78f6df..9beb46a23c 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/feed/FeedInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/feed/FeedInfo.java @@ -13,26 +13,35 @@ public class FeedInfo extends ListInfo { - public FeedInfo(int serviceId, String id, String url, String originalUrl, String name, List contentFilter, String sortFilter) { + public FeedInfo(final int serviceId, + final String id, + final String url, + final String originalUrl, + final String name, + final List contentFilter, + final String sortFilter) { super(serviceId, id, url, originalUrl, name, contentFilter, sortFilter); } - public static FeedInfo getInfo(String url) throws IOException, ExtractionException { + public static FeedInfo getInfo(final String url) throws IOException, ExtractionException { return getInfo(NewPipe.getServiceByUrl(url), url); } - public static FeedInfo getInfo(StreamingService service, String url) throws IOException, ExtractionException { + public static FeedInfo getInfo(final StreamingService service, final String url) + throws IOException, ExtractionException { final FeedExtractor extractor = service.getFeedExtractor(url); if (extractor == null) { - throw new IllegalArgumentException("Service \"" + service.getServiceInfo().getName() + "\" doesn't support FeedExtractor."); + throw new IllegalArgumentException("Service \"" + service.getServiceInfo().getName() + + "\" doesn't support FeedExtractor."); } extractor.fetchPage(); return getInfo(extractor); } - public static FeedInfo getInfo(FeedExtractor extractor) throws IOException, ExtractionException { + public static FeedInfo getInfo(final FeedExtractor extractor) + throws IOException, ExtractionException { extractor.fetchPage(); final int serviceId = extractor.getServiceId(); @@ -43,7 +52,8 @@ public static FeedInfo getInfo(FeedExtractor extractor) throws IOException, Extr final FeedInfo info = new FeedInfo(serviceId, id, url, originalUrl, name, null, null); - final InfoItemsPage itemsPage = ExtractorHelper.getItemsPageOrLogError(info, extractor); + final InfoItemsPage itemsPage + = ExtractorHelper.getItemsPageOrLogError(info, extractor); info.setRelatedItems(itemsPage.getItems()); info.setNextPage(itemsPage.getNextPage()); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskExtractor.java index 7abefbd949..3bf986a9e9 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskExtractor.java @@ -31,9 +31,9 @@ public abstract class KioskExtractor extends ListExtractor { private final String id; - public KioskExtractor(StreamingService streamingService, - ListLinkHandler linkHandler, - String kioskId) { + public KioskExtractor(final StreamingService streamingService, + final ListLinkHandler linkHandler, + final String kioskId) { super(streamingService, linkHandler); this.id = kioskId; } @@ -50,7 +50,6 @@ public String getId() { * In order to get the name of the kiosk in the desired language we have to * crawl if from the website. * @return the translated version of id - * @throws ParsingException */ @Nonnull @Override diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskInfo.java index e054214e2c..6ee1dcb9fa 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskInfo.java @@ -26,34 +26,30 @@ import org.schabi.newpipe.extractor.Page; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.exceptions.ExtractionException; -import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.utils.ExtractorHelper; import java.io.IOException; -public class KioskInfo extends ListInfo { - private KioskInfo(int serviceId, ListLinkHandler linkHandler, String name) throws ParsingException { +public final class KioskInfo extends ListInfo { + private KioskInfo(final int serviceId, final ListLinkHandler linkHandler, final String name) { super(serviceId, linkHandler, name); } - public static ListExtractor.InfoItemsPage getMoreItems(StreamingService service, - String url, - Page page) + public static ListExtractor.InfoItemsPage getMoreItems( + final StreamingService service, final String url, final Page page) throws IOException, ExtractionException { - KioskList kl = service.getKioskList(); - KioskExtractor extractor = kl.getExtractorByUrl(url, page); - return extractor.getPage(page); + return service.getKioskList().getExtractorByUrl(url, page).getPage(page); } - public static KioskInfo getInfo(String url) throws IOException, ExtractionException { + public static KioskInfo getInfo(final String url) throws IOException, ExtractionException { return getInfo(NewPipe.getServiceByUrl(url), url); } - public static KioskInfo getInfo(StreamingService service, String url) throws IOException, ExtractionException { - KioskList kl = service.getKioskList(); - KioskExtractor extractor = kl.getExtractorByUrl(url, null); + public static KioskInfo getInfo(final StreamingService service, final String url) + throws IOException, ExtractionException { + final KioskExtractor extractor = service.getKioskList().getExtractorByUrl(url, null); extractor.fetchPage(); return getInfo(extractor); } @@ -63,13 +59,14 @@ public static KioskInfo getInfo(StreamingService service, String url) throws IOE * * @param extractor an extractor where fetchPage() was already got called on. */ - public static KioskInfo getInfo(KioskExtractor extractor) throws ExtractionException { + public static KioskInfo getInfo(final KioskExtractor extractor) throws ExtractionException { final KioskInfo info = new KioskInfo(extractor.getServiceId(), extractor.getLinkHandler(), extractor.getName()); - final ListExtractor.InfoItemsPage itemsPage = ExtractorHelper.getItemsPageOrLogError(info, extractor); + final ListExtractor.InfoItemsPage itemsPage + = ExtractorHelper.getItemsPageOrLogError(info, extractor); info.setRelatedItems(itemsPage.getItems()); info.setNextPage(itemsPage.getNextPage()); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskList.java b/extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskList.java index 913cbe26ba..cc9df054c9 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskList.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskList.java @@ -19,9 +19,9 @@ public class KioskList { public interface KioskExtractorFactory { - KioskExtractor createNewKiosk(final StreamingService streamingService, - final String url, - final String kioskId) + KioskExtractor createNewKiosk(StreamingService streamingService, + String url, + String kioskId) throws ExtractionException, IOException; } @@ -34,8 +34,8 @@ KioskExtractor createNewKiosk(final StreamingService streamingService, @Nullable private ContentCountry forcedContentCountry; - private class KioskEntry { - public KioskEntry(KioskExtractorFactory ef, ListLinkHandlerFactory h) { + private static class KioskEntry { + KioskEntry(final KioskExtractorFactory ef, final ListLinkHandlerFactory h) { extractorFactory = ef; handlerFactory = h; } @@ -44,11 +44,13 @@ public KioskEntry(KioskExtractorFactory ef, ListLinkHandlerFactory h) { final ListLinkHandlerFactory handlerFactory; } - public KioskList(StreamingService service) { + public KioskList(final StreamingService service) { this.service = service; } - public void addKioskEntry(KioskExtractorFactory extractorFactory, ListLinkHandlerFactory handlerFactory, String id) + public void addKioskEntry(final KioskExtractorFactory extractorFactory, + final ListLinkHandlerFactory handlerFactory, + final String id) throws Exception { if (kioskList.get(id) != null) { throw new Exception("Kiosk with type " + id + " already exists."); @@ -56,7 +58,7 @@ public void addKioskEntry(KioskExtractorFactory extractorFactory, ListLinkHandle kioskList.put(id, new KioskEntry(extractorFactory, handlerFactory)); } - public void setDefaultKiosk(String kioskType) { + public void setDefaultKiosk(final String kioskType) { defaultKiosk = kioskType; } @@ -65,19 +67,20 @@ public KioskExtractor getDefaultKioskExtractor() return getDefaultKioskExtractor(null); } - public KioskExtractor getDefaultKioskExtractor(Page nextPage) + public KioskExtractor getDefaultKioskExtractor(final Page nextPage) throws ExtractionException, IOException { return getDefaultKioskExtractor(nextPage, NewPipe.getPreferredLocalization()); } - public KioskExtractor getDefaultKioskExtractor(Page nextPage, Localization localization) + public KioskExtractor getDefaultKioskExtractor(final Page nextPage, + final Localization localization) throws ExtractionException, IOException { if (!isNullOrEmpty(defaultKiosk)) { return getExtractorById(defaultKiosk, nextPage, localization); } else { if (!kioskList.isEmpty()) { // if not set get any entry - Object[] keySet = kioskList.keySet().toArray(); + final Object[] keySet = kioskList.keySet().toArray(); return getExtractorById(keySet[0].toString(), nextPage, localization); } else { return null; @@ -89,22 +92,28 @@ public String getDefaultKioskId() { return defaultKiosk; } - public KioskExtractor getExtractorById(String kioskId, Page nextPage) + public KioskExtractor getExtractorById(final String kioskId, final Page nextPage) throws ExtractionException, IOException { return getExtractorById(kioskId, nextPage, NewPipe.getPreferredLocalization()); } - public KioskExtractor getExtractorById(String kioskId, Page nextPage, Localization localization) + public KioskExtractor getExtractorById(final String kioskId, + final Page nextPage, + final Localization localization) throws ExtractionException, IOException { - KioskEntry ke = kioskList.get(kioskId); + final KioskEntry ke = kioskList.get(kioskId); if (ke == null) { throw new ExtractionException("No kiosk found with the type: " + kioskId); } else { final KioskExtractor kioskExtractor = ke.extractorFactory.createNewKiosk(service, ke.handlerFactory.fromId(kioskId).getUrl(), kioskId); - if (forcedLocalization != null) kioskExtractor.forceLocalization(forcedLocalization); - if (forcedContentCountry != null) kioskExtractor.forceContentCountry(forcedContentCountry); + if (forcedLocalization != null) { + kioskExtractor.forceLocalization(forcedLocalization); + } + if (forcedContentCountry != null) { + kioskExtractor.forceContentCountry(forcedContentCountry); + } return kioskExtractor; } @@ -114,15 +123,17 @@ public Set getAvailableKiosks() { return kioskList.keySet(); } - public KioskExtractor getExtractorByUrl(String url, Page nextPage) + public KioskExtractor getExtractorByUrl(final String url, final Page nextPage) throws ExtractionException, IOException { return getExtractorByUrl(url, nextPage, NewPipe.getPreferredLocalization()); } - public KioskExtractor getExtractorByUrl(String url, Page nextPage, Localization localization) + public KioskExtractor getExtractorByUrl(final String url, + final Page nextPage, + final Localization localization) throws ExtractionException, IOException { - for (Map.Entry e : kioskList.entrySet()) { - KioskEntry ke = e.getValue(); + for (final Map.Entry e : kioskList.entrySet()) { + final KioskEntry ke = e.getValue(); if (ke.handlerFactory.acceptUrl(url)) { return getExtractorById(ke.handlerFactory.getId(url), nextPage, localization); } @@ -130,15 +141,15 @@ public KioskExtractor getExtractorByUrl(String url, Page nextPage, Localization throw new ExtractionException("Could not find a kiosk that fits to the url: " + url); } - public ListLinkHandlerFactory getListLinkHandlerFactoryByType(String type) { + public ListLinkHandlerFactory getListLinkHandlerFactoryByType(final String type) { return kioskList.get(type).handlerFactory; } - public void forceLocalization(@Nullable Localization localization) { + public void forceLocalization(@Nullable final Localization localization) { this.forcedLocalization = localization; } - public void forceContentCountry(@Nullable ContentCountry contentCountry) { + public void forceContentCountry(@Nullable final ContentCountry contentCountry) { this.forcedContentCountry = contentCountry; } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/LinkHandler.java b/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/LinkHandler.java index b7439c39d1..5bc7a4a11a 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/LinkHandler.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/LinkHandler.java @@ -10,13 +10,13 @@ public class LinkHandler implements Serializable { protected final String url; protected final String id; - public LinkHandler(String originalUrl, String url, String id) { + public LinkHandler(final String originalUrl, final String url, final String id) { this.originalUrl = originalUrl; this.url = url; this.id = id; } - public LinkHandler(LinkHandler handler) { + public LinkHandler(final LinkHandler handler) { this(handler.originalUrl, handler.url, handler.id); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/LinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/LinkHandlerFactory.java index 7d04b87cfb..1c6ae2abc2 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/LinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/LinkHandlerFactory.java @@ -1,6 +1,5 @@ package org.schabi.newpipe.extractor.linkhandler; -import org.schabi.newpipe.extractor.exceptions.FoundAdException; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.utils.Utils; @@ -31,10 +30,12 @@ public abstract class LinkHandlerFactory { /////////////////////////////////// public abstract String getId(String url) throws ParsingException; + public abstract String getUrl(String id) throws ParsingException; - public abstract boolean onAcceptUrl(final String url) throws ParsingException; - public String getUrl(String id, String baseUrl) throws ParsingException { + public abstract boolean onAcceptUrl(String url) throws ParsingException; + + public String getUrl(final String id, final String baseUrl) throws ParsingException { return getUrl(id); } @@ -46,6 +47,7 @@ public String getUrl(String id, String baseUrl) throws ParsingException { * Builds a {@link LinkHandler} from a url.
* Be sure to call {@link Utils#followGoogleRedirectIfNeeded(String)} on the url if overriding * this function. + * * @param url the url to extract path and id from * @return a {@link LinkHandler} complete with information */ @@ -64,12 +66,15 @@ public LinkHandler fromUrl(final String url) throws ParsingException { * extracted?).
* So do not call {@link Utils#followGoogleRedirectIfNeeded(String)} on the URL if overriding * this function, since that should be done in {@link #fromUrl(String)}. - * @param url the URL without Google search redirects to extract id from + * + * @param url the URL without Google search redirects to extract id from * @param baseUrl the base URL * @return a {@link LinkHandler} complete with information */ - public LinkHandler fromUrl(String url, String baseUrl) throws ParsingException { - if (url == null) throw new IllegalArgumentException("URL cannot be null"); + public LinkHandler fromUrl(final String url, final String baseUrl) throws ParsingException { + if (url == null) { + throw new IllegalArgumentException("URL cannot be null"); + } if (!acceptUrl(url)) { throw new ParsingException("URL not accepted: " + url); } @@ -78,14 +83,18 @@ public LinkHandler fromUrl(String url, String baseUrl) throws ParsingException { return new LinkHandler(url, getUrl(id, baseUrl), id); } - public LinkHandler fromId(String id) throws ParsingException { - if (id == null) throw new IllegalArgumentException("id can not be null"); + public LinkHandler fromId(final String id) throws ParsingException { + if (id == null) { + throw new IllegalArgumentException("id can not be null"); + } final String url = getUrl(id); return new LinkHandler(url, url, id); } - public LinkHandler fromId(String id, String baseUrl) throws ParsingException { - if (id == null) throw new IllegalArgumentException("id can not be null"); + public LinkHandler fromId(final String id, final String baseUrl) throws ParsingException { + if (id == null) { + throw new IllegalArgumentException("id can not be null"); + } final String url = getUrl(id, baseUrl); return new LinkHandler(url, url, id); } @@ -96,11 +105,6 @@ public LinkHandler fromId(String id, String baseUrl) throws ParsingException { * Return false if this service shall not allow to be called through ACTIONs. */ public boolean acceptUrl(final String url) throws ParsingException { - try { - return onAcceptUrl(url); - } catch (FoundAdException fe) { - throw fe; - } + return onAcceptUrl(url); } - } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/ListLinkHandler.java b/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/ListLinkHandler.java index 3e85fe1bda..17134e4e25 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/ListLinkHandler.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/ListLinkHandler.java @@ -7,17 +7,17 @@ public class ListLinkHandler extends LinkHandler { protected final List contentFilters; protected final String sortFilter; - public ListLinkHandler(String originalUrl, - String url, - String id, - List contentFilters, - String sortFilter) { + public ListLinkHandler(final String originalUrl, + final String url, + final String id, + final List contentFilters, + final String sortFilter) { super(originalUrl, url, id); this.contentFilters = Collections.unmodifiableList(contentFilters); this.sortFilter = sortFilter; } - public ListLinkHandler(ListLinkHandler handler) { + public ListLinkHandler(final ListLinkHandler handler) { this(handler.originalUrl, handler.url, handler.id, @@ -25,9 +25,9 @@ public ListLinkHandler(ListLinkHandler handler) { handler.sortFilter); } - public ListLinkHandler(LinkHandler handler, - List contentFilters, - String sortFilter) { + public ListLinkHandler(final LinkHandler handler, + final List contentFilters, + final String sortFilter) { this(handler.originalUrl, handler.url, handler.id, diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/ListLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/ListLinkHandlerFactory.java index 11b94c7a55..2e38678285 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/ListLinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/ListLinkHandlerFactory.java @@ -13,17 +13,23 @@ public abstract class ListLinkHandlerFactory extends LinkHandlerFactory { // To Override /////////////////////////////////// - public List getContentFilter(String url) throws ParsingException { + @SuppressWarnings({"RedundantThrows", "unused"}) + public List getContentFilter(final String url) throws ParsingException { return Collections.emptyList(); } - public String getSortFilter(String url) throws ParsingException { + @SuppressWarnings({"RedundantThrows", "unused"}) + public String getSortFilter(final String url) throws ParsingException { return ""; } - public abstract String getUrl(String id, List contentFilter, String sortFilter) throws ParsingException; + public abstract String getUrl(String id, List contentFilter, String sortFilter) + throws ParsingException; - public String getUrl(String id, List contentFilter, String sortFilter, String baseUrl) throws ParsingException { + public String getUrl(final String id, + final List contentFilter, + final String sortFilter, + final String baseUrl) throws ParsingException { return getUrl(id, contentFilter, sortFilter); } @@ -39,55 +45,59 @@ public ListLinkHandler fromUrl(final String url) throws ParsingException { } @Override - public ListLinkHandler fromUrl(String url, String baseUrl) throws ParsingException { - if (url == null) throw new IllegalArgumentException("url may not be null"); + public ListLinkHandler fromUrl(final String url, final String baseUrl) throws ParsingException { + if (url == null) { + throw new IllegalArgumentException("url may not be null"); + } - return new ListLinkHandler(super.fromUrl(url, baseUrl), getContentFilter(url), getSortFilter(url)); + return new ListLinkHandler(super.fromUrl(url, baseUrl), getContentFilter(url), + getSortFilter(url)); } @Override - public ListLinkHandler fromId(String id) throws ParsingException { - return new ListLinkHandler(super.fromId(id), new ArrayList(0), ""); + public ListLinkHandler fromId(final String id) throws ParsingException { + return new ListLinkHandler(super.fromId(id), new ArrayList<>(0), ""); } @Override - public ListLinkHandler fromId(String id, String baseUrl) throws ParsingException { - return new ListLinkHandler(super.fromId(id, baseUrl), new ArrayList(0), ""); + public ListLinkHandler fromId(final String id, final String baseUrl) throws ParsingException { + return new ListLinkHandler(super.fromId(id, baseUrl), new ArrayList<>(0), ""); } - public ListLinkHandler fromQuery(String id, - List contentFilters, - String sortFilter) throws ParsingException { + public ListLinkHandler fromQuery(final String id, + final List contentFilters, + final String sortFilter) throws ParsingException { final String url = getUrl(id, contentFilters, sortFilter); return new ListLinkHandler(url, url, id, contentFilters, sortFilter); } - public ListLinkHandler fromQuery(String id, - List contentFilters, - String sortFilter, String baseUrl) throws ParsingException { + public ListLinkHandler fromQuery(final String id, + final List contentFilters, + final String sortFilter, + final String baseUrl) throws ParsingException { final String url = getUrl(id, contentFilters, sortFilter, baseUrl); return new ListLinkHandler(url, url, id, contentFilters, sortFilter); } /** - * For making ListLinkHandlerFactory compatible with LinkHandlerFactory we need to override this, - * however it should not be overridden by the actual implementation. + * For making ListLinkHandlerFactory compatible with LinkHandlerFactory we need to override + * this, however it should not be overridden by the actual implementation. * - * @param id * @return the url corresponding to id without any filters applied */ - public String getUrl(String id) throws ParsingException { - return getUrl(id, new ArrayList(0), ""); + public String getUrl(final String id) throws ParsingException { + return getUrl(id, new ArrayList<>(0), ""); } @Override - public String getUrl(String id, String baseUrl) throws ParsingException { - return getUrl(id, new ArrayList(0), "", baseUrl); + public String getUrl(final String id, final String baseUrl) throws ParsingException { + return getUrl(id, new ArrayList<>(0), "", baseUrl); } /** - * Will returns content filter the corresponding extractor can handle like "channels", "videos", "music", etc. + * Will returns content filter the corresponding extractor can handle like "channels", "videos", + * "music", etc. * * @return filter that can be applied when building a query for getting a list */ @@ -96,7 +106,8 @@ public String[] getAvailableContentFilter() { } /** - * Will returns sort filter the corresponding extractor can handle like "A-Z", "oldest first", "size", etc. + * Will returns sort filter the corresponding extractor can handle like "A-Z", "oldest first", + * "size", etc. * * @return filter that can be applied when building a query for getting a list */ diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/SearchQueryHandler.java b/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/SearchQueryHandler.java index 67dd7e49df..010d977d04 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/SearchQueryHandler.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/SearchQueryHandler.java @@ -4,15 +4,15 @@ public class SearchQueryHandler extends ListLinkHandler { - public SearchQueryHandler(String originalUrl, - String url, - String searchString, - List contentFilters, - String sortFilter) { + public SearchQueryHandler(final String originalUrl, + final String url, + final String searchString, + final List contentFilters, + final String sortFilter) { super(originalUrl, url, searchString, contentFilters, sortFilter); } - public SearchQueryHandler(ListLinkHandler handler) { + public SearchQueryHandler(final ListLinkHandler handler) { this(handler.originalUrl, handler.url, handler.id, diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/SearchQueryHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/SearchQueryHandlerFactory.java index 3b891b305c..07e7a5dcac 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/SearchQueryHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/SearchQueryHandlerFactory.java @@ -14,9 +14,11 @@ public abstract class SearchQueryHandlerFactory extends ListLinkHandlerFactory { /////////////////////////////////// @Override - public abstract String getUrl(String query, List contentFilter, String sortFilter) throws ParsingException; + public abstract String getUrl(String query, List contentFilter, String sortFilter) + throws ParsingException; - public String getSearchString(String url) { + @SuppressWarnings("unused") + public String getSearchString(final String url) { return ""; } @@ -25,28 +27,26 @@ public String getSearchString(String url) { /////////////////////////////////// @Override - public String getId(String url) { + public String getId(final String url) { return getSearchString(url); } @Override - public SearchQueryHandler fromQuery(String query, - List contentFilter, - String sortFilter) throws ParsingException { + public SearchQueryHandler fromQuery(final String query, + final List contentFilter, + final String sortFilter) throws ParsingException { return new SearchQueryHandler(super.fromQuery(query, contentFilter, sortFilter)); } - public SearchQueryHandler fromQuery(String query) throws ParsingException { + public SearchQueryHandler fromQuery(final String query) throws ParsingException { return fromQuery(query, new ArrayList<>(0), EMPTY_STRING); } /** * It's not mandatory for NewPipe to handle the Url - * - * @param url */ @Override - public boolean onAcceptUrl(String url) { + public boolean onAcceptUrl(final String url) { return false; } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/ContentCountry.java b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/ContentCountry.java index 2398d9934d..89baf11ea2 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/ContentCountry.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/ContentCountry.java @@ -1,6 +1,7 @@ package org.schabi.newpipe.extractor.localization; import javax.annotation.Nonnull; + import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; @@ -9,23 +10,26 @@ /** * Represents a country that should be used when fetching content. *

- * YouTube, for example, give different results in their feed depending on which country is selected. + * YouTube, for example, give different results in their feed depending on which country is + * selected. *

*/ public class ContentCountry implements Serializable { - public static final ContentCountry DEFAULT = new ContentCountry(Localization.DEFAULT.getCountryCode()); + public static final ContentCountry DEFAULT = + new ContentCountry(Localization.DEFAULT.getCountryCode()); - @Nonnull private final String countryCode; + @Nonnull + private final String countryCode; - public static List listFrom(String... countryCodeList) { + public static List listFrom(final String... countryCodeList) { final List toReturn = new ArrayList<>(); - for (String countryCode : countryCodeList) { + for (final String countryCode : countryCodeList) { toReturn.add(new ContentCountry(countryCode)); } return Collections.unmodifiableList(toReturn); } - public ContentCountry(@Nonnull String countryCode) { + public ContentCountry(@Nonnull final String countryCode) { this.countryCode = countryCode; } @@ -40,11 +44,15 @@ public String toString() { } @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof ContentCountry)) return false; + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (!(o instanceof ContentCountry)) { + return false; + } - ContentCountry that = (ContentCountry) o; + final ContentCountry that = (ContentCountry) o; return countryCode.equals(that.countryCode); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/DateWrapper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/DateWrapper.java index b1424e3b26..ffc29a61ce 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/DateWrapper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/DateWrapper.java @@ -9,7 +9,8 @@ import java.util.GregorianCalendar; /** - * A wrapper class that provides a field to describe if the date/time is precise or just an approximation. + * A wrapper class that provides a field to describe if the date/time is precise or just an + * approximation. */ public class DateWrapper implements Serializable { @Nonnull @@ -20,7 +21,8 @@ public class DateWrapper implements Serializable { * @deprecated Use {@link #DateWrapper(OffsetDateTime)} instead. */ @Deprecated - public DateWrapper(@Nonnull Calendar calendar) { + public DateWrapper(@Nonnull final Calendar calendar) { + //noinspection deprecation this(calendar, false); } @@ -28,15 +30,16 @@ public DateWrapper(@Nonnull Calendar calendar) { * @deprecated Use {@link #DateWrapper(OffsetDateTime, boolean)} instead. */ @Deprecated - public DateWrapper(@Nonnull Calendar calendar, boolean isApproximation) { + public DateWrapper(@Nonnull final Calendar calendar, final boolean isApproximation) { this(OffsetDateTime.ofInstant(calendar.toInstant(), ZoneOffset.UTC), isApproximation); } - public DateWrapper(@Nonnull OffsetDateTime offsetDateTime) { + public DateWrapper(@Nonnull final OffsetDateTime offsetDateTime) { this(offsetDateTime, false); } - public DateWrapper(@Nonnull OffsetDateTime offsetDateTime, boolean isApproximation) { + public DateWrapper(@Nonnull final OffsetDateTime offsetDateTime, + final boolean isApproximation) { this.offsetDateTime = offsetDateTime.withOffsetSameInstant(ZoneOffset.UTC); this.isApproximation = isApproximation; } @@ -60,8 +63,8 @@ public OffsetDateTime offsetDateTime() { } /** - * @return if the date is considered is precise or just an approximation (e.g. service only returns an approximation - * like 2 weeks ago instead of a precise date). + * @return if the date is considered is precise or just an approximation (e.g. service only + * returns an approximation like 2 weeks ago instead of a precise date). */ public boolean isApproximation() { return isApproximation; diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/Localization.java b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/Localization.java index e0f60a86be..0a28d70b8c 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/Localization.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/Localization.java @@ -4,8 +4,15 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; + import java.io.Serializable; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; public class Localization implements Serializable { public static final Localization DEFAULT = new Localization("en", "GB"); @@ -16,11 +23,12 @@ public class Localization implements Serializable { private final String countryCode; /** - * @param localizationCodeList a list of localization code, formatted like {@link #getLocalizationCode()} + * @param localizationCodeList a list of localization code, formatted like {@link + * #getLocalizationCode()} */ - public static List listFrom(String... localizationCodeList) { + public static List listFrom(final String... localizationCodeList) { final List toReturn = new ArrayList<>(); - for (String localizationCode : localizationCodeList) { + for (final String localizationCode : localizationCodeList) { toReturn.add(fromLocalizationCode(localizationCode)); } return Collections.unmodifiableList(toReturn); @@ -29,10 +37,11 @@ public static List listFrom(String... localizationCodeList) { /** * @param localizationCode a localization code, formatted like {@link #getLocalizationCode()} */ - public static Localization fromLocalizationCode(String localizationCode) { + public static Localization fromLocalizationCode(final String localizationCode) { final int indexSeparator = localizationCode.indexOf("-"); - final String languageCode, countryCode; + final String languageCode; + final String countryCode; if (indexSeparator != -1) { languageCode = localizationCode.substring(0, indexSeparator); countryCode = localizationCode.substring(indexSeparator + 1); @@ -44,15 +53,16 @@ public static Localization fromLocalizationCode(String localizationCode) { return new Localization(languageCode, countryCode); } - public Localization(@Nonnull String languageCode, @Nullable String countryCode) { + public Localization(@Nonnull final String languageCode, @Nullable final String countryCode) { this.languageCode = languageCode; this.countryCode = countryCode; } - public Localization(@Nonnull String languageCode) { + public Localization(@Nonnull final String languageCode) { this(languageCode, null); } + @Nonnull public String getLanguageCode() { return languageCode; } @@ -66,7 +76,7 @@ public Locale asLocale() { return new Locale(getLanguageCode(), getCountryCode()); } - public static Localization fromLocale(@Nonnull Locale locale) { + public static Localization fromLocale(@Nonnull final Locale locale) { return new Localization(locale.getLanguage(), locale.getCountry()); } @@ -84,14 +94,18 @@ public String toString() { } @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Localization)) return false; + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (!(o instanceof Localization)) { + return false; + } - Localization that = (Localization) o; + final Localization that = (Localization) o; - return languageCode.equals(that.languageCode) && - Objects.equals(countryCode, that.countryCode); + return languageCode.equals(that.languageCode) + && Objects.equals(countryCode, that.countryCode); } @Override @@ -108,17 +122,19 @@ public int hashCode() { * @param code a three letter language code * @return the Locale corresponding */ - public static Locale getLocaleFromThreeLetterCode(@Nonnull String code) throws ParsingException { + public static Locale getLocaleFromThreeLetterCode(@Nonnull final String code) + throws ParsingException { final String[] languages = Locale.getISOLanguages(); final Map localeMap = new HashMap<>(languages.length); - for (String language : languages) { + for (final String language : languages) { final Locale locale = new Locale(language); localeMap.put(locale.getISO3Language(), locale); } if (localeMap.containsKey(code)) { return localeMap.get(code); } else { - throw new ParsingException("Could not get Locale from this three letter language code" + code); + throw new ParsingException( + "Could not get Locale from this three letter language code" + code); } } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java index dd74f9aae5..117b5b12f9 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java @@ -25,16 +25,17 @@ public class TimeAgoParser { * Instantiate a new {@link TimeAgoParser} every time you extract a new batch of items. *

* - * @param patternsHolder An object that holds the "time ago" patterns, special cases, and the language word separator. + * @param patternsHolder An object that holds the "time ago" patterns, special cases, and the + * language word separator. */ - public TimeAgoParser(PatternsHolder patternsHolder) { + public TimeAgoParser(final PatternsHolder patternsHolder) { this.patternsHolder = patternsHolder; now = OffsetDateTime.now(ZoneOffset.UTC); } /** - * Parses a textual date in the format '2 days ago' into a Calendar representation which is then wrapped in a - * {@link DateWrapper} object. + * Parses a textual date in the format '2 days ago' into a Calendar representation which is then + * wrapped in a {@link DateWrapper} object. *

* Beginning with days ago, the date is considered as an approximation. * @@ -42,10 +43,12 @@ public TimeAgoParser(PatternsHolder patternsHolder) { * @return The parsed time (can be approximated) * @throws ParsingException if the time unit could not be recognized */ - public DateWrapper parse(String textualDate) throws ParsingException { - for (Map.Entry> caseUnitEntry : patternsHolder.specialCases().entrySet()) { + public DateWrapper parse(final String textualDate) throws ParsingException { + for (final Map.Entry> caseUnitEntry + : patternsHolder.specialCases().entrySet()) { final ChronoUnit chronoUnit = caseUnitEntry.getKey(); - for (Map.Entry caseMapToAmountEntry : caseUnitEntry.getValue().entrySet()) { + for (final Map.Entry caseMapToAmountEntry + : caseUnitEntry.getValue().entrySet()) { final String caseText = caseMapToAmountEntry.getKey(); final Integer caseAmount = caseMapToAmountEntry.getValue(); @@ -58,7 +61,7 @@ public DateWrapper parse(String textualDate) throws ParsingException { int timeAgoAmount; try { timeAgoAmount = parseTimeAgoAmount(textualDate); - } catch (NumberFormatException e) { + } catch (final NumberFormatException e) { // If there is no valid number in the textual date, // assume it is 1 (as in 'a second ago'). timeAgoAmount = 1; @@ -68,16 +71,16 @@ public DateWrapper parse(String textualDate) throws ParsingException { return getResultFor(timeAgoAmount, chronoUnit); } - private int parseTimeAgoAmount(String textualDate) throws NumberFormatException { - String timeValueStr = textualDate.replaceAll("\\D+", ""); - return Integer.parseInt(timeValueStr); + private int parseTimeAgoAmount(final String textualDate) throws NumberFormatException { + return Integer.parseInt(textualDate.replaceAll("\\D+", "")); } - private ChronoUnit parseChronoUnit(String textualDate) throws ParsingException { - for (Map.Entry> entry : patternsHolder.asMap().entrySet()) { + private ChronoUnit parseChronoUnit(final String textualDate) throws ParsingException { + for (final Map.Entry> entry + : patternsHolder.asMap().entrySet()) { final ChronoUnit chronoUnit = entry.getKey(); - for (String agoPhrase : entry.getValue()) { + for (final String agoPhrase : entry.getValue()) { if (textualDateMatches(textualDate, agoPhrase)) { return chronoUnit; } @@ -87,7 +90,7 @@ private ChronoUnit parseChronoUnit(String textualDate) throws ParsingException { throw new ParsingException("Unable to parse the date: " + textualDate); } - private boolean textualDateMatches(String textualDate, String agoPhrase) { + private boolean textualDateMatches(final String textualDate, final String agoPhrase) { if (textualDate.equals(agoPhrase)) { return true; } @@ -98,7 +101,8 @@ private boolean textualDateMatches(String textualDate, String agoPhrase) { final String escapedPhrase = Pattern.quote(agoPhrase.toLowerCase()); final String escapedSeparator; if (patternsHolder.wordSeparator().equals(" ")) { - // From JDK8 → \h - Treat horizontal spaces as a normal one (non-breaking space, thin space, etc.) + // From JDK8 → \h - Treat horizontal spaces as a normal one + // (non-breaking space, thin space, etc.) escapedSeparator = "[ \\t\\xA0\\u1680\\u180e\\u2000-\\u200a\\u202f\\u205f\\u3000]"; } else { escapedSeparator = Pattern.quote(patternsHolder.wordSeparator()); @@ -113,7 +117,7 @@ private boolean textualDateMatches(String textualDate, String agoPhrase) { } } - private DateWrapper getResultFor(int timeAgoAmount, ChronoUnit chronoUnit) { + private DateWrapper getResultFor(final int timeAgoAmount, final ChronoUnit chronoUnit) { OffsetDateTime offsetDateTime = now; boolean isApproximation = false; diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoPatternsManager.java b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoPatternsManager.java index 1b41addeb3..19b697661c 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoPatternsManager.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoPatternsManager.java @@ -6,14 +6,18 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; -public class TimeAgoPatternsManager { +public final class TimeAgoPatternsManager { + private TimeAgoPatternsManager() { + } + @Nullable - private static PatternsHolder getPatternsFor(@Nonnull Localization localization) { - return PatternsManager.getPatterns(localization.getLanguageCode(), localization.getCountryCode()); + private static PatternsHolder getPatternsFor(@Nonnull final Localization localization) { + return PatternsManager.getPatterns(localization.getLanguageCode(), + localization.getCountryCode()); } @Nullable - public static TimeAgoParser getTimeAgoParserFor(@Nonnull Localization localization) { + public static TimeAgoParser getTimeAgoParserFor(@Nonnull final Localization localization) { final PatternsHolder holder = getPatternsFor(localization); if (holder == null) { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfo.java index 20627e2a1e..aea33304c9 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfo.java @@ -15,7 +15,7 @@ import java.util.ArrayList; import java.util.List; -public class PlaylistInfo extends ListInfo { +public final class PlaylistInfo extends ListInfo { /** * Mixes are handled as particular playlists in NewPipeExtractor. {@link PlaylistType#NORMAL} is @@ -52,23 +52,27 @@ public enum PlaylistType { MIX_GENRE, } - private PlaylistInfo(int serviceId, ListLinkHandler linkHandler, String name) throws ParsingException { + @SuppressWarnings("RedundantThrows") + private PlaylistInfo(final int serviceId, final ListLinkHandler linkHandler, final String name) + throws ParsingException { super(serviceId, linkHandler, name); } - public static PlaylistInfo getInfo(String url) throws IOException, ExtractionException { + public static PlaylistInfo getInfo(final String url) throws IOException, ExtractionException { return getInfo(NewPipe.getServiceByUrl(url), url); } - public static PlaylistInfo getInfo(StreamingService service, String url) throws IOException, ExtractionException { - PlaylistExtractor extractor = service.getPlaylistExtractor(url); + public static PlaylistInfo getInfo(final StreamingService service, final String url) + throws IOException, ExtractionException { + final PlaylistExtractor extractor = service.getPlaylistExtractor(url); extractor.fetchPage(); return getInfo(extractor); } - public static InfoItemsPage getMoreItems(StreamingService service, - String url, - Page page) throws IOException, ExtractionException { + public static InfoItemsPage getMoreItems(final StreamingService service, + final String url, + final Page page) + throws IOException, ExtractionException { return service.getPlaylistExtractor(url).getPage(page); } @@ -77,7 +81,8 @@ public static InfoItemsPage getMoreItems(StreamingService servic * * @param extractor an extractor where fetchPage() was already got called on. */ - public static PlaylistInfo getInfo(PlaylistExtractor extractor) throws ExtractionException { + public static PlaylistInfo getInfo(final PlaylistExtractor extractor) + throws ExtractionException { final PlaylistInfo info = new PlaylistInfo( extractor.getServiceId(), @@ -85,73 +90,75 @@ public static PlaylistInfo getInfo(PlaylistExtractor extractor) throws Extractio extractor.getName()); // collect uploader extraction failures until we are sure this is not // just a playlist without an uploader - List uploaderParsingErrors = new ArrayList(3); + final List uploaderParsingErrors = new ArrayList<>(); try { info.setOriginalUrl(extractor.getOriginalUrl()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } try { info.setStreamCount(extractor.getStreamCount()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } try { info.setThumbnailUrl(extractor.getThumbnailUrl()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } try { info.setUploaderUrl(extractor.getUploaderUrl()); - } catch (Exception e) { + } catch (final Exception e) { info.setUploaderUrl(""); uploaderParsingErrors.add(e); } try { info.setUploaderName(extractor.getUploaderName()); - } catch (Exception e) { + } catch (final Exception e) { info.setUploaderName(""); uploaderParsingErrors.add(e); } try { info.setUploaderAvatarUrl(extractor.getUploaderAvatarUrl()); - } catch (Exception e) { + } catch (final Exception e) { info.setUploaderAvatarUrl(""); uploaderParsingErrors.add(e); } try { info.setSubChannelUrl(extractor.getSubChannelUrl()); - } catch (Exception e) { + } catch (final Exception e) { uploaderParsingErrors.add(e); } try { info.setSubChannelName(extractor.getSubChannelName()); - } catch (Exception e) { + } catch (final Exception e) { uploaderParsingErrors.add(e); } try { info.setSubChannelAvatarUrl(extractor.getSubChannelAvatarUrl()); - } catch (Exception e) { + } catch (final Exception e) { uploaderParsingErrors.add(e); } try { info.setBannerUrl(extractor.getBannerUrl()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } try { info.setPlaylistType(extractor.getPlaylistType()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } - // do not fail if everything but the uploader infos could be collected - if (!uploaderParsingErrors.isEmpty() && - (!info.getErrors().isEmpty() || uploaderParsingErrors.size() < 3)) { + + // do not fail if everything but the uploader infos could be collected (TODO better comment) + if (!uploaderParsingErrors.isEmpty() + && (!info.getErrors().isEmpty() || uploaderParsingErrors.size() < 3)) { info.addAllErrors(uploaderParsingErrors); } - final InfoItemsPage itemsPage = ExtractorHelper.getItemsPageOrLogError(info, extractor); + final InfoItemsPage itemsPage + = ExtractorHelper.getItemsPageOrLogError(info, extractor); info.setRelatedItems(itemsPage.getItems()); info.setNextPage(itemsPage.getNextPage()); @@ -173,7 +180,7 @@ public String getThumbnailUrl() { return thumbnailUrl; } - public void setThumbnailUrl(String thumbnailUrl) { + public void setThumbnailUrl(final String thumbnailUrl) { this.thumbnailUrl = thumbnailUrl; } @@ -181,7 +188,7 @@ public String getBannerUrl() { return bannerUrl; } - public void setBannerUrl(String bannerUrl) { + public void setBannerUrl(final String bannerUrl) { this.bannerUrl = bannerUrl; } @@ -189,7 +196,7 @@ public String getUploaderUrl() { return uploaderUrl; } - public void setUploaderUrl(String uploaderUrl) { + public void setUploaderUrl(final String uploaderUrl) { this.uploaderUrl = uploaderUrl; } @@ -197,7 +204,7 @@ public String getUploaderName() { return uploaderName; } - public void setUploaderName(String uploaderName) { + public void setUploaderName(final String uploaderName) { this.uploaderName = uploaderName; } @@ -205,7 +212,7 @@ public String getUploaderAvatarUrl() { return uploaderAvatarUrl; } - public void setUploaderAvatarUrl(String uploaderAvatarUrl) { + public void setUploaderAvatarUrl(final String uploaderAvatarUrl) { this.uploaderAvatarUrl = uploaderAvatarUrl; } @@ -213,7 +220,7 @@ public String getSubChannelUrl() { return subChannelUrl; } - public void setSubChannelUrl(String subChannelUrl) { + public void setSubChannelUrl(final String subChannelUrl) { this.subChannelUrl = subChannelUrl; } @@ -221,7 +228,7 @@ public String getSubChannelName() { return subChannelName; } - public void setSubChannelName(String subChannelName) { + public void setSubChannelName(final String subChannelName) { this.subChannelName = subChannelName; } @@ -229,7 +236,7 @@ public String getSubChannelAvatarUrl() { return subChannelAvatarUrl; } - public void setSubChannelAvatarUrl(String subChannelAvatarUrl) { + public void setSubChannelAvatarUrl(final String subChannelAvatarUrl) { this.subChannelAvatarUrl = subChannelAvatarUrl; } @@ -237,7 +244,7 @@ public long getStreamCount() { return streamCount; } - public void setStreamCount(long streamCount) { + public void setStreamCount(final long streamCount) { this.streamCount = streamCount; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfoItem.java b/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfoItem.java index 218beea4a6..92ade89b21 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfoItem.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfoItem.java @@ -11,7 +11,7 @@ public class PlaylistInfoItem extends InfoItem { private long streamCount = 0; private PlaylistInfo.PlaylistType playlistType; - public PlaylistInfoItem(int serviceId, String url, String name) { + public PlaylistInfoItem(final int serviceId, final String url, final String name) { super(InfoType.PLAYLIST, serviceId, url, name); } @@ -19,16 +19,16 @@ public String getUploaderName() { return uploaderName; } - public void setUploaderName(String uploader_name) { - this.uploaderName = uploader_name; + public void setUploaderName(final String uploaderName) { + this.uploaderName = uploaderName; } public long getStreamCount() { return streamCount; } - public void setStreamCount(long stream_count) { - this.streamCount = stream_count; + public void setStreamCount(final long streamCount) { + this.streamCount = streamCount; } public PlaylistInfo.PlaylistType getPlaylistType() { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfoItemExtractor.java index 4cfa472082..be9b10262b 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfoItemExtractor.java @@ -10,14 +10,12 @@ public interface PlaylistInfoItemExtractor extends InfoItemExtractor { /** * Get the uploader name * @return the uploader name - * @throws ParsingException */ String getUploaderName() throws ParsingException; /** * Get the number of streams * @return the number of streams - * @throws ParsingException */ long getStreamCount() throws ParsingException; diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfoItemsCollector.java b/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfoItemsCollector.java index a525f423a9..87e0d16f14 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfoItemsCollector.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfoItemsCollector.java @@ -3,34 +3,32 @@ import org.schabi.newpipe.extractor.InfoItemsCollector; import org.schabi.newpipe.extractor.exceptions.ParsingException; -public class PlaylistInfoItemsCollector extends InfoItemsCollector { +public class PlaylistInfoItemsCollector + extends InfoItemsCollector { - public PlaylistInfoItemsCollector(int serviceId) { + public PlaylistInfoItemsCollector(final int serviceId) { super(serviceId); } @Override - public PlaylistInfoItem extract(PlaylistInfoItemExtractor extractor) throws ParsingException { - - String name = extractor.getName(); - int serviceId = getServiceId(); - String url = extractor.getUrl(); - - PlaylistInfoItem resultItem = new PlaylistInfoItem(serviceId, url, name); + public PlaylistInfoItem extract(final PlaylistInfoItemExtractor extractor) + throws ParsingException { + final PlaylistInfoItem resultItem = new PlaylistInfoItem( + getServiceId(), extractor.getUrl(), extractor.getName()); try { resultItem.setUploaderName(extractor.getUploaderName()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setThumbnailUrl(extractor.getThumbnailUrl()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setStreamCount(extractor.getStreamCount()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/search/SearchExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/search/SearchExtractor.java index 3539dc8168..601359acf6 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/search/SearchExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/search/SearchExtractor.java @@ -14,12 +14,12 @@ public abstract class SearchExtractor extends ListExtractor { public static class NothingFoundException extends ExtractionException { - public NothingFoundException(String message) { + public NothingFoundException(final String message) { super(message); } } - public SearchExtractor(StreamingService service, SearchQueryHandler linkHandler) { + public SearchExtractor(final StreamingService service, final SearchQueryHandler linkHandler) { super(service, linkHandler); } @@ -34,11 +34,11 @@ public String getSearchString() { * {@link SearchExtractor#isCorrectedSearch()} is true. * * @return a suggestion to another query, the corrected query, or an empty String. - * @throws ParsingException */ @Nonnull public abstract String getSearchSuggestion() throws ParsingException; + @Nonnull @Override public SearchQueryHandler getLinkHandler() { return (SearchQueryHandler) super.getLinkHandler(); @@ -66,8 +66,7 @@ public String getName() { * Example: on YouTube, if you search for "Covid-19", * there is a box with information from the WHO about Covid-19 and a link to the WHO's website. * @return additional meta information about the search query - * @throws ParsingException */ - @Nonnull + @Nonnull public abstract List getMetaInfo() throws ParsingException; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/search/SearchInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/search/SearchInfo.java index d1eb0b3857..ee87446e95 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/search/SearchInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/search/SearchInfo.java @@ -1,6 +1,11 @@ package org.schabi.newpipe.extractor.search; -import org.schabi.newpipe.extractor.*; +import org.schabi.newpipe.extractor.InfoItem; +import org.schabi.newpipe.extractor.ListExtractor; +import org.schabi.newpipe.extractor.ListInfo; +import org.schabi.newpipe.extractor.MetaInfo; +import org.schabi.newpipe.extractor.Page; +import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler; import org.schabi.newpipe.extractor.utils.ExtractorHelper; @@ -11,26 +16,29 @@ import javax.annotation.Nonnull; public class SearchInfo extends ListInfo { - private String searchString; + private final String searchString; private String searchSuggestion; private boolean isCorrectedSearch; private List metaInfo; - public SearchInfo(int serviceId, - SearchQueryHandler qIHandler, - String searchString) { + public SearchInfo(final int serviceId, + final SearchQueryHandler qIHandler, + final String searchString) { super(serviceId, qIHandler, "Search"); this.searchString = searchString; } - public static SearchInfo getInfo(StreamingService service, SearchQueryHandler searchQuery) throws ExtractionException, IOException { - SearchExtractor extractor = service.getSearchExtractor(searchQuery); + public static SearchInfo getInfo(final StreamingService service, + final SearchQueryHandler searchQuery) + throws ExtractionException, IOException { + final SearchExtractor extractor = service.getSearchExtractor(searchQuery); extractor.fetchPage(); return getInfo(extractor); } - public static SearchInfo getInfo(SearchExtractor extractor) throws ExtractionException, IOException { + public static SearchInfo getInfo(final SearchExtractor extractor) + throws ExtractionException, IOException { final SearchInfo info = new SearchInfo( extractor.getServiceId(), extractor.getLinkHandler(), @@ -38,26 +46,27 @@ public static SearchInfo getInfo(SearchExtractor extractor) throws ExtractionExc try { info.setOriginalUrl(extractor.getOriginalUrl()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } try { info.setSearchSuggestion(extractor.getSearchSuggestion()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } try { info.setIsCorrectedSearch(extractor.isCorrectedSearch()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } try { info.setMetaInfo(extractor.getMetaInfo()); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); } - ListExtractor.InfoItemsPage page = ExtractorHelper.getItemsPageOrLogError(info, extractor); + final ListExtractor.InfoItemsPage page + = ExtractorHelper.getItemsPageOrLogError(info, extractor); info.setRelatedItems(page.getItems()); info.setNextPage(page.getNextPage()); @@ -65,9 +74,9 @@ public static SearchInfo getInfo(SearchExtractor extractor) throws ExtractionExc } - public static ListExtractor.InfoItemsPage getMoreItems(StreamingService service, - SearchQueryHandler query, - Page page) + public static ListExtractor.InfoItemsPage getMoreItems(final StreamingService service, + final SearchQueryHandler query, + final Page page) throws IOException, ExtractionException { return service.getSearchExtractor(query).getPage(page); } @@ -85,11 +94,11 @@ public boolean isCorrectedSearch() { return this.isCorrectedSearch; } - public void setIsCorrectedSearch(boolean isCorrectedSearch) { + public void setIsCorrectedSearch(final boolean isCorrectedSearch) { this.isCorrectedSearch = isCorrectedSearch; } - public void setSearchSuggestion(String searchSuggestion) { + public void setSearchSuggestion(final String searchSuggestion) { this.searchSuggestion = searchSuggestion; } @@ -98,7 +107,7 @@ public List getMetaInfo() { return metaInfo; } - public void setMetaInfo(@Nonnull List metaInfo) { + public void setMetaInfo(@Nonnull final List metaInfo) { this.metaInfo = metaInfo; } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamExtractor.java index 4902bff30e..c5d434732b 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamExtractor.java @@ -74,7 +74,7 @@ public Description getDescription() throws ParsingException { try { text = JsonUtils.getString(json, "description"); } catch (ParsingException e) { - return Description.emptyDescription; + return Description.EMPTY_DESCRIPTION; } if (text.length() == 250 && text.substring(247).equals("...")) { //if description is shortened, get full description diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/AudioStream.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/AudioStream.java index 94321643c7..c1cf2e0e1b 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/AudioStream.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/AudioStream.java @@ -24,7 +24,7 @@ import org.schabi.newpipe.extractor.services.youtube.ItagItem; public class AudioStream extends Stream { - public int average_bitrate = -1; + private final int averageBitrate; // Fields for Dash private int itag; @@ -42,9 +42,11 @@ public class AudioStream extends Stream { * @param format the format * @param averageBitrate the average bitrate */ - public AudioStream(String url, MediaFormat format, int averageBitrate) { + public AudioStream(final String url, + final MediaFormat format, + final int averageBitrate) { super(url, format); - this.average_bitrate = averageBitrate; + this.averageBitrate = averageBitrate; } /** @@ -52,7 +54,7 @@ public AudioStream(String url, MediaFormat format, int averageBitrate) { * @param url the url * @param itag the ItagItem of the Stream */ - public AudioStream(String url, ItagItem itag) { + public AudioStream(final String url, final ItagItem itag) { this(url, itag.getMediaFormat(), itag.avgBitrate); this.itag = itag.id; this.quality = itag.getQuality(); @@ -65,9 +67,9 @@ public AudioStream(String url, ItagItem itag) { } @Override - public boolean equalStats(Stream cmp) { - return super.equalStats(cmp) && cmp instanceof AudioStream && - average_bitrate == ((AudioStream) cmp).average_bitrate; + public boolean equalStats(final Stream cmp) { + return super.equalStats(cmp) && cmp instanceof AudioStream + && averageBitrate == ((AudioStream) cmp).averageBitrate; } /** @@ -75,7 +77,7 @@ public boolean equalStats(Stream cmp) { * @return the average bitrate or -1 */ public int getAverageBitrate() { - return average_bitrate; + return averageBitrate; } public int getItag() { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Description.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Description.java index b92839e25d..5a6f94b7ff 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Description.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Description.java @@ -10,12 +10,12 @@ public class Description implements Serializable { public static final int HTML = 1; public static final int MARKDOWN = 2; public static final int PLAIN_TEXT = 3; - public static final Description emptyDescription = new Description(EMPTY_STRING, PLAIN_TEXT); + public static final Description EMPTY_DESCRIPTION = new Description(EMPTY_STRING, PLAIN_TEXT); - private String content; - private int type; + private final String content; + private final int type; - public Description(String content, int type) { + public Description(final String content, final int type) { this.type = type; if (content == null) { this.content = ""; @@ -33,10 +33,14 @@ public int getType() { } @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Description that = (Description) o; + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + final Description that = (Description) o; return type == that.type && Objects.equals(content, that.content); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Frameset.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Frameset.java index d07a0b7b93..22343b0cac 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Frameset.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Frameset.java @@ -21,7 +21,7 @@ public Frameset( final int durationPerFrame, final int framesPerPageX, final int framesPerPageY) { - + this.urls = urls; this.totalCount = totalCount; this.durationPerFrame = durationPerFrame; @@ -84,8 +84,8 @@ public int getDurationPerFrame() { * Returns the information for the frame at stream position. * * @param position Position in milliseconds - * @return An int-array containing the bounds and URL where the indexes are specified as - * followed: + * @return An int-array containing the bounds and URL where the indexes are + * specified as follows: * *

    *
  • 0: Index of the URL
  • @@ -96,9 +96,9 @@ public int getDurationPerFrame() { *
*/ public int[] getFrameBoundsAt(final long position) { - if (position < 0 || position > ((totalCount + 1) * durationPerFrame)) { + if (position < 0 || position > ((long) (totalCount + 1) * durationPerFrame)) { // Return the first frame as fallback - return new int[] { 0, 0, 0, frameWidth, frameHeight }; + return new int[] {0, 0, 0, frameWidth, frameHeight}; } final int framesPerStoryboard = framesPerPageX * framesPerPageY; @@ -116,4 +116,4 @@ public int[] getFrameBoundsAt(final long position) { /* right */ columnIndex * frameWidth + frameWidth, /* bottom */ rowIndex * frameHeight + frameHeight }; } -} \ No newline at end of file +} 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 7e99081678..d87a221440 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 @@ -27,7 +27,7 @@ public abstract class Stream implements Serializable { * @param url the url * @param format the format */ - public Stream(String url, MediaFormat format) { + public Stream(final String url, final MediaFormat format) { this(url, null, format); } @@ -35,12 +35,14 @@ public Stream(String url, MediaFormat format) { * Instantiates a new stream object. * * @param url the url - * @param torrentUrl the url to torrent file, example https://webtorrent.io/torrents/big-buck-bunny.torrent + * @param torrentUrl the url to torrent file, example + * https://webtorrent.io/torrents/big-buck-bunny.torrent * @param format the format */ - public Stream(String url, String torrentUrl, MediaFormat format) { + public Stream(final String url, final String torrentUrl, final MediaFormat format) { this.url = url; this.torrentUrl = torrentUrl; + //noinspection deprecation this.format = format.id; this.mediaFormat = format; } @@ -48,24 +50,29 @@ public Stream(String url, String torrentUrl, MediaFormat format) { /** * Reveals whether two streams have the same stats (format and bitrate, for example) */ - public boolean equalStats(Stream cmp) { + public boolean equalStats(final Stream cmp) { return cmp != null && getFormatId() == cmp.getFormatId(); } /** * Reveals whether two Streams are equal */ - public boolean equals(Stream cmp) { + public boolean equals(final Stream cmp) { return equalStats(cmp) && url.equals(cmp.url); } /** * Check if the list already contains one stream with equals stats */ - public static boolean containSimilarStream(Stream stream, List streamList) { - if (isNullOrEmpty(streamList)) return false; - for (Stream cmpStream : streamList) { - if (stream.equalStats(cmpStream)) return true; + public static boolean containSimilarStream(final Stream stream, + final List streamList) { + if (isNullOrEmpty(streamList)) { + return false; + } + for (final Stream cmpStream : streamList) { + if (stream.equalStats(cmpStream)) { + return true; + } } return false; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java index 939b4f5a5c..ab922b1c98 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java @@ -36,6 +36,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; + import java.io.IOException; import java.util.Collections; import java.util.List; @@ -49,7 +50,7 @@ public abstract class StreamExtractor extends Extractor { public static final int NO_AGE_LIMIT = 0; public static final long UNKNOWN_SUBSCRIBER_COUNT = -1; - public StreamExtractor(StreamingService service, LinkHandler linkHandler) { + public StreamExtractor(final StreamingService service, final LinkHandler linkHandler) { super(service, linkHandler); } @@ -70,7 +71,8 @@ public String getTextualUploadDate() throws ParsingException { /** * A more general {@code Calendar} instance set to the date provided by the service.
- * Implementations usually will just parse the date returned from the {@link #getTextualUploadDate()}. + * Implementations usually will just parse the date returned from the {@link + * #getTextualUploadDate()}. * *

If the stream is a live stream, {@code null} should be returned.

* @@ -85,10 +87,10 @@ public DateWrapper getUploadDate() throws ParsingException { } /** - * This will return the url to the thumbnail of the stream. Try to return the medium resolution here. + * This will return the url to the thumbnail of the stream. Try to return the medium resolution + * here. * * @return The url of the thumbnail. - * @throws ParsingException */ @Nonnull public abstract String getThumbnailUrl() throws ParsingException; @@ -96,12 +98,12 @@ public DateWrapper getUploadDate() throws ParsingException { /** * This is the stream description. * - * @return The description of the stream/video or Description.emptyDescription if the description is empty. - * @throws ParsingException + * @return The description of the stream/video or {@link Description#EMPTY_DESCRIPTION} if the + * description is empty. */ @Nonnull public Description getDescription() throws ParsingException { - return Description.emptyDescription; + return Description.EMPTY_DESCRIPTION; } /** @@ -118,7 +120,6 @@ public int getAgeLimit() throws ParsingException { * This should return the length of a video in seconds. * * @return The length of the stream in seconds or 0 when it has no length (e.g. a livestream). - * @throws ParsingException */ public long getLength() throws ParsingException { return 0; @@ -130,7 +131,6 @@ public long getLength() throws ParsingException { * If the url has no time stamp simply return zero. * * @return the timestamp in seconds or 0 when there is no timestamp - * @throws ParsingException */ public long getTimeStamp() throws ParsingException { return 0; @@ -141,7 +141,6 @@ public long getTimeStamp() throws ParsingException { * If the current stream has no view count or its not available simply return -1 * * @return amount of views or -1 if not available. - * @throws ParsingException */ public long getViewCount() throws ParsingException { return -1; @@ -152,7 +151,6 @@ public long getViewCount() throws ParsingException { * If the current stream has no likes or its not available simply return -1 * * @return the amount of likes the stream got or -1 if not available. - * @throws ParsingException */ public long getLikeCount() throws ParsingException { return -1; @@ -163,7 +161,6 @@ public long getLikeCount() throws ParsingException { * If the current stream has no dislikes or its not available simply return -1 * * @return the amount of likes the stream got or -1 if not available. - * @throws ParsingException */ public long getDislikeCount() throws ParsingException { return -1; @@ -172,12 +169,10 @@ public long getDislikeCount() throws ParsingException { /** * The Url to the page of the creator/uploader of the stream. This must not be a homepage, * but the page offered by the service the extractor handles. This url will be handled by the - * {@link ChannelExtractor}, - * so be sure to implement that one before you return a value here, otherwise NewPipe will crash if one selects - * this url. + * {@link ChannelExtractor}, so be sure to implement that one before you return a value here, + * otherwise NewPipe will crash if one selects this url. * * @return the url to the page of the creator/uploader of the stream or an empty string - * @throws ParsingException */ @Nonnull public abstract String getUploaderUrl() throws ParsingException; @@ -187,7 +182,6 @@ public long getDislikeCount() throws ParsingException { * If the name is not available you can simply return an empty string. * * @return the name of the creator/uploader of the stream or an empty tring - * @throws ParsingException */ @Nonnull public abstract String getUploaderName() throws ParsingException; @@ -197,7 +191,6 @@ public long getDislikeCount() throws ParsingException { * If there is no verification implemented, return false. * * @return whether the uploader has been verified by the service's provider - * @throws ParsingException */ public boolean isUploaderVerified() throws ParsingException { return false; @@ -207,8 +200,8 @@ public boolean isUploaderVerified() throws ParsingException { * The subscriber count of the uploader. * If the subscriber count is not implemented, or is unavailable, return -1. * - * @return the subscriber count of the uploader or {@value UNKNOWN_SUBSCRIBER_COUNT} if not available - * @throws ParsingException + * @return the subscriber count of the uploader or {@value UNKNOWN_SUBSCRIBER_COUNT} if not + * available */ public long getUploaderSubscriberCount() throws ParsingException { return UNKNOWN_SUBSCRIBER_COUNT; @@ -219,7 +212,6 @@ public long getUploaderSubscriberCount() throws ParsingException { * If the url is not available you can return an empty String. * * @return The url of the image file of the uploader or an empty String - * @throws ParsingException */ @Nonnull public String getUploaderAvatarUrl() throws ParsingException { @@ -229,12 +221,10 @@ public String getUploaderAvatarUrl() throws ParsingException { /** * The Url to the page of the sub-channel of the stream. This must not be a homepage, * but the page offered by the service the extractor handles. This url will be handled by the - * {@link ChannelExtractor}, - * so be sure to implement that one before you return a value here, otherwise NewPipe will crash if one selects - * this url. + * {@link ChannelExtractor}, so be sure to implement that one before you return a value here, + * otherwise NewPipe will crash if one selects this url. * * @return the url to the page of the sub-channel of the stream or an empty String - * @throws ParsingException */ @Nonnull public String getSubChannelUrl() throws ParsingException { @@ -246,7 +236,6 @@ public String getSubChannelUrl() throws ParsingException { * If the name is not available you can simply return an empty string. * * @return the name of the sub-channel of the stream or an empty String - * @throws ParsingException */ @Nonnull public String getSubChannelName() throws ParsingException { @@ -258,7 +247,6 @@ public String getSubChannelName() throws ParsingException { * If the url is not available you can return an empty String. * * @return The url of the image file of the sub-channel or an empty String - * @throws ParsingException */ @Nonnull public String getSubChannelAvatarUrl() throws ParsingException { @@ -278,13 +266,12 @@ public String getDashMpdUrl() throws ParsingException { } /** - * I am not sure if this is in use, and how this is used. However the frontend is missing support - * for HLS streams. Prove me if I am wrong. Please open an + * I am not sure if this is in use, and how this is used. However the frontend is missing + * support for HLS streams. Prove me if I am wrong. Please open an * issue, * or fix this description if you know whats up with this. * * @return The Url to the hls stream or an empty string if not available. - * @throws ParsingException */ @Nonnull public String getHlsUrl() throws ParsingException { @@ -292,54 +279,42 @@ public String getHlsUrl() throws ParsingException { } /** - * This should return a list of available - * AudioStreams + * This should return a list of available {@link AudioStream}s. * You can also return null or an empty list, however be aware that if you don't return anything - * in getVideoStreams(), getVideoOnlyStreams() and getDashMpdUrl() either the Collector will handle this as - * a failed extraction procedure. + * in getVideoStreams(), getVideoOnlyStreams() and getDashMpdUrl() either the Collector will + * handle this as a failed extraction procedure. * * @return a list of audio only streams in the format of AudioStream - * @throws IOException - * @throws ExtractionException */ public abstract List getAudioStreams() throws IOException, ExtractionException; /** - * This should return a list of available - * VideoStreams + * This should return a list of available {@link VideoStream}s. * Be aware this is the list of video streams which do contain an audio stream. * You can also return null or an empty list, however be aware that if you don't return anything - * in getAudioStreams(), getVideoOnlyStreams() and getDashMpdUrl() either the Collector will handle this as - * a failed extraction procedure. + * in getAudioStreams(), getVideoOnlyStreams() and getDashMpdUrl() either the Collector will + * handle this as a failed extraction procedure. * * @return a list of combined video and streams in the format of AudioStream - * @throws IOException - * @throws ExtractionException */ public abstract List getVideoStreams() throws IOException, ExtractionException; /** - * This should return a list of available - * VideoStreams. + * This should return a list of available {@link VideoStream}s. * Be aware this is the list of video streams which do NOT contain an audio stream. * You can also return null or an empty list, however be aware that if you don't return anything - * in getAudioStreams(), getVideoStreams() and getDashMpdUrl() either the Collector will handle this as - * a failed extraction procedure. + * in getAudioStreams(), getVideoStreams() and getDashMpdUrl() either the Collector will handle + * this as a failed extraction procedure. * * @return a list of video and streams in the format of AudioStream - * @throws IOException - * @throws ExtractionException */ public abstract List getVideoOnlyStreams() throws IOException, ExtractionException; /** - * This will return a list of available - * Subtitless. + * This will return a list of available {@link SubtitlesStream}s. * If no subtitles are available an empty list can be returned. * * @return a list of available subtitles or an empty list - * @throws IOException - * @throws ExtractionException */ @Nonnull public List getSubtitlesDefault() throws IOException, ExtractionException { @@ -347,26 +322,22 @@ public List getSubtitlesDefault() throws IOException, Extractio } /** - * This will return a list of available - * Subtitless. - * given by a specific type. + * This will return a list of available {@link SubtitlesStream}s given by a specific type. * If no subtitles in that specific format are available an empty list can be returned. * * @param format the media format by which the subtitles should be filtered * @return a list of available subtitles or an empty list - * @throws IOException - * @throws ExtractionException */ @Nonnull - public List getSubtitles(MediaFormat format) throws IOException, ExtractionException { + public List getSubtitles(final MediaFormat format) + throws IOException, ExtractionException { return Collections.emptyList(); } /** - * Get the StreamType. + * Get the {@link StreamType}. * * @return the type of the stream - * @throws ParsingException */ public abstract StreamType getStreamType() throws ParsingException; @@ -378,8 +349,6 @@ public List getSubtitles(MediaFormat format) throws IOException * If related streams aren't available simply return {@code null}. * * @return a list of InfoItems showing the related videos/streams - * @throws IOException - * @throws ExtractionException */ @Nullable public InfoItemsCollector @@ -388,26 +357,26 @@ public List getSubtitles(MediaFormat format) throws IOException } /** - * @deprecated Use {@link #getRelatedItems()}. May be removed in a future version. * @return The result of {@link #getRelatedItems()} if it is a - * StreamInfoItemsCollector, null otherwise - * @throws IOException - * @throws ExtractionException + * {@link StreamInfoItemsCollector}, null otherwise + * @deprecated Use {@link #getRelatedItems()}. May be removed in a future version. */ @Deprecated @Nullable public StreamInfoItemsCollector getRelatedStreams() throws IOException, ExtractionException { - InfoItemsCollector collector = getRelatedItems(); + final InfoItemsCollector collector = getRelatedItems(); if (collector instanceof StreamInfoItemsCollector) { return (StreamInfoItemsCollector) collector; - } else return null; + } else { + return null; + } } /** * Should return a list of Frameset object that contains preview of stream frames * - * @return list of preview frames or empty list if frames preview is not supported or not found for specified stream - * @throws ExtractionException + * @return list of preview frames or empty list if frames preview is not supported or not found + * for specified stream */ @Nonnull public List getFrames() throws ExtractionException { @@ -428,18 +397,16 @@ public String getErrorMessage() { ////////////////////////////////////////////////////////////////// /** - * Override this function if the format of time stamp in the url is not the same format as that form youtube. - * Honestly I don't even know the time stamp format of YouTube. + * Override this function if the format of timestamp in the url is not the same format as that + * from youtube. * - * @param regexPattern * @return the time stamp/seek for the video in seconds - * @throws ParsingException */ - protected long getTimestampSeconds(String regexPattern) throws ParsingException { - String timeStamp; + protected long getTimestampSeconds(final String regexPattern) throws ParsingException { + final String timestamp; try { - timeStamp = Parser.matchGroup1(regexPattern, getOriginalUrl()); - } catch (Parser.RegexException e) { + timestamp = Parser.matchGroup1(regexPattern, getOriginalUrl()); + } catch (final Parser.RegexException e) { // catch this instantly since a url does not necessarily have a timestamp // -2 because the testing system will consequently know that the regex failed @@ -447,33 +414,29 @@ protected long getTimestampSeconds(String regexPattern) throws ParsingException return -2; } - if (!timeStamp.isEmpty()) { + if (!timestamp.isEmpty()) { try { String secondsString = ""; String minutesString = ""; String hoursString = ""; try { - secondsString = Parser.matchGroup1("(\\d+)s", timeStamp); - minutesString = Parser.matchGroup1("(\\d+)m", timeStamp); - hoursString = Parser.matchGroup1("(\\d+)h", timeStamp); - } catch (Exception e) { - //it could be that time is given in another method - if (secondsString.isEmpty() //if nothing was got, - && minutesString.isEmpty()//treat as unlabelled seconds - && hoursString.isEmpty()) { - secondsString = Parser.matchGroup1("t=(\\d+)", timeStamp); + secondsString = Parser.matchGroup1("(\\d+)s", timestamp); + minutesString = Parser.matchGroup1("(\\d+)m", timestamp); + hoursString = Parser.matchGroup1("(\\d+)h", timestamp); + } catch (final Exception e) { + // it could be that time is given in another method + if (secondsString.isEmpty() && minutesString.isEmpty()) { + // if nothing was obtained, treat as unlabelled seconds + secondsString = Parser.matchGroup1("t=(\\d+)", timestamp); } } - int seconds = secondsString.isEmpty() ? 0 : Integer.parseInt(secondsString); - int minutes = minutesString.isEmpty() ? 0 : Integer.parseInt(minutesString); - int hours = hoursString.isEmpty() ? 0 : Integer.parseInt(hoursString); + final int seconds = secondsString.isEmpty() ? 0 : Integer.parseInt(secondsString); + final int minutes = minutesString.isEmpty() ? 0 : Integer.parseInt(minutesString); + final int hours = hoursString.isEmpty() ? 0 : Integer.parseInt(hoursString); - //don't trust BODMAS! - return seconds + (60 * minutes) + (3600 * hours); - //Log.d(TAG, "derived timestamp value:"+ret); - //the ordering varies internationally - } catch (ParsingException e) { + return seconds + (60L * minutes) + (3600L * hours); + } catch (final ParsingException e) { throw new ParsingException("Could not get timestamp.", e); } } else { @@ -488,7 +451,6 @@ protected long getTimestampSeconds(String regexPattern) throws ParsingException * you can simply return an empty string. * * @return the host of the stream or an empty string. - * @throws ParsingException */ @Nonnull public String getHost() throws ParsingException { @@ -499,7 +461,6 @@ public String getHost() throws ParsingException { * The privacy of the stream (Eg. Public, Private, Unlisted…). * * @return the privacy of the stream. - * @throws ParsingException */ public Privacy getPrivacy() throws ParsingException { return Privacy.PUBLIC; @@ -510,7 +471,6 @@ public Privacy getPrivacy() throws ParsingException { * If the category is not available you can simply return an empty string. * * @return the category of the stream or an empty string. - * @throws ParsingException */ @Nonnull public String getCategory() throws ParsingException { @@ -522,7 +482,6 @@ public String getCategory() throws ParsingException { * If the licence is not available you can simply return an empty string. * * @return the licence of the stream or an empty String. - * @throws ParsingException */ @Nonnull public String getLicence() throws ParsingException { @@ -536,7 +495,6 @@ public String getLicence() throws ParsingException { * new Locale(language_code); * * @return the locale language of the stream or null. - * @throws ParsingException */ @Nullable public Locale getLanguageInfo() throws ParsingException { @@ -548,7 +506,6 @@ public Locale getLanguageInfo() throws ParsingException { * If the tag list is not available you can simply return an empty list. * * @return the list of tags of the stream or Collections.emptyList(). - * @throws ParsingException */ @Nonnull public List getTags() throws ParsingException { @@ -563,7 +520,6 @@ public List getTags() throws ParsingException { * you can simply return an empty String. * * @return the support information of the stream or an empty string. - * @throws ParsingException */ @Nonnull public String getSupportInfo() throws ParsingException { @@ -575,7 +531,6 @@ public String getSupportInfo() throws ParsingException { * If the segment list is not available you can simply return an empty list. * * @return The list of segments of the stream or an empty list. - * @throws ParsingException */ @Nonnull public List getStreamSegments() throws ParsingException { @@ -585,14 +540,14 @@ public List getStreamSegments() throws ParsingException { /** * Meta information about the stream. *

- * This can be information about the stream creator (e.g. if the creator is a public broadcaster) - * or further information on the topic (e.g. hints that the video might contain conspiracy theories - * or contains information about a current health situation like the Covid-19 pandemic). + * This can be information about the stream creator (e.g. if the creator is a public + * broadcaster) or further information on the topic (e.g. hints that the video might contain + * conspiracy theories or contains information about a current health situation like the + * Covid-19 pandemic). *

* The meta information often contains links to external sources like Wikipedia or the WHO. * * @return The meta info of the stream or an empty list if not provided. - * @throws ParsingException */ @Nonnull public List getMetaInfo() throws ParsingException { 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 42ca495cc7..d2caa9c891 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 @@ -1,6 +1,10 @@ package org.schabi.newpipe.extractor.stream; -import org.schabi.newpipe.extractor.*; +import org.schabi.newpipe.extractor.Info; +import org.schabi.newpipe.extractor.InfoItem; +import org.schabi.newpipe.extractor.MetaInfo; +import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException; import org.schabi.newpipe.extractor.exceptions.ContentNotSupportedException; import org.schabi.newpipe.extractor.exceptions.ExtractionException; @@ -44,34 +48,42 @@ public class StreamInfo extends Info { public static class StreamExtractException extends ExtractionException { - StreamExtractException(String message) { + StreamExtractException(final String message) { super(message); } } - public StreamInfo(int serviceId, String url, String originalUrl, StreamType streamType, String id, String name, - int ageLimit) { + public StreamInfo(final int serviceId, + final String url, + final String originalUrl, + final StreamType streamType, + final String id, + final String name, + final int ageLimit) { super(serviceId, id, url, originalUrl, name); this.streamType = streamType; this.ageLimit = ageLimit; } - public static StreamInfo getInfo(String url) throws IOException, ExtractionException { + public static StreamInfo getInfo(final String url) throws IOException, ExtractionException { return getInfo(NewPipe.getServiceByUrl(url), url); } - public static StreamInfo getInfo(StreamingService service, String url) throws IOException, ExtractionException { + public static StreamInfo getInfo(final StreamingService service, + final String url) throws IOException, ExtractionException { return getInfo(service.getStreamExtractor(url)); } - public static StreamInfo getInfo(StreamExtractor extractor) throws ExtractionException, IOException { + public static StreamInfo getInfo(final StreamExtractor extractor) + throws ExtractionException, IOException { extractor.fetchPage(); - StreamInfo streamInfo; try { - streamInfo = extractImportantData(extractor); - streamInfo = extractStreams(streamInfo, extractor); - streamInfo = extractOptionalData(streamInfo, extractor); - } catch (ExtractionException e) { + final StreamInfo streamInfo = extractImportantData(extractor); + extractStreams(streamInfo, extractor); + extractOptionalData(streamInfo, extractor); + return streamInfo; + + } catch (final ExtractionException e) { // Currently YouTube does not distinguish between age restricted videos and // videos blocked // by country. This means that during the initialisation of the extractor, the @@ -88,32 +100,32 @@ public static StreamInfo getInfo(StreamExtractor extractor) throws ExtractionExc throw new ContentNotAvailableException(errorMessage, e); } } - - return streamInfo; } - private static StreamInfo extractImportantData(StreamExtractor extractor) throws ExtractionException { + private static StreamInfo extractImportantData(final StreamExtractor extractor) + throws ExtractionException { /* ---- important data, without the video can't be displayed goes here: ---- */ // if one of these is not available an exception is meant to be thrown directly // into the frontend. - int serviceId = extractor.getServiceId(); - String url = extractor.getUrl(); - String originalUrl = extractor.getOriginalUrl(); - StreamType streamType = extractor.getStreamType(); - String id = extractor.getId(); - String name = extractor.getName(); - int ageLimit = extractor.getAgeLimit(); + final String url = extractor.getUrl(); + final StreamType streamType = extractor.getStreamType(); + final String id = extractor.getId(); + final String name = extractor.getName(); + final int ageLimit = extractor.getAgeLimit(); - if ((streamType == StreamType.NONE) || isNullOrEmpty(url) || (isNullOrEmpty(id)) - || (name == null /* streamInfo.title can be empty of course */) || (ageLimit == -1)) { + // suppress always-non-null warning as here we double-check it really is not null + //noinspection ConstantConditions + if (streamType == StreamType.NONE || isNullOrEmpty(url) || isNullOrEmpty(id) + || name == null /* but it can be empty of course */ || ageLimit == -1) { throw new ExtractionException("Some important stream information was not given."); } - return new StreamInfo(serviceId, url, originalUrl, streamType, id, name, ageLimit); + return new StreamInfo(extractor.getServiceId(), url, extractor.getOriginalUrl(), + streamType, id, name, ageLimit); } - private static StreamInfo extractStreams(StreamInfo streamInfo, StreamExtractor extractor) + private static void extractStreams(final StreamInfo streamInfo, final StreamExtractor extractor) throws ExtractionException { /* ---- stream extraction goes here ---- */ // At least one type of stream has to be available, @@ -121,56 +133,59 @@ private static StreamInfo extractStreams(StreamInfo streamInfo, StreamExtractor try { streamInfo.setDashMpdUrl(extractor.getDashMpdUrl()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(new ExtractionException("Couldn't get Dash manifest", e)); } try { streamInfo.setHlsUrl(extractor.getHlsUrl()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(new ExtractionException("Couldn't get HLS manifest", e)); } /* Load and extract audio */ try { streamInfo.setAudioStreams(extractor.getAudioStreams()); - } catch (ContentNotSupportedException e) { + } catch (final ContentNotSupportedException e) { throw e; - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(new ExtractionException("Couldn't get audio streams", e)); } /* Extract video stream url */ try { streamInfo.setVideoStreams(extractor.getVideoStreams()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(new ExtractionException("Couldn't get video streams", e)); } /* Extract video only stream url */ try { streamInfo.setVideoOnlyStreams(extractor.getVideoOnlyStreams()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(new ExtractionException("Couldn't get video only streams", e)); } // Lists can be null if a exception was thrown during extraction - if (streamInfo.getVideoStreams() == null) + if (streamInfo.getVideoStreams() == null) { streamInfo.setVideoStreams(Collections.emptyList()); - if (streamInfo.getVideoOnlyStreams() == null) + } + if (streamInfo.getVideoOnlyStreams() == null) { streamInfo.setVideoOnlyStreams(Collections.emptyList()); - if (streamInfo.getAudioStreams() == null) + } + if (streamInfo.getAudioStreams() == null) { streamInfo.setAudioStreams(Collections.emptyList()); + } Exception dashMpdError = null; if (!isNullOrEmpty(streamInfo.getDashMpdUrl())) { try { - DashMpdParser.ParserResult result = DashMpdParser.getStreams(streamInfo); + final DashMpdParser.ParserResult result = DashMpdParser.getStreams(streamInfo); streamInfo.getVideoOnlyStreams().addAll(result.getVideoOnlyStreams()); streamInfo.getAudioStreams().addAll(result.getAudioStreams()); streamInfo.getVideoStreams().addAll(result.getVideoStreams()); streamInfo.segmentedVideoOnlyStreams = result.getSegmentedVideoOnlyStreams(); streamInfo.segmentedAudioStreams = result.getSegmentedAudioStreams(); streamInfo.segmentedVideoStreams = result.getSegmentedVideoStreams(); - } catch (Exception e) { + } catch (final Exception e) { // Sometimes we receive 403 (forbidden) error when trying to download the // manifest (similar to what happens with youtube-dl), // just skip the exception (but store it somewhere), as we later check if we @@ -191,13 +206,13 @@ private static StreamInfo extractStreams(StreamInfo streamInfo, StreamExtractor streamInfo.addError(dashMpdError); } - throw new StreamExtractException("Could not get any stream. See error variable to get further details."); + throw new StreamExtractException( + "Could not get any stream. See error variable to get further details."); } - - return streamInfo; } - private static StreamInfo extractOptionalData(StreamInfo streamInfo, StreamExtractor extractor) { + private static void extractOptionalData(final StreamInfo streamInfo, + final StreamExtractor extractor) { /* ---- optional data goes here: ---- */ // If one of these fails, the frontend needs to handle that they are not // available. @@ -207,153 +222,152 @@ private static StreamInfo extractOptionalData(StreamInfo streamInfo, StreamExtra try { streamInfo.setThumbnailUrl(extractor.getThumbnailUrl()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setDuration(extractor.getLength()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setUploaderName(extractor.getUploaderName()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setUploaderUrl(extractor.getUploaderUrl()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setUploaderAvatarUrl(extractor.getUploaderAvatarUrl()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setUploaderVerified(extractor.isUploaderVerified()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setUploaderSubscriberCount(extractor.getUploaderSubscriberCount()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setSubChannelName(extractor.getSubChannelName()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setSubChannelUrl(extractor.getSubChannelUrl()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setSubChannelAvatarUrl(extractor.getSubChannelAvatarUrl()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setDescription(extractor.getDescription()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setViewCount(extractor.getViewCount()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setTextualUploadDate(extractor.getTextualUploadDate()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setUploadDate(extractor.getUploadDate()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setStartPosition(extractor.getTimeStamp()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setLikeCount(extractor.getLikeCount()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setDislikeCount(extractor.getDislikeCount()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setSubtitles(extractor.getSubtitlesDefault()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } //additional info try { streamInfo.setHost(extractor.getHost()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setPrivacy(extractor.getPrivacy()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setCategory(extractor.getCategory()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setLicence(extractor.getLicence()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setLanguageInfo(extractor.getLanguageInfo()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setTags(extractor.getTags()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setSupportInfo(extractor.getSupportInfo()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setStreamSegments(extractor.getStreamSegments()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setMetaInfo(extractor.getMetaInfo()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } try { streamInfo.setPreviewFrames(extractor.getFrames()); - } catch (Exception e) { + } catch (final Exception e) { streamInfo.addError(e); } - streamInfo.setRelatedItems(ExtractorHelper.getRelatedItemsOrLogError(streamInfo, extractor)); - - return streamInfo; + streamInfo + .setRelatedItems(ExtractorHelper.getRelatedItemsOrLogError(streamInfo, extractor)); } private StreamType streamType; @@ -361,7 +375,7 @@ private static StreamInfo extractOptionalData(StreamInfo streamInfo, StreamExtra private String textualUploadDate; private DateWrapper uploadDate; private long duration = -1; - private int ageLimit = -1; + private int ageLimit; private Description description; private long viewCount = -1; @@ -398,7 +412,7 @@ private static StreamInfo extractOptionalData(StreamInfo streamInfo, StreamExtra private StreamExtractor.Privacy privacy; private String category = ""; private String licence = ""; - private String support = ""; + private String supportInfo = ""; private Locale language = null; private List tags = new ArrayList<>(); private List streamSegments = new ArrayList<>(); @@ -418,7 +432,7 @@ public StreamType getStreamType() { return streamType; } - public void setStreamType(StreamType streamType) { + public void setStreamType(final StreamType streamType) { this.streamType = streamType; } @@ -431,7 +445,7 @@ public String getThumbnailUrl() { return thumbnailUrl; } - public void setThumbnailUrl(String thumbnailUrl) { + public void setThumbnailUrl(final String thumbnailUrl) { this.thumbnailUrl = thumbnailUrl; } @@ -439,7 +453,7 @@ public String getTextualUploadDate() { return textualUploadDate; } - public void setTextualUploadDate(String textualUploadDate) { + public void setTextualUploadDate(final String textualUploadDate) { this.textualUploadDate = textualUploadDate; } @@ -447,7 +461,7 @@ public DateWrapper getUploadDate() { return uploadDate; } - public void setUploadDate(DateWrapper uploadDate) { + public void setUploadDate(final DateWrapper uploadDate) { this.uploadDate = uploadDate; } @@ -460,7 +474,7 @@ public long getDuration() { return duration; } - public void setDuration(long duration) { + public void setDuration(final long duration) { this.duration = duration; } @@ -468,7 +482,7 @@ public int getAgeLimit() { return ageLimit; } - public void setAgeLimit(int ageLimit) { + public void setAgeLimit(final int ageLimit) { this.ageLimit = ageLimit; } @@ -476,7 +490,7 @@ public Description getDescription() { return description; } - public void setDescription(Description description) { + public void setDescription(final Description description) { this.description = description; } @@ -484,7 +498,7 @@ public long getViewCount() { return viewCount; } - public void setViewCount(long viewCount) { + public void setViewCount(final long viewCount) { this.viewCount = viewCount; } @@ -497,7 +511,7 @@ public long getLikeCount() { return likeCount; } - public void setLikeCount(long likeCount) { + public void setLikeCount(final long likeCount) { this.likeCount = likeCount; } @@ -510,7 +524,7 @@ public long getDislikeCount() { return dislikeCount; } - public void setDislikeCount(long dislikeCount) { + public void setDislikeCount(final long dislikeCount) { this.dislikeCount = dislikeCount; } @@ -518,7 +532,7 @@ public String getUploaderName() { return uploaderName; } - public void setUploaderName(String uploaderName) { + public void setUploaderName(final String uploaderName) { this.uploaderName = uploaderName; } @@ -526,7 +540,7 @@ public String getUploaderUrl() { return uploaderUrl; } - public void setUploaderUrl(String uploaderUrl) { + public void setUploaderUrl(final String uploaderUrl) { this.uploaderUrl = uploaderUrl; } @@ -534,7 +548,7 @@ public String getUploaderAvatarUrl() { return uploaderAvatarUrl; } - public void setUploaderAvatarUrl(String uploaderAvatarUrl) { + public void setUploaderAvatarUrl(final String uploaderAvatarUrl) { this.uploaderAvatarUrl = uploaderAvatarUrl; } @@ -550,7 +564,7 @@ public long getUploaderSubscriberCount() { return uploaderSubscriberCount; } - public void setUploaderSubscriberCount(long uploaderSubscriberCount) { + public void setUploaderSubscriberCount(final long uploaderSubscriberCount) { this.uploaderSubscriberCount = uploaderSubscriberCount; } @@ -558,7 +572,7 @@ public String getSubChannelName() { return subChannelName; } - public void setSubChannelName(String subChannelName) { + public void setSubChannelName(final String subChannelName) { this.subChannelName = subChannelName; } @@ -566,7 +580,7 @@ public String getSubChannelUrl() { return subChannelUrl; } - public void setSubChannelUrl(String subChannelUrl) { + public void setSubChannelUrl(final String subChannelUrl) { this.subChannelUrl = subChannelUrl; } @@ -574,7 +588,7 @@ public String getSubChannelAvatarUrl() { return subChannelAvatarUrl; } - public void setSubChannelAvatarUrl(String subChannelAvatarUrl) { + public void setSubChannelAvatarUrl(final String subChannelAvatarUrl) { this.subChannelAvatarUrl = subChannelAvatarUrl; } @@ -582,7 +596,7 @@ public List getVideoStreams() { return videoStreams; } - public void setVideoStreams(List videoStreams) { + public void setVideoStreams(final List videoStreams) { this.videoStreams = videoStreams; } @@ -590,7 +604,7 @@ public List getAudioStreams() { return audioStreams; } - public void setAudioStreams(List audioStreams) { + public void setAudioStreams(final List audioStreams) { this.audioStreams = audioStreams; } @@ -598,7 +612,7 @@ public List getVideoOnlyStreams() { return videoOnlyStreams; } - public void setVideoOnlyStreams(List videoOnlyStreams) { + public void setVideoOnlyStreams(final List videoOnlyStreams) { this.videoOnlyStreams = videoOnlyStreams; } @@ -606,7 +620,7 @@ public String getDashMpdUrl() { return dashMpdUrl; } - public void setDashMpdUrl(String dashMpdUrl) { + public void setDashMpdUrl(final String dashMpdUrl) { this.dashMpdUrl = dashMpdUrl; } @@ -614,7 +628,7 @@ public List getSegmentedVideoStreams() { return segmentedVideoStreams; } - public void setSegmentedVideoStreams(List segmentedVideoStreams) { + public void setSegmentedVideoStreams(final List segmentedVideoStreams) { this.segmentedVideoStreams = segmentedVideoStreams; } @@ -622,7 +636,7 @@ public List getSegmentedAudioStreams() { return segmentedAudioStreams; } - public void setSegmentedAudioStreams(List segmentedAudioStreams) { + public void setSegmentedAudioStreams(final List segmentedAudioStreams) { this.segmentedAudioStreams = segmentedAudioStreams; } @@ -630,7 +644,7 @@ public List getSegmentedVideoOnlyStreams() { return segmentedVideoOnlyStreams; } - public void setSegmentedVideoOnlyStreams(List segmentedVideoOnlyStreams) { + public void setSegmentedVideoOnlyStreams(final List segmentedVideoOnlyStreams) { this.segmentedVideoOnlyStreams = segmentedVideoOnlyStreams; } @@ -638,7 +652,7 @@ public String getHlsUrl() { return hlsUrl; } - public void setHlsUrl(String hlsUrl) { + public void setHlsUrl(final String hlsUrl) { this.hlsUrl = hlsUrl; } @@ -654,7 +668,7 @@ public List getRelatedStreams() { return getRelatedItems(); } - public void setRelatedItems(List relatedItems) { + public void setRelatedItems(final List relatedItems) { this.relatedItems = relatedItems; } @@ -662,15 +676,15 @@ public void setRelatedItems(List relatedItems) { * @deprecated Use {@link #setRelatedItems(List)} */ @Deprecated - public void setRelatedStreams(List relatedItems) { - setRelatedItems(relatedItems); + public void setRelatedStreams(final List relatedItemsToSet) { + setRelatedItems(relatedItemsToSet); } public long getStartPosition() { return startPosition; } - public void setStartPosition(long startPosition) { + public void setStartPosition(final long startPosition) { this.startPosition = startPosition; } @@ -678,7 +692,7 @@ public List getSubtitles() { return subtitles; } - public void setSubtitles(List subtitles) { + public void setSubtitles(final List subtitles) { this.subtitles = subtitles; } @@ -686,63 +700,63 @@ public String getHost() { return this.host; } - public void setHost(String str) { - this.host = str; + public void setHost(final String host) { + this.host = host; } public StreamExtractor.Privacy getPrivacy() { return this.privacy; } - public void setPrivacy(StreamExtractor.Privacy str) { - this.privacy = str; + public void setPrivacy(final StreamExtractor.Privacy privacy) { + this.privacy = privacy; } public String getCategory() { return this.category; } - public void setCategory(String cat) { - this.category = cat; + public void setCategory(final String category) { + this.category = category; } public String getLicence() { return this.licence; } - public void setLicence(String str) { - this.licence = str; + public void setLicence(final String licence) { + this.licence = licence; } public Locale getLanguageInfo() { return this.language; } - public void setLanguageInfo(Locale lang) { - this.language = lang; + public void setLanguageInfo(final Locale locale) { + this.language = locale; } public List getTags() { return this.tags; } - public void setTags(List tags) { + public void setTags(final List tags) { this.tags = tags; } - public void setSupportInfo(String support) { - this.support = support; + public void setSupportInfo(final String support) { + this.supportInfo = support; } public String getSupportInfo() { - return this.support; + return this.supportInfo; } public List getStreamSegments() { return streamSegments; } - public void setStreamSegments(List streamSegments) { + public void setStreamSegments(final List streamSegments) { this.streamSegments = streamSegments; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItem.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItem.java index 61690df7ee..72e1454cad 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItem.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItem.java @@ -43,7 +43,10 @@ public class StreamInfoItem extends InfoItem { private String uploaderAvatarUrl = null; private boolean uploaderVerified = false; - public StreamInfoItem(int serviceId, String url, String name, StreamType streamType) { + public StreamInfoItem(final int serviceId, + final String url, + final String name, + final StreamType streamType) { super(InfoType.STREAM, serviceId, url, name); this.streamType = streamType; } @@ -56,23 +59,23 @@ public String getUploaderName() { return uploaderName; } - public void setUploaderName(String uploader_name) { - this.uploaderName = uploader_name; + public void setUploaderName(final String uploaderName) { + this.uploaderName = uploaderName; } public long getViewCount() { return viewCount; } - public void setViewCount(long view_count) { - this.viewCount = view_count; + public void setViewCount(final long viewCount) { + this.viewCount = viewCount; } public long getDuration() { return duration; } - public void setDuration(long duration) { + public void setDuration(final long duration) { this.duration = duration; } @@ -80,7 +83,7 @@ public String getUploaderUrl() { return uploaderUrl; } - public void setUploaderUrl(String uploaderUrl) { + public void setUploaderUrl(final String uploaderUrl) { this.uploaderUrl = uploaderUrl; } @@ -106,8 +109,8 @@ public String getTextualUploadDate() { return textualUploadDate; } - public void setTextualUploadDate(String uploadDate) { - this.textualUploadDate = uploadDate; + public void setTextualUploadDate(final String textualUploadDate) { + this.textualUploadDate = textualUploadDate; } @Nullable @@ -115,7 +118,7 @@ public DateWrapper getUploadDate() { return uploadDate; } - public void setUploadDate(@Nullable DateWrapper uploadDate) { + public void setUploadDate(@Nullable final DateWrapper uploadDate) { this.uploadDate = uploadDate; } @@ -123,25 +126,25 @@ public boolean isUploaderVerified() { return uploaderVerified; } - public void setUploaderVerified(boolean uploaderVerified) { + public void setUploaderVerified(final boolean uploaderVerified) { this.uploaderVerified = uploaderVerified; } @Override public String toString() { - return "StreamInfoItem{" + - "streamType=" + streamType + - ", uploaderName='" + uploaderName + '\'' + - ", textualUploadDate='" + textualUploadDate + '\'' + - ", viewCount=" + viewCount + - ", duration=" + duration + - ", uploaderUrl='" + uploaderUrl + '\'' + - ", infoType=" + getInfoType() + - ", serviceId=" + getServiceId() + - ", url='" + getUrl() + '\'' + - ", name='" + getName() + '\'' + - ", thumbnailUrl='" + getThumbnailUrl() + '\'' + - ", uploaderVerified='" + isUploaderVerified() + '\'' + - '}'; - } -} \ No newline at end of file + return "StreamInfoItem{" + + "streamType=" + streamType + + ", uploaderName='" + uploaderName + '\'' + + ", textualUploadDate='" + textualUploadDate + '\'' + + ", viewCount=" + viewCount + + ", duration=" + duration + + ", uploaderUrl='" + uploaderUrl + '\'' + + ", infoType=" + getInfoType() + + ", serviceId=" + getServiceId() + + ", url='" + getUrl() + '\'' + + ", name='" + getName() + '\'' + + ", thumbnailUrl='" + getThumbnailUrl() + '\'' + + ", uploaderVerified='" + isUploaderVerified() + '\'' + + '}'; + } +} diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemExtractor.java index 21aa25f2b0..131719d8c3 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemExtractor.java @@ -124,6 +124,7 @@ public interface StreamInfoItemExtractor extends InfoItemExtractor { * @throws ParsingException if there is an error in the extraction */ @Nullable - default String getShortDescription() throws ParsingException { return null; } - + default String getShortDescription() throws ParsingException { + return null; + } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemsCollector.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemsCollector.java index 01909c26ea..231a929e58 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemsCollector.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemsCollector.java @@ -1,14 +1,10 @@ package org.schabi.newpipe.extractor.stream; -import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.InfoItemsCollector; 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; /* * Created by Christian Schabesberger on 28.02.16. @@ -30,80 +26,76 @@ * along with NewPipe. If not, see . */ -public class StreamInfoItemsCollector extends InfoItemsCollector { +public class StreamInfoItemsCollector + extends InfoItemsCollector { - public StreamInfoItemsCollector(int serviceId) { + public StreamInfoItemsCollector(final int serviceId) { super(serviceId); } - public StreamInfoItemsCollector(int serviceId, Comparator comparator) { + public StreamInfoItemsCollector(final int serviceId, + final Comparator comparator) { super(serviceId, comparator); } @Override - public StreamInfoItem extract(StreamInfoItemExtractor extractor) throws ParsingException { + public StreamInfoItem extract(final StreamInfoItemExtractor extractor) throws ParsingException { if (extractor.isAd()) { throw new FoundAdException("Found ad"); } - // important information - int serviceId = getServiceId(); - String url = extractor.getUrl(); - String name = extractor.getName(); - StreamType streamType = extractor.getStreamType(); - - StreamInfoItem resultItem = new StreamInfoItem(serviceId, url, name, streamType); - + final StreamInfoItem resultItem = new StreamInfoItem( + getServiceId(), extractor.getUrl(), extractor.getName(), extractor.getStreamType()); // optional information try { resultItem.setDuration(extractor.getDuration()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setUploaderName(extractor.getUploaderName()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setTextualUploadDate(extractor.getTextualUploadDate()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setUploadDate(extractor.getUploadDate()); - } catch (ParsingException e) { + } catch (final ParsingException e) { addError(e); } try { resultItem.setViewCount(extractor.getViewCount()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setThumbnailUrl(extractor.getThumbnailUrl()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setUploaderUrl(extractor.getUploaderUrl()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setUploaderAvatarUrl(extractor.getUploaderAvatarUrl()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setUploaderVerified(extractor.isUploaderVerified()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } try { resultItem.setShortDescription(extractor.getShortDescription()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } @@ -111,23 +103,13 @@ public StreamInfoItem extract(StreamInfoItemExtractor extractor) throws ParsingE } @Override - public void commit(StreamInfoItemExtractor extractor) { + public void commit(final StreamInfoItemExtractor extractor) { try { addItem(extract(extractor)); - } catch (FoundAdException ae) { + } catch (final FoundAdException ae) { //System.out.println("AD_WARNING: " + ae.getMessage()); - } catch (Exception e) { + } catch (final Exception e) { addError(e); } } - - public List getStreamInfoItemList() { - List siiList = new ArrayList<>(); - for (InfoItem ii : super.getItems()) { - if (ii instanceof StreamInfoItem) { - siiList.add((StreamInfoItem) ii); - } - } - return siiList; - } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamSegment.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamSegment.java index d603cbe6a6..1fb4947208 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamSegment.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamSegment.java @@ -33,7 +33,7 @@ public class StreamSegment implements Serializable { @Nullable private String previewUrl = null; - public StreamSegment(String title, int startTimeSeconds) { + public StreamSegment(final String title, final int startTimeSeconds) { this.title = title; this.startTimeSeconds = startTimeSeconds; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/SubtitlesStream.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/SubtitlesStream.java index cbc18302ae..0ac01a89cf 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/SubtitlesStream.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/SubtitlesStream.java @@ -11,7 +11,10 @@ public class SubtitlesStream extends Stream implements Serializable { private final boolean autoGenerated; private final String code; - public SubtitlesStream(MediaFormat format, String languageCode, String url, boolean autoGenerated) { + public SubtitlesStream(final MediaFormat format, + final String languageCode, + final String url, + final boolean autoGenerated) { super(url, format); /* @@ -25,7 +28,8 @@ public SubtitlesStream(MediaFormat format, String languageCode, String url, bool this.locale = new Locale(splits[0]); break; case 3: - this.locale = new Locale(splits[0], splits[1], splits[2]);// complex variants doesn't work! + // complex variants doesn't work! + this.locale = new Locale(splits[0], splits[1], splits[2]); break; case 2: this.locale = new Locale(splits[0], splits[1]); @@ -45,11 +49,11 @@ public boolean isAutoGenerated() { } @Override - public boolean equalStats(Stream cmp) { - return super.equalStats(cmp) && - cmp instanceof SubtitlesStream && - code.equals(((SubtitlesStream) cmp).code) && - autoGenerated == ((SubtitlesStream) cmp).autoGenerated; + public boolean equalStats(final Stream cmp) { + return super.equalStats(cmp) + && cmp instanceof SubtitlesStream + && code.equals(((SubtitlesStream) cmp).code) + && autoGenerated == ((SubtitlesStream) cmp).autoGenerated; } public String getDisplayLanguageName() { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/VideoStream.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/VideoStream.java index 9beb6d14bd..9e6b4eb2be 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/VideoStream.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/VideoStream.java @@ -40,15 +40,18 @@ public class VideoStream extends Stream { private String quality; private String codec; - public VideoStream(String url, MediaFormat format, String resolution) { + public VideoStream(final String url, final MediaFormat format, final String resolution) { this(url, format, resolution, false); } - public VideoStream(String url, MediaFormat format, String resolution, boolean isVideoOnly) { + public VideoStream(final String url, + final MediaFormat format, + final String resolution, + final boolean isVideoOnly) { this(url, null, format, resolution, isVideoOnly); } - public VideoStream(String url, boolean isVideoOnly, ItagItem itag) { + public VideoStream(final String url, final boolean isVideoOnly, final ItagItem itag) { this(url, itag.getMediaFormat(), itag.resolutionString, isVideoOnly); this.itag = itag.id; this.bitrate = itag.getBitrate(); @@ -63,21 +66,28 @@ public VideoStream(String url, boolean isVideoOnly, ItagItem itag) { this.fps = itag.fps; } - public VideoStream(String url, String torrentUrl, MediaFormat format, String resolution) { + public VideoStream(final String url, + final String torrentUrl, + final MediaFormat format, + final String resolution) { this(url, torrentUrl, format, resolution, false); } - public VideoStream(String url, String torrentUrl, MediaFormat format, String resolution, boolean isVideoOnly) { + public VideoStream(final String url, + final String torrentUrl, + final MediaFormat format, + final String resolution, + final boolean isVideoOnly) { super(url, torrentUrl, format); this.resolution = resolution; this.isVideoOnly = isVideoOnly; } @Override - public boolean equalStats(Stream cmp) { - return super.equalStats(cmp) && cmp instanceof VideoStream && - resolution.equals(((VideoStream) cmp).resolution) && - isVideoOnly == ((VideoStream) cmp).isVideoOnly; + public boolean equalStats(final Stream cmp) { + return super.equalStats(cmp) && cmp instanceof VideoStream + && resolution.equals(((VideoStream) cmp).resolution) + && isVideoOnly == ((VideoStream) cmp).isVideoOnly; } /** diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/subscription/SubscriptionExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/subscription/SubscriptionExtractor.java index cc223630fd..cfa40ca144 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/subscription/SubscriptionExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/subscription/SubscriptionExtractor.java @@ -22,16 +22,17 @@ public InvalidSourceException() { this(null, null); } - public InvalidSourceException(String detailMessage) { + public InvalidSourceException(@Nullable final String detailMessage) { this(detailMessage, null); } - public InvalidSourceException(Throwable cause) { + public InvalidSourceException(final Throwable cause) { this(null, cause); } - public InvalidSourceException(String detailMessage, Throwable cause) { - super(detailMessage == null ? "Not a valid source" : "Not a valid source (" + detailMessage + ")", cause); + public InvalidSourceException(@Nullable final String detailMessage, final Throwable cause) { + super("Not a valid source" + (detailMessage == null ? "" : " (" + detailMessage + ")"), + cause); } } @@ -42,7 +43,8 @@ public enum ContentSource { private final List supportedSources; protected final StreamingService service; - public SubscriptionExtractor(StreamingService service, List supportedSources) { + public SubscriptionExtractor(final StreamingService service, + final List supportedSources) { this.service = service; this.supportedSources = Collections.unmodifiableList(supportedSources); } @@ -52,7 +54,8 @@ public List getSupportedSources() { } /** - * Returns an url that can help/guide the user to the file (or channel url) to extract the subscriptions. + * Returns an url that can help/guide the user to the file (or channel url) to extract the + * subscriptions. *

For example, in YouTube, the export subscriptions url is a good choice to return here.

*/ @Nullable @@ -72,7 +75,8 @@ public List fromChannelUrl(final String channelUrl) /** * Reads and parse a list of {@link SubscriptionItem} from the given InputStream. * - * @throws InvalidSourceException when the content read from the InputStream is invalid and can not be parsed + * @throws InvalidSourceException when the content read from the InputStream is invalid and can + * not be parsed */ public List fromInputStream(@Nonnull final InputStream contentInputStream) throws ExtractionException { @@ -83,7 +87,8 @@ public List fromInputStream(@Nonnull final InputStream content /** * Reads and parse a list of {@link SubscriptionItem} from the given InputStream. * - * @throws InvalidSourceException when the content read from the InputStream is invalid and can not be parsed + * @throws InvalidSourceException when the content read from the InputStream is invalid and can + * not be parsed */ public List fromInputStream(@Nonnull final InputStream contentInputStream, @Nonnull final String contentType) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/subscription/SubscriptionItem.java b/extractor/src/main/java/org/schabi/newpipe/extractor/subscription/SubscriptionItem.java index 2beee7a9db..4199164179 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/subscription/SubscriptionItem.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/subscription/SubscriptionItem.java @@ -4,9 +4,10 @@ public class SubscriptionItem implements Serializable { private final int serviceId; - private final String url, name; + private final String url; + private final String name; - public SubscriptionItem(int serviceId, String url, String name) { + public SubscriptionItem(final int serviceId, final String url, final String name) { this.serviceId = serviceId; this.url = url; this.name = name; @@ -26,7 +27,7 @@ public String getName() { @Override public String toString() { - return getClass().getSimpleName() + "@" + Integer.toHexString(hashCode()) + - "[name=" + name + " > " + serviceId + ":" + url + "]"; + return getClass().getSimpleName() + "@" + Integer.toHexString(hashCode()) + + "[name=" + name + " > " + serviceId + ":" + url + "]"; } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/suggestion/SuggestionExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/suggestion/SuggestionExtractor.java index 6b0dd28ae1..171fa0bd75 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/suggestion/SuggestionExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/suggestion/SuggestionExtractor.java @@ -15,11 +15,12 @@ public abstract class SuggestionExtractor { @Nullable private Localization forcedLocalization; @Nullable private ContentCountry forcedContentCountry; - public SuggestionExtractor(StreamingService service) { + public SuggestionExtractor(final StreamingService service) { this.service = service; } - public abstract List suggestionList(String query) throws IOException, ExtractionException; + public abstract List suggestionList(String query) + throws IOException, ExtractionException; public int getServiceId() { return service.getServiceId(); @@ -31,11 +32,11 @@ public StreamingService getService() { // TODO: Create a more general Extractor class - public void forceLocalization(@Nullable Localization localization) { + public void forceLocalization(@Nullable final Localization localization) { this.forcedLocalization = localization; } - public void forceContentCountry(@Nullable ContentCountry contentCountry) { + public void forceContentCountry(@Nullable final ContentCountry contentCountry) { this.forcedContentCountry = contentCountry; } @@ -46,6 +47,7 @@ public Localization getExtractorLocalization() { @Nonnull public ContentCountry getExtractorContentCountry() { - return forcedContentCountry == null ? getService().getContentCountry() : forcedContentCountry; + return forcedContentCountry == null + ? getService().getContentCountry() : forcedContentCountry; } } From bd7b3620408b0b9cf806e3ed103036086b0d5d50 Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 17 Mar 2022 15:40:12 +0100 Subject: [PATCH 04/23] Fix checkstyle issues & more in DashMpdParser Also remove useless null check on ItagItem.getItag() as that function already throws an exception if there is no itag --- .../extractor/utils/DashMpdParser.java | 133 ++++++++++-------- 1 file changed, 71 insertions(+), 62 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/DashMpdParser.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/DashMpdParser.java index b8b191ef7f..b1acabc75b 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/DashMpdParser.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/DashMpdParser.java @@ -43,13 +43,13 @@ * along with NewPipe. If not, see . */ -public class DashMpdParser { +public final class DashMpdParser { private DashMpdParser() { } public static class DashMpdParsingException extends ParsingException { - DashMpdParsingException(String message, Exception e) { + DashMpdParsingException(final String message, final Exception e) { super(message, e); } } @@ -64,12 +64,12 @@ public static class ParserResult { private final List segmentedVideoOnlyStreams; - public ParserResult(List videoStreams, - List audioStreams, - List videoOnlyStreams, - List segmentedVideoStreams, - List segmentedAudioStreams, - List segmentedVideoOnlyStreams) { + public ParserResult(final List videoStreams, + final List audioStreams, + final List videoOnlyStreams, + final List segmentedVideoStreams, + final List segmentedAudioStreams, + final List segmentedVideoOnlyStreams) { this.videoStreams = videoStreams; this.audioStreams = audioStreams; this.videoOnlyStreams = videoOnlyStreams; @@ -110,19 +110,20 @@ public List getSegmentedVideoOnlyStreams() { * It has video, video only and audio streams and will only add to the list if it don't * find a similar stream in the respective lists (calling {@link Stream#equalStats}). *

- * Info about dash MPD can be found here + * Info about dash MPD can be found + * here. * * @param streamInfo where the parsed streams will be added - * @see www.brendanlog.com */ public static ParserResult getStreams(final StreamInfo streamInfo) throws DashMpdParsingException, ReCaptchaException { - String dashDoc; - Downloader downloader = NewPipe.getDownloader(); + final String dashDoc; + final Downloader downloader = NewPipe.getDownloader(); try { dashDoc = downloader.get(streamInfo.getDashMpdUrl()).responseBody(); - } catch (IOException ioe) { - throw new DashMpdParsingException("Could not get dash mpd: " + streamInfo.getDashMpdUrl(), ioe); + } catch (final IOException ioe) { + throw new DashMpdParsingException( + "Could not get dash mpd: " + streamInfo.getDashMpdUrl(), ioe); } try { @@ -144,62 +145,70 @@ public static ParserResult getStreams(final StreamInfo streamInfo) for (int i = 0; i < representationList.getLength(); i++) { final Element representation = (Element) representationList.item(i); try { - final String mimeType = ((Element) representation.getParentNode()).getAttribute("mimeType"); + final String mimeType + = ((Element) representation.getParentNode()).getAttribute("mimeType"); final String id = representation.getAttribute("id"); - final String url = representation.getElementsByTagName("BaseURL").item(0).getTextContent(); + final String url = representation + .getElementsByTagName("BaseURL").item(0).getTextContent(); final ItagItem itag = ItagItem.getItag(Integer.parseInt(id)); - final Node segmentationList = representation.getElementsByTagName("SegmentList").item(0); - - // if SegmentList is not null this means that BaseUrl is not representing the url to the stream. - // instead we need to add the "media=" value from the tags inside the - // tag in order to get a full working url. However each of these is just pointing to a part of the - // video, so we can not return a URL with a working stream here. - // Instead of putting those streams into the list of regular stream urls wie put them in a - // for example "segmentedVideoStreams" list. - if (itag != null) { - final MediaFormat mediaFormat = MediaFormat.getFromMimeType(mimeType); - - if (itag.itagType.equals(ItagItem.ItagType.AUDIO)) { - if (segmentationList == null) { - final AudioStream audioStream = new AudioStream(url, mediaFormat, itag.avgBitrate); - if (!Stream.containSimilarStream(audioStream, streamInfo.getAudioStreams())) { - audioStreams.add(audioStream); - } - } else { - segmentedAudioStreams.add( - new AudioStream(id, mediaFormat, itag.avgBitrate)); + final Node segmentationList + = representation.getElementsByTagName("SegmentList").item(0); + + // If SegmentList is not null this means that BaseUrl is not representing the + // url to the stream. Instead we need to add the "media=" value from the + // tags inside the tag in order to get a full + // working url. However each of these is just pointing to a part of the video, + // so we can not return a URL with a working stream here. Instead of putting + // those streams into the list of regular stream urls we put them in a for + // example "segmentedVideoStreams" list. + + final MediaFormat mediaFormat = MediaFormat.getFromMimeType(mimeType); + + if (itag.itagType.equals(ItagItem.ItagType.AUDIO)) { + if (segmentationList == null) { + final AudioStream audioStream + = new AudioStream(url, mediaFormat, itag.avgBitrate); + if (!Stream.containSimilarStream(audioStream, + streamInfo.getAudioStreams())) { + audioStreams.add(audioStream); } } else { - boolean isVideoOnly = itag.itagType.equals(ItagItem.ItagType.VIDEO_ONLY); - - if (segmentationList == null) { - final VideoStream videoStream = new VideoStream(url, - mediaFormat, - itag.resolutionString, - isVideoOnly); - - if (isVideoOnly) { - if (!Stream.containSimilarStream(videoStream, streamInfo.getVideoOnlyStreams())) { - videoOnlyStreams.add(videoStream); - } - } else if (!Stream.containSimilarStream(videoStream, streamInfo.getVideoStreams())) { - videoStreams.add(videoStream); + segmentedAudioStreams.add( + new AudioStream(id, mediaFormat, itag.avgBitrate)); + } + } else { + final boolean isVideoOnly + = itag.itagType.equals(ItagItem.ItagType.VIDEO_ONLY); + + if (segmentationList == null) { + final VideoStream videoStream = new VideoStream(url, + mediaFormat, + itag.resolutionString, + isVideoOnly); + + if (isVideoOnly) { + if (!Stream.containSimilarStream(videoStream, + streamInfo.getVideoOnlyStreams())) { + videoOnlyStreams.add(videoStream); } + } else if (!Stream.containSimilarStream(videoStream, + streamInfo.getVideoStreams())) { + videoStreams.add(videoStream); + } + } else { + final VideoStream videoStream = new VideoStream(id, + mediaFormat, + itag.resolutionString, + isVideoOnly); + + if (isVideoOnly) { + segmentedVideoOnlyStreams.add(videoStream); } else { - final VideoStream videoStream = new VideoStream(id, - mediaFormat, - itag.resolutionString, - isVideoOnly); - - if (isVideoOnly) { - segmentedVideoOnlyStreams.add(videoStream); - } else { - segmentedVideoStreams.add(videoStream); - } + segmentedVideoStreams.add(videoStream); } } } - } catch (Exception ignored) { + } catch (final Exception ignored) { } } return new ParserResult( @@ -209,7 +218,7 @@ public static ParserResult getStreams(final StreamInfo streamInfo) segmentedVideoStreams, segmentedAudioStreams, segmentedVideoOnlyStreams); - } catch (Exception e) { + } catch (final Exception e) { throw new DashMpdParsingException("Could not parse Dash mpd", e); } } From 87d2834986a36028a7c5f84a1991e6a4b2ab54d4 Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 17 Mar 2022 15:41:51 +0100 Subject: [PATCH 05/23] Fix checkstyle issues & more in DonationLinkHelper Also add comment about the class being unused and replace the fixLink function with Utils.stringToUrl() --- .../extractor/utils/DonationLinkHelper.java | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/DonationLinkHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/DonationLinkHelper.java index fd2af7a2fd..30fce28876 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/DonationLinkHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/DonationLinkHelper.java @@ -1,9 +1,11 @@ package org.schabi.newpipe.extractor.utils; import java.net.MalformedURLException; -import java.net.URL; -public class DonationLinkHelper { +/** + * Note: this class seems unused? Should it be removed? + */ +public final class DonationLinkHelper { public enum DonationService { NO_DONATION, PATREON, @@ -15,9 +17,12 @@ public enum AffiliateService { AMAZON, } - public static DonationService getDonationServiceByLink(String link) throws MalformedURLException { - URL url = new URL(fixLink(link)); - switch (url.getHost()) { + private DonationLinkHelper() { + } + + public static DonationService getDonationServiceByLink(final String link) + throws MalformedURLException { + switch (Utils.stringToURL(link).getHost()) { case "www.patreon.com": case "patreon.com": return DonationService.PATREON; @@ -29,18 +34,11 @@ public static DonationService getDonationServiceByLink(String link) throws Malfo } } - public static AffiliateService getAffiliateServiceByLink(String link) throws MalformedURLException { - URL url = new URL(fixLink(link)); - switch (url.getHost()) { - case "amzn.to": return AffiliateService.AMAZON; - default: return AffiliateService.NO_AFFILIATE; + public static AffiliateService getAffiliateServiceByLink(final String link) + throws MalformedURLException { + if ("amzn.to".equals(Utils.stringToURL(link).getHost())) { + return AffiliateService.AMAZON; } + return AffiliateService.NO_AFFILIATE; } - - private static String fixLink(String link) { - return (link.startsWith("https://") || link.startsWith("http://")) - ? link - : "https://" + link; - } - } From 1d5f22e41f89e343c520975e2a96a54bfb23b4e1 Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 17 Mar 2022 15:49:28 +0100 Subject: [PATCH 06/23] Fix checkstyle issues & more in JsonUtils Also use Java 8 streams and extract duplicate code to getInstanceOf function --- .../newpipe/extractor/utils/JsonUtils.java | 119 +++++++++--------- 1 file changed, 61 insertions(+), 58 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java index 22d992f54f..989a680917 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java @@ -12,11 +12,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import javax.annotation.Nonnull; import javax.annotation.Nullable; -public class JsonUtils { +public final class JsonUtils { public static final JsonObject EMPTY_OBJECT = new JsonObject(); public static final JsonArray EMPTY_ARRAY = new JsonArray(); @@ -24,83 +25,85 @@ private JsonUtils() { } @Nonnull - public static Object getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException { + public static Object getValue(@Nonnull final JsonObject object, + @Nonnull final String path) throws ParsingException { - List keys = Arrays.asList(path.split("\\.")); - object = getObject(object, keys.subList(0, keys.size() - 1)); - if (null == object) throw new ParsingException("Unable to get " + path); - Object result = object.get(keys.get(keys.size() - 1)); - if (null == result) throw new ParsingException("Unable to get " + path); + final List keys = Arrays.asList(path.split("\\.")); + final JsonObject parentObject = getObject(object, keys.subList(0, keys.size() - 1)); + if (parentObject == null) { + throw new ParsingException("Unable to get " + path); + } + + final Object result = object.get(keys.get(keys.size() - 1)); + if (result == null) { + throw new ParsingException("Unable to get " + path); + } return result; } - @Nonnull - public static String getString(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException { - Object value = getValue(object, path); - if (value instanceof String) { - return (String) value; + private static T getInstanceOf(@Nonnull final JsonObject object, + @Nonnull final String path, + @Nonnull final Class klass) throws ParsingException { + final Object value = getValue(object, path); + if (klass.isInstance(value)) { + return klass.cast(value); } else { - throw new ParsingException("Unable to get " + path); + throw new ParsingException("Wrong data type at path " + path); } } @Nonnull - public static Boolean getBoolean(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException { - Object value = getValue(object, path); - if (value instanceof Boolean) { - return (Boolean) value; - } else { - throw new ParsingException("Unable to get " + path); - } + public static String getString(@Nonnull final JsonObject object, @Nonnull final String path) + throws ParsingException { + return getInstanceOf(object, path, String.class); } @Nonnull - public static Number getNumber(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException { - Object value = getValue(object, path); - if (value instanceof Number) { - return (Number) value; - } else { - throw new ParsingException("Unable to get " + path); - } + public static Boolean getBoolean(@Nonnull final JsonObject object, + @Nonnull final String path) throws ParsingException { + return getInstanceOf(object, path, Boolean.class); } @Nonnull - public static JsonObject getObject(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException { - Object value = getValue(object, path); - if (value instanceof JsonObject) { - return (JsonObject) value; - } else { - throw new ParsingException("Unable to get " + path); - } + public static Number getNumber(@Nonnull final JsonObject object, + @Nonnull final String path) + throws ParsingException { + return getInstanceOf(object, path, Number.class); } @Nonnull - public static JsonArray getArray(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException { - Object value = getValue(object, path); - if (value instanceof JsonArray) { - return (JsonArray) value; - } else { - throw new ParsingException("Unable to get " + path); - } + public static JsonObject getObject(@Nonnull final JsonObject object, + @Nonnull final String path) throws ParsingException { + return getInstanceOf(object, path, JsonObject.class); } @Nonnull - public static List getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException { + public static JsonArray getArray(@Nonnull final JsonObject object, @Nonnull final String path) + throws ParsingException { + return getInstanceOf(object, path, JsonArray.class); + } - List result = new ArrayList<>(); + @Nonnull + public static List getValues(@Nonnull final JsonArray array, @Nonnull final String path) + throws ParsingException { + + final List result = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { - JsonObject obj = array.getObject(i); + final JsonObject obj = array.getObject(i); result.add(getValue(obj, path)); } return result; } @Nullable - private static JsonObject getObject(@Nonnull JsonObject object, @Nonnull List keys) { + private static JsonObject getObject(@Nonnull final JsonObject object, + @Nonnull final List keys) { JsonObject result = object; - for (String key : keys) { + for (final String key : keys) { result = result.getObject(key); - if (null == result) break; + if (result == null) { + break; + } } return result; } @@ -108,7 +111,7 @@ private static JsonObject getObject(@Nonnull JsonObject object, @Nonnull ListExample HTML:

*
      * {@code
-     * 

This is Sparta!

+ *

+ * This is Sparta!

* } *
- *

Calling this function to get the attribute data-town returns the JsonObject for

+ *

Calling this function to get the attribute data-town returns the JsonObject + * for

*
      * {@code
      *   {
@@ -140,6 +145,7 @@ public static JsonObject toJsonObject(final String responseBody) throws ParsingE
      *   }
      * }
      * 
+ * * @param html The HTML where the JSON we're looking for is stored inside a * variable inside some JavaScript block * @param variable Name of the variable @@ -153,12 +159,9 @@ public static JsonObject getJsonData(final String html, final String variable) } public static List getStringListFromJsonArray(@Nonnull final JsonArray array) { - final List stringList = new ArrayList<>(array.size()); - for (Object o : array) { - if (o instanceof String) { - stringList.add((String) o); - } - } - return stringList; + return array.stream() + .filter(String.class::isInstance) + .map(String.class::cast) + .collect(Collectors.toList()); } } From ca7c63f273457c54e3f67c12492d8aa4ef0c8ddf Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 17 Mar 2022 15:57:30 +0100 Subject: [PATCH 07/23] Fix remaining checkstyle issues in utils/ subpackage --- .../extractor/utils/ExtractorHelper.java | 26 +++++--- .../newpipe/extractor/utils/JavaScript.java | 2 +- .../newpipe/extractor/utils/Parser.java | 52 ++++++++------- .../newpipe/extractor/utils/StringUtils.java | 5 +- .../schabi/newpipe/extractor/utils/Utils.java | 65 +++++++++---------- 5 files changed, 79 insertions(+), 71 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/ExtractorHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/ExtractorHelper.java index 3a74c2d3d1..12023ca4c9 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/ExtractorHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/ExtractorHelper.java @@ -11,31 +11,36 @@ import java.util.Collections; import java.util.List; -public class ExtractorHelper { - private ExtractorHelper() {} +public final class ExtractorHelper { + private ExtractorHelper() { + } - public static InfoItemsPage getItemsPageOrLogError(Info info, ListExtractor extractor) { + public static InfoItemsPage getItemsPageOrLogError( + final Info info, final ListExtractor extractor) { try { - InfoItemsPage page = extractor.getInitialPage(); + final InfoItemsPage page = extractor.getInitialPage(); info.addAllErrors(page.getErrors()); return page; - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); return InfoItemsPage.emptyPage(); } } - public static List getRelatedItemsOrLogError(StreamInfo info, StreamExtractor extractor) { + public static List getRelatedItemsOrLogError(final StreamInfo info, + final StreamExtractor extractor) { try { - InfoItemsCollector collector = extractor.getRelatedItems(); - if (collector == null) return Collections.emptyList(); + final InfoItemsCollector collector = extractor.getRelatedItems(); + if (collector == null) { + return Collections.emptyList(); + } info.addAllErrors(collector.getErrors()); //noinspection unchecked return (List) collector.getItems(); - } catch (Exception e) { + } catch (final Exception e) { info.addError(e); return Collections.emptyList(); } @@ -45,7 +50,8 @@ public static List getRelatedItemsOrLogError(StreamInfo info, StreamEx * @deprecated Use {@link #getRelatedItemsOrLogError(StreamInfo, StreamExtractor)} */ @Deprecated - public static List getRelatedVideosOrLogError(StreamInfo info, StreamExtractor extractor) { + public static List getRelatedVideosOrLogError(final StreamInfo info, + final StreamExtractor extractor) { return getRelatedItemsOrLogError(info, extractor); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/JavaScript.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/JavaScript.java index 4b81ba7d7c..c588793843 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/JavaScript.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/JavaScript.java @@ -4,7 +4,7 @@ import org.mozilla.javascript.Function; import org.mozilla.javascript.ScriptableObject; -public class JavaScript { +public final class JavaScript { private JavaScript() { } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Parser.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Parser.java index 5b25ed7601..96789856bc 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Parser.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Parser.java @@ -39,60 +39,66 @@ /** * avoid using regex !!! */ -public class Parser { +public final class Parser { private Parser() { } public static class RegexException extends ParsingException { - public RegexException(String message) { + public RegexException(final String message) { super(message); } } - public static String matchGroup1(String pattern, String input) throws RegexException { + public static String matchGroup1(final String pattern, final String input) + throws RegexException { return matchGroup(pattern, input, 1); } - public static String matchGroup1(Pattern pattern, String input) throws RegexException { + public static String matchGroup1(final Pattern pattern, + final String input) throws RegexException { return matchGroup(pattern, input, 1); } - public static String matchGroup(String pattern, String input, int group) throws RegexException { - Pattern pat = Pattern.compile(pattern); - return matchGroup(pat, input, group); + public static String matchGroup(final String pattern, + final String input, + final int group) throws RegexException { + return matchGroup(Pattern.compile(pattern), input, group); } - public static String matchGroup(Pattern pat, String input, int group) throws RegexException { - Matcher mat = pat.matcher(input); - boolean foundMatch = mat.find(); + public static String matchGroup(final Pattern pat, final String input, final int group) + throws RegexException { + final Matcher matcher = pat.matcher(input); + final boolean foundMatch = matcher.find(); if (foundMatch) { - return mat.group(group); + return matcher.group(group); } else { // only pass input to exception message when it is not too long if (input.length() > 1024) { throw new RegexException("failed to find pattern \"" + pat.pattern() + "\""); } else { - throw new RegexException("failed to find pattern \"" + pat.pattern() + "\" inside of \"" + input + "\""); + throw new RegexException("failed to find pattern \"" + pat.pattern() + + "\" inside of \"" + input + "\""); } } } - public static boolean isMatch(String pattern, String input) { + public static boolean isMatch(final String pattern, final String input) { final Pattern pat = Pattern.compile(pattern); final Matcher mat = pat.matcher(input); return mat.find(); } - public static boolean isMatch(Pattern pattern, String input) { + public static boolean isMatch(final Pattern pattern, final String input) { final Matcher mat = pattern.matcher(input); return mat.find(); } - public static Map compatParseMap(final String input) throws UnsupportedEncodingException { - Map map = new HashMap<>(); - for (String arg : input.split("&")) { - String[] splitArg = arg.split("="); + public static Map compatParseMap(final String input) + throws UnsupportedEncodingException { + final Map map = new HashMap<>(); + for (final String arg : input.split("&")) { + final String[] splitArg = arg.split("="); if (splitArg.length > 1) { map.put(splitArg[0], URLDecoder.decode(splitArg[1], UTF_8)); } else { @@ -104,19 +110,19 @@ public static Map compatParseMap(final String input) throws Unsu public static String[] getLinksFromString(final String txt) throws ParsingException { try { - ArrayList links = new ArrayList<>(); - LinkExtractor linkExtractor = LinkExtractor.builder() + final ArrayList links = new ArrayList<>(); + final LinkExtractor linkExtractor = LinkExtractor.builder() .linkTypes(EnumSet.of(LinkType.URL, LinkType.WWW)) .build(); - Iterable linkss = linkExtractor.extractLinks(txt); - for (LinkSpan ls : linkss) { + final Iterable linkSpans = linkExtractor.extractLinks(txt); + for (final LinkSpan ls : linkSpans) { links.add(txt.substring(ls.getBeginIndex(), ls.getEndIndex())); } String[] linksarray = new String[links.size()]; linksarray = links.toArray(linksarray); return linksarray; - } catch (Exception e) { + } catch (final Exception e) { throw new ParsingException("Could not get links from string", e); } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/StringUtils.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/StringUtils.java index ea61bbfbc2..3002a8d6d7 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/StringUtils.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/StringUtils.java @@ -2,7 +2,7 @@ import javax.annotation.Nonnull; -public class StringUtils { +public final class StringUtils { private StringUtils() { } @@ -15,7 +15,8 @@ private StringUtils() { * or parenthesis could not be matched . */ @Nonnull - public static String matchToClosingParenthesis(@Nonnull final String string, @Nonnull final String start) { + public static String matchToClosingParenthesis(@Nonnull final String string, + @Nonnull final String start) { int startIndex = string.indexOf(start); if (startIndex < 0) { throw new IndexOutOfBoundsException(); 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 d47d9afb6f..124a9bc8db 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,10 +6,15 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; -import java.util.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; import java.util.regex.Pattern; -public class Utils { +public final class Utils { public static final String HTTP = "http://"; public static final String HTTPS = "https://"; @@ -46,18 +51,16 @@ public static String removeNonDigitCharacters(final String toRemove) { * * @param numberWord string to be converted to a long * @return a long - * @throws NumberFormatException - * @throws ParsingException */ public static long mixedNumberWordToLong(final String numberWord) throws NumberFormatException, ParsingException { String multiplier = ""; try { multiplier = Parser.matchGroup("[\\d]+([\\.,][\\d]+)?([KMBkmb])+", numberWord, 2); - } catch (ParsingException ignored) { + } catch (final ParsingException ignored) { } - double count = Double.parseDouble(Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord) - .replace(",", ".")); + final double count = Double.parseDouble( + Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord).replace(",", ".")); switch (multiplier.toUpperCase()) { case "K": return (long) (count * 1e3); @@ -86,15 +89,10 @@ public static void checkUrl(final String pattern, final String url) throws Parsi } } - public static void printErrors(List errors) { - for (Throwable e : errors) { - e.printStackTrace(); - System.err.println("----------------"); - } - } - public static String replaceHttpWithHttps(final String url) { - if (url == null) return null; + if (url == null) { + return null; + } if (!url.isEmpty() && url.startsWith(HTTP)) { return HTTPS + url.substring(HTTP.length()); @@ -111,19 +109,17 @@ public static String replaceHttpWithHttps(final String url) { * @return a string that contains the value of the query parameter or null if nothing was found */ public static String getQueryValue(final URL url, final String parameterName) { - String urlQuery = url.getQuery(); + final String urlQuery = url.getQuery(); if (urlQuery != null) { - for (String param : urlQuery.split("&")) { - String[] params = param.split("=", 2); + for (final String param : urlQuery.split("&")) { + final String[] params = param.split("=", 2); String query; try { query = URLDecoder.decode(params[0], UTF_8); } catch (final UnsupportedEncodingException e) { - System.err.println( - "Cannot decode string with UTF-8. using the string without decoding"); - e.printStackTrace(); + // Cannot decode string with UTF-8, using the string without decoding query = params[0]; } @@ -131,9 +127,7 @@ public static String getQueryValue(final URL url, final String parameterName) { try { return URLDecoder.decode(params[1], UTF_8); } catch (final UnsupportedEncodingException e) { - System.err.println( - "Cannot decode string with UTF-8. using the string without decoding"); - e.printStackTrace(); + // Cannot decode string with UTF-8, using the string without decoding return params[1]; } } @@ -153,7 +147,7 @@ public static String getQueryValue(final URL url, final String parameterName) { public static URL stringToURL(final String url) throws MalformedURLException { try { return new URL(url); - } catch (MalformedURLException e) { + } catch (final MalformedURLException e) { // if no protocol is given try prepending "https://" if (e.getMessage().equals("no protocol: " + url)) { return new URL(HTTPS + url); @@ -165,13 +159,13 @@ public static URL stringToURL(final String url) throws MalformedURLException { public static boolean isHTTP(final URL url) { // make sure its http or https - String protocol = url.getProtocol(); + final String protocol = url.getProtocol(); if (!protocol.equals("http") && !protocol.equals("https")) { return false; } - boolean usesDefaultPort = url.getPort() == url.getDefaultPort(); - boolean setsNoPort = url.getPort() == -1; + final boolean usesDefaultPort = url.getPort() == url.getDefaultPort(); + final boolean setsNoPort = url.getPort() == -1; return setsNoPort || usesDefaultPort; } @@ -186,14 +180,15 @@ public static String removeMAndWWWFromUrl(final String url) { return url; } - public static String removeUTF8BOM(String s) { - if (s.startsWith("\uFEFF")) { - s = s.substring(1); + public static String removeUTF8BOM(final String s) { + String result = s; + if (result.startsWith("\uFEFF")) { + result = result.substring(1); } - if (s.endsWith("\uFEFF")) { - s = s.substring(0, s.length() - 1); + if (result.endsWith("\uFEFF")) { + result = result.substring(0, result.length() - 1); } - return s; + return result; } public static String getBaseUrl(final String url) throws ParsingException { @@ -243,7 +238,7 @@ public static boolean isNullOrEmpty(final Collection collection) { } // can be used for JsonObjects - public static boolean isNullOrEmpty(final Map map) { + public static boolean isNullOrEmpty(final Map map) { return map == null || map.isEmpty(); } From d79e20340cfea7d7a96b2e40487c0f069491d25c Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 17 Mar 2022 16:14:58 +0100 Subject: [PATCH 08/23] Fix checkstyle issues in root package extractor/ Note: not all issues were fixed because MediaFormat and ServiceList use a specific formatting that makes sense for them --- .../schabi/newpipe/extractor/Extractor.java | 36 ++++-- .../org/schabi/newpipe/extractor/Info.java | 27 +++-- .../schabi/newpipe/extractor/InfoItem.java | 7 +- .../newpipe/extractor/InfoItemsCollector.java | 13 +- .../newpipe/extractor/ListExtractor.java | 18 ++- .../schabi/newpipe/extractor/ListInfo.java | 22 ++-- .../schabi/newpipe/extractor/MediaFormat.java | 36 +++--- .../schabi/newpipe/extractor/MetaInfo.java | 6 +- .../org/schabi/newpipe/extractor/NewPipe.java | 45 +++---- .../org/schabi/newpipe/extractor/Page.java | 7 +- .../newpipe/extractor/StreamingService.java | 112 ++++++++++-------- 11 files changed, 191 insertions(+), 138 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/Extractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/Extractor.java index 9389e255d6..523b548b47 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/Extractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/Extractor.java @@ -10,13 +10,15 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; + import java.io.IOException; import java.util.Objects; public abstract class Extractor { /** * {@link StreamingService} currently related to this extractor.
- * Useful for getting other things from a service (like the url handlers for cleaning/accepting/get id from urls). + * Useful for getting other things from a service (like the url handlers for + * cleaning/accepting/get id from urls). */ private final StreamingService service; private final LinkHandler linkHandler; @@ -27,16 +29,18 @@ public abstract class Extractor { private ContentCountry forcedContentCountry = null; private boolean pageFetched = false; - private final Downloader downloader; + // called like this to prevent checkstyle errors about "hiding a field" + private final Downloader theDownloader; public Extractor(final StreamingService service, final LinkHandler linkHandler) { this.service = Objects.requireNonNull(service, "service is null"); this.linkHandler = Objects.requireNonNull(linkHandler, "LinkHandler is null"); - this.downloader = Objects.requireNonNull(NewPipe.getDownloader(), "downloader is null"); + this.theDownloader = Objects.requireNonNull(NewPipe.getDownloader(), "downloader is null"); } /** - * @return The {@link LinkHandler} of the current extractor object (e.g. a ChannelExtractor should return a channel url handler). + * @return The {@link LinkHandler} of the current extractor object (e.g. a ChannelExtractor + * should return a channel url handler). */ @Nonnull public LinkHandler getLinkHandler() { @@ -50,13 +54,17 @@ public LinkHandler getLinkHandler() { * @throws ExtractionException if the pages content is not understood */ public void fetchPage() throws IOException, ExtractionException { - if (pageFetched) return; - onFetchPage(downloader); + if (pageFetched) { + return; + } + onFetchPage(theDownloader); pageFetched = true; } protected void assertPageFetched() { - if (!pageFetched) throw new IllegalStateException("Page is not fetched. Make sure you call fetchPage()"); + if (!pageFetched) { + throw new IllegalStateException("Page is not fetched. Make sure you call fetchPage()"); + } } protected boolean isPageFetched() { @@ -66,11 +74,12 @@ protected boolean isPageFetched() { /** * Fetch the current page. * - * @param downloader the download to use + * @param downloader the downloader to use * @throws IOException if the page can not be loaded * @throws ExtractionException if the pages content is not understood */ - public abstract void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException; + public abstract void onFetchPage(@Nonnull Downloader downloader) + throws IOException, ExtractionException; @Nonnull public String getId() throws ParsingException { @@ -111,18 +120,18 @@ public int getServiceId() { } public Downloader getDownloader() { - return downloader; + return theDownloader; } /*////////////////////////////////////////////////////////////////////////// // Localization //////////////////////////////////////////////////////////////////////////*/ - public void forceLocalization(Localization localization) { + public void forceLocalization(final Localization localization) { this.forcedLocalization = localization; } - public void forceContentCountry(ContentCountry contentCountry) { + public void forceContentCountry(final ContentCountry contentCountry) { this.forcedContentCountry = contentCountry; } @@ -133,7 +142,8 @@ public Localization getExtractorLocalization() { @Nonnull public ContentCountry getExtractorContentCountry() { - return forcedContentCountry == null ? getService().getContentCountry() : forcedContentCountry; + return forcedContentCountry == null ? getService().getContentCountry() + : forcedContentCountry; } @Nonnull diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/Info.java b/extractor/src/main/java/org/schabi/newpipe/extractor/Info.java index 3a1980b568..78a15553b1 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/Info.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/Info.java @@ -17,7 +17,8 @@ public abstract class Info implements Serializable { */ private final String id; /** - * Different than the {@link #originalUrl} in the sense that it may be set as a cleaned url. + * Different than the {@link #originalUrl} in the sense that it may be set as a cleaned + * url. * * @see LinkHandler#getUrl() * @see Extractor#getOriginalUrl() @@ -33,15 +34,19 @@ public abstract class Info implements Serializable { private final List errors = new ArrayList<>(); - public void addError(Throwable throwable) { + public void addError(final Throwable throwable) { this.errors.add(throwable); } - public void addAllErrors(Collection errors) { - this.errors.addAll(errors); + public void addAllErrors(final Collection throwables) { + this.errors.addAll(throwables); } - public Info(int serviceId, String id, String url, String originalUrl, String name) { + public Info(final int serviceId, + final String id, + final String url, + final String originalUrl, + final String name) { this.serviceId = serviceId; this.id = id; this.url = url; @@ -49,7 +54,7 @@ public Info(int serviceId, String id, String url, String originalUrl, String nam this.name = name; } - public Info(int serviceId, LinkHandler linkHandler, String name) { + public Info(final int serviceId, final LinkHandler linkHandler, final String name) { this(serviceId, linkHandler.getId(), linkHandler.getUrl(), @@ -59,14 +64,16 @@ public Info(int serviceId, LinkHandler linkHandler, String name) { @Override public String toString() { - final String ifDifferentString = !url.equals(originalUrl) ? " (originalUrl=\"" + originalUrl + "\")" : ""; - return getClass().getSimpleName() + "[url=\"" + url + "\"" + ifDifferentString + ", name=\"" + name + "\"]"; + final String ifDifferentString + = url.equals(originalUrl) ? "" : " (originalUrl=\"" + originalUrl + "\")"; + return getClass().getSimpleName() + "[url=\"" + url + "\"" + ifDifferentString + + ", name=\"" + name + "\"]"; } // if you use an api and want to handle the website url // overriding original url is essential - public void setOriginalUrl(String url) { - originalUrl = url; + public void setOriginalUrl(final String originalUrl) { + this.originalUrl = originalUrl; } public int getServiceId() { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/InfoItem.java b/extractor/src/main/java/org/schabi/newpipe/extractor/InfoItem.java index aead6c7f63..cbcab0a50c 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/InfoItem.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/InfoItem.java @@ -29,7 +29,10 @@ public abstract class InfoItem implements Serializable { private final String name; private String thumbnailUrl; - public InfoItem(InfoType infoType, int serviceId, String url, String name) { + public InfoItem(final InfoType infoType, + final int serviceId, + final String url, + final String name) { this.infoType = infoType; this.serviceId = serviceId; this.url = url; @@ -52,7 +55,7 @@ public String getName() { return name; } - public void setThumbnailUrl(String thumbnailUrl) { + public void setThumbnailUrl(final String thumbnailUrl) { this.thumbnailUrl = thumbnailUrl; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/InfoItemsCollector.java b/extractor/src/main/java/org/schabi/newpipe/extractor/InfoItemsCollector.java index 1fd4b28a4e..b0ac2e14f7 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/InfoItemsCollector.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/InfoItemsCollector.java @@ -29,7 +29,8 @@ * along with NewPipe. If not, see . */ -public abstract class InfoItemsCollector implements Collector { +public abstract class InfoItemsCollector + implements Collector { private final List itemList = new ArrayList<>(); private final List errors = new ArrayList<>(); @@ -77,7 +78,7 @@ public void reset() { * Add an error * @param error the error */ - protected void addError(Exception error) { + protected void addError(final Exception error) { errors.add(error); } @@ -85,7 +86,7 @@ protected void addError(Exception error) { * Add an item * @param item the item */ - protected void addItem(I item) { + protected void addItem(final I item) { itemList.add(item); } @@ -98,12 +99,12 @@ public int getServiceId() { } @Override - public void commit(E extractor) { + public void commit(final E extractor) { try { addItem(extract(extractor)); - } catch (FoundAdException ae) { + } catch (final FoundAdException ae) { // found an ad. Maybe a debug line could be placed here - } catch (ParsingException e) { + } catch (final ParsingException e) { addError(e); } } 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 e53dbbef97..abea56359c 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java @@ -12,6 +12,7 @@ /** * Base class to extractors that have a list (e.g. playlists, users). + * @param the info item type this list extractor provides */ public abstract class ListExtractor extends Extractor { /** @@ -30,7 +31,7 @@ public abstract class ListExtractor extends Extractor { */ public static final long ITEM_COUNT_MORE_THAN_100 = -3; - public ListExtractor(StreamingService service, ListLinkHandler linkHandler) { + public ListExtractor(final StreamingService service, final ListLinkHandler linkHandler) { super(service, linkHandler); } @@ -50,8 +51,9 @@ public ListExtractor(StreamingService service, ListLinkHandler linkHandler) { * @return a {@link InfoItemsPage} corresponding to the requested page * @see InfoItemsPage#getNextPage() */ - public abstract InfoItemsPage getPage(final Page page) throws IOException, ExtractionException; + public abstract InfoItemsPage getPage(Page page) throws IOException, ExtractionException; + @Nonnull @Override public ListLinkHandler getLinkHandler() { return (ListLinkHandler) super.getLinkHandler(); @@ -64,15 +66,17 @@ public ListLinkHandler getLinkHandler() { /** * A class that is used to wrap a list of gathered items and eventual errors, it * also contains a field that points to the next available page ({@link #nextPage}). + * @param the info item type that this page is supposed to store and provide */ public static class InfoItemsPage { private static final InfoItemsPage EMPTY = - new InfoItemsPage<>(Collections.emptyList(), null, Collections.emptyList()); + new InfoItemsPage<>(Collections.emptyList(), null, Collections.emptyList()); /** * A convenient method that returns a representation of an empty page. * - * @return a type-safe page with the list of items and errors empty and the nextPage set to {@code null}. + * @return a type-safe page with the list of items and errors empty and the nextPage set to + * {@code null}. */ public static InfoItemsPage emptyPage() { //noinspection unchecked @@ -97,11 +101,13 @@ public static InfoItemsPage emptyPage() { */ private final List errors; - public InfoItemsPage(InfoItemsCollector collector, Page nextPage) { + public InfoItemsPage(final InfoItemsCollector collector, final Page nextPage) { this(collector.getItems(), nextPage, collector.getErrors()); } - public InfoItemsPage(List itemsList, Page nextPage, List errors) { + public InfoItemsPage(final List itemsList, + final Page nextPage, + final List errors) { this.itemsList = itemsList; this.nextPage = nextPage; this.errors = errors; 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 c06c24c44a..8e383f3e18 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java @@ -10,19 +10,21 @@ public abstract class ListInfo extends Info { private final List contentFilters; private final String sortFilter; - public ListInfo(int serviceId, - String id, - String url, - String originalUrl, - String name, - List contentFilter, - String sortFilter) { + public ListInfo(final int serviceId, + final String id, + final String url, + final String originalUrl, + final String name, + final List contentFilter, + final String sortFilter) { super(serviceId, id, url, originalUrl, name); this.contentFilters = contentFilter; this.sortFilter = sortFilter; } - public ListInfo(int serviceId, ListLinkHandler listUrlIdHandler, String name) { + public ListInfo(final int serviceId, + final ListLinkHandler listUrlIdHandler, + final String name) { super(serviceId, listUrlIdHandler, name); this.contentFilters = listUrlIdHandler.getContentFilters(); this.sortFilter = listUrlIdHandler.getSortFilter(); @@ -32,7 +34,7 @@ public List getRelatedItems() { return relatedItems; } - public void setRelatedItems(List relatedItems) { + public void setRelatedItems(final List relatedItems) { this.relatedItems = relatedItems; } @@ -44,7 +46,7 @@ public Page getNextPage() { return nextPage; } - public void setNextPage(Page page) { + public void setNextPage(final Page page) { this.nextPage = page; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/MediaFormat.java b/extractor/src/main/java/org/schabi/newpipe/extractor/MediaFormat.java index 6936568a58..7194847075 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/MediaFormat.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/MediaFormat.java @@ -27,32 +27,34 @@ */ public enum MediaFormat { + // @formatter:off //video and audio combined formats - // id name suffix mime type - MPEG_4 (0x0, "MPEG-4", "mp4", "video/mp4"), - v3GPP (0x10, "3GPP", "3gp", "video/3gpp"), - WEBM (0x20, "WebM", "webm", "video/webm"), + // id name suffix mimeType + MPEG_4 (0x0, "MPEG-4", "mp4", "video/mp4"), + v3GPP (0x10, "3GPP", "3gp", "video/3gpp"), + WEBM (0x20, "WebM", "webm", "video/webm"), // audio formats - M4A (0x100, "m4a", "m4a", "audio/mp4"), - WEBMA (0x200, "WebM", "webm", "audio/webm"), - MP3 (0x300, "MP3", "mp3", "audio/mpeg"), - OPUS (0x400, "opus", "opus", "audio/opus"), - OGG (0x500, "ogg", "ogg", "audio/ogg"), - WEBMA_OPUS (0x200, "WebM Opus", "webm", "audio/webm"), + M4A (0x100, "m4a", "m4a", "audio/mp4"), + WEBMA (0x200, "WebM", "webm", "audio/webm"), + MP3 (0x300, "MP3", "mp3", "audio/mpeg"), + OPUS (0x400, "opus", "opus", "audio/opus"), + OGG (0x500, "ogg", "ogg", "audio/ogg"), + WEBMA_OPUS(0x200, "WebM Opus", "webm", "audio/webm"), // subtitles formats - VTT (0x1000, "WebVTT", "vtt", "text/vtt"), - TTML (0x2000, "Timed Text Markup Language", "ttml", "application/ttml+xml"), - TRANSCRIPT1 (0x3000, "TranScript v1", "srv1", "text/xml"), - TRANSCRIPT2 (0x4000, "TranScript v2", "srv2", "text/xml"), - TRANSCRIPT3 (0x5000, "TranScript v3", "srv3", "text/xml"), - SRT (0x6000, "SubRip file format", "srt", "text/srt"); + VTT (0x1000, "WebVTT", "vtt", "text/vtt"), + TTML (0x2000, "Timed Text Markup Language", "ttml", "application/ttml+xml"), + TRANSCRIPT1(0x3000, "TranScript v1", "srv1", "text/xml"), + TRANSCRIPT2(0x4000, "TranScript v2", "srv2", "text/xml"), + TRANSCRIPT3(0x5000, "TranScript v3", "srv3", "text/xml"), + SRT (0x6000, "SubRip file format", "srt", "text/srt"); + // @formatter:on public final int id; public final String name; public final String suffix; public final String mimeType; - MediaFormat(int id, String name, String suffix, String mimeType) { + MediaFormat(final int id, final String name, final String suffix, final String mimeType) { this.id = id; this.name = name; this.suffix = suffix; diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/MetaInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/MetaInfo.java index da9928e878..79e2650d1d 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/MetaInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/MetaInfo.java @@ -16,8 +16,10 @@ public class MetaInfo implements Serializable { private List urls = new ArrayList<>(); private List urlTexts = new ArrayList<>(); - public MetaInfo(@Nonnull final String title, @Nonnull final Description content, - @Nonnull final List urls, @Nonnull final List urlTexts) { + public MetaInfo(@Nonnull final String title, + @Nonnull final Description content, + @Nonnull final List urls, + @Nonnull final List urlTexts) { this.title = title; this.content = content; this.urls = urls; diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/NewPipe.java b/extractor/src/main/java/org/schabi/newpipe/extractor/NewPipe.java index cdd536849c..767a2e64b0 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/NewPipe.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/NewPipe.java @@ -32,7 +32,7 @@ /** * Provides access to streaming services supported by NewPipe. */ -public class NewPipe { +public final class NewPipe { private static Downloader downloader; private static Localization preferredLocalization; private static ContentCountry preferredContentCountry; @@ -40,19 +40,20 @@ public class NewPipe { private NewPipe() { } - public static void init(Downloader d) { + public static void init(final Downloader d) { downloader = d; preferredLocalization = Localization.DEFAULT; preferredContentCountry = ContentCountry.DEFAULT; } - public static void init(Downloader d, Localization l) { + public static void init(final Downloader d, final Localization l) { downloader = d; preferredLocalization = l; - preferredContentCountry = l.getCountryCode().isEmpty() ? ContentCountry.DEFAULT : new ContentCountry(l.getCountryCode()); + preferredContentCountry = l.getCountryCode().isEmpty() + ? ContentCountry.DEFAULT : new ContentCountry(l.getCountryCode()); } - public static void init(Downloader d, Localization l, ContentCountry c) { + public static void init(final Downloader d, final Localization l, final ContentCountry c) { downloader = d; preferredLocalization = l; preferredContentCountry = c; @@ -88,8 +89,8 @@ public static StreamingService getService(String serviceName) throws ExtractionE throw new ExtractionException("There's no service with the name = \"" + serviceName + "\""); } - public static StreamingService getServiceByUrl(String url) throws ExtractionException { - for (StreamingService service : ServiceList.all()) { + public static StreamingService getServiceByUrl(final String url) throws ExtractionException { + for (final StreamingService service : ServiceList.all()) { if (service.getLinkTypeByUrl(url) != StreamingService.LinkType.NONE) { return service; } @@ -97,18 +98,18 @@ public static StreamingService getServiceByUrl(String url) throws ExtractionExce throw new ExtractionException("No service can handle the url = \"" + url + "\""); } - public static int getIdOfService(String serviceName) { + public static int getIdOfService(final String serviceName) { try { return getService(serviceName).getServiceId(); - } catch (ExtractionException ignored) { + } catch (final ExtractionException ignored) { return -1; } } - public static String getNameOfService(int id) { + public static String getNameOfService(final int id) { try { return getService(id).getServiceInfo().getName(); - } catch (Exception e) { + } catch (final Exception e) { System.err.println("Service id not known"); e.printStackTrace(); return ""; @@ -119,19 +120,21 @@ public static String getNameOfService(int id) { // Localization //////////////////////////////////////////////////////////////////////////*/ - public static void setupLocalization(Localization preferredLocalization) { - setupLocalization(preferredLocalization, null); + public static void setupLocalization(final Localization thePreferredLocalization) { + setupLocalization(thePreferredLocalization, null); } - public static void setupLocalization(Localization preferredLocalization, @Nullable ContentCountry preferredContentCountry) { - NewPipe.preferredLocalization = preferredLocalization; + public static void setupLocalization( + final Localization thePreferredLocalization, + @Nullable final ContentCountry thePreferredContentCountry) { + NewPipe.preferredLocalization = thePreferredLocalization; - if (preferredContentCountry != null) { - NewPipe.preferredContentCountry = preferredContentCountry; + if (thePreferredContentCountry != null) { + NewPipe.preferredContentCountry = thePreferredContentCountry; } else { - NewPipe.preferredContentCountry = preferredLocalization.getCountryCode().isEmpty() + NewPipe.preferredContentCountry = thePreferredLocalization.getCountryCode().isEmpty() ? ContentCountry.DEFAULT - : new ContentCountry(preferredLocalization.getCountryCode()); + : new ContentCountry(thePreferredLocalization.getCountryCode()); } } @@ -140,7 +143,7 @@ public static Localization getPreferredLocalization() { return preferredLocalization == null ? Localization.DEFAULT : preferredLocalization; } - public static void setPreferredLocalization(Localization preferredLocalization) { + public static void setPreferredLocalization(final Localization preferredLocalization) { NewPipe.preferredLocalization = preferredLocalization; } @@ -149,7 +152,7 @@ public static ContentCountry getPreferredContentCountry() { return preferredContentCountry == null ? ContentCountry.DEFAULT : preferredContentCountry; } - public static void setPreferredContentCountry(ContentCountry preferredContentCountry) { + public static void setPreferredContentCountry(final ContentCountry preferredContentCountry) { NewPipe.preferredContentCountry = preferredContentCountry; } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/Page.java b/extractor/src/main/java/org/schabi/newpipe/extractor/Page.java index 5f49cb2ce3..e1b19e7fb9 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/Page.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/Page.java @@ -17,8 +17,11 @@ public class Page implements Serializable { @Nullable private final byte[] body; - public Page(final String url, final String id, final List ids, - final Map cookies, @Nullable final byte[] body) { + public Page(final String url, + final String id, + final List ids, + final Map cookies, + @Nullable final byte[] body) { this.url = url; this.id = id; this.ids = ids; diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/StreamingService.java b/extractor/src/main/java/org/schabi/newpipe/extractor/StreamingService.java index dcde0aff60..94b8ba2d91 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/StreamingService.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/StreamingService.java @@ -6,7 +6,12 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.feed.FeedExtractor; import org.schabi.newpipe.extractor.kiosk.KioskList; -import org.schabi.newpipe.extractor.linkhandler.*; +import org.schabi.newpipe.extractor.linkhandler.LinkHandler; +import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory; +import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; +import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory; +import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler; +import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory; import org.schabi.newpipe.extractor.localization.ContentCountry; import org.schabi.newpipe.extractor.localization.Localization; import org.schabi.newpipe.extractor.localization.TimeAgoParser; @@ -55,7 +60,7 @@ public static class ServiceInfo { * @param name the name of the service * @param mediaCapabilities the type of media this service can handle */ - public ServiceInfo(String name, List mediaCapabilities) { + public ServiceInfo(final String name, final List mediaCapabilities) { this.name = name; this.mediaCapabilities = Collections.unmodifiableList(mediaCapabilities); } @@ -74,8 +79,8 @@ public enum MediaCapability { } /** - * LinkType will be used to determine which type of URL you are handling, and therefore which part - * of NewPipe should handle a certain URL. + * LinkType will be used to determine which type of URL you are handling, and therefore which + * part of NewPipe should handle a certain URL. */ public enum LinkType { NONE, @@ -90,14 +95,15 @@ public enum LinkType { /** * Creates a new Streaming service. * If you Implement one do not set id within your implementation of this extractor, instead - * set the id when you put the extractor into - * ServiceList. + * set the id when you put the extractor into {@link ServiceList} * All other parameters can be set directly from the overriding constructor. * @param id the number of the service to identify him within the NewPipe frontend * @param name the name of the service * @param capabilities the type of media this service can handle */ - public StreamingService(int id, String name, List capabilities) { + public StreamingService(final int id, + final String name, + final List capabilities) { this.serviceId = id; this.serviceInfo = new ServiceInfo(name, capabilities); } @@ -172,22 +178,21 @@ public String toString() { public abstract SubscriptionExtractor getSubscriptionExtractor(); /** - * This method decides which strategy will be chosen to fetch the feed. In YouTube, for example, a separate feed - * exists which is lightweight and made specifically to be used like this. + * This method decides which strategy will be chosen to fetch the feed. In YouTube, for example, + * a separate feed exists which is lightweight and made specifically to be used like this. *

* In services which there's no other way to retrieve them, null should be returned. * * @return a {@link FeedExtractor} instance or null. */ @Nullable - public FeedExtractor getFeedExtractor(String url) throws ExtractionException { + public FeedExtractor getFeedExtractor(final String url) throws ExtractionException { return null; } /** * Must create a new instance of a KioskList implementation. * @return a new KioskList instance - * @throws ExtractionException */ public abstract KioskList getKioskList() throws ExtractionException; @@ -195,49 +200,52 @@ public FeedExtractor getFeedExtractor(String url) throws ExtractionException { * Must create a new instance of a ChannelExtractor implementation. * @param linkHandler is pointing to the channel which should be handled by this new instance. * @return a new ChannelExtractor - * @throws ExtractionException */ - public abstract ChannelExtractor getChannelExtractor(ListLinkHandler linkHandler) throws ExtractionException; + public abstract ChannelExtractor getChannelExtractor(ListLinkHandler linkHandler) + throws ExtractionException; /** * Must crete a new instance of a PlaylistExtractor implementation. * @param linkHandler is pointing to the playlist which should be handled by this new instance. * @return a new PlaylistExtractor - * @throws ExtractionException */ - public abstract PlaylistExtractor getPlaylistExtractor(ListLinkHandler linkHandler) throws ExtractionException; + public abstract PlaylistExtractor getPlaylistExtractor(ListLinkHandler linkHandler) + throws ExtractionException; /** * Must create a new instance of a StreamExtractor implementation. * @param linkHandler is pointing to the stream which should be handled by this new instance. * @return a new StreamExtractor - * @throws ExtractionException */ - public abstract StreamExtractor getStreamExtractor(LinkHandler linkHandler) throws ExtractionException; + public abstract StreamExtractor getStreamExtractor(LinkHandler linkHandler) + throws ExtractionException; - public abstract CommentsExtractor getCommentsExtractor(ListLinkHandler linkHandler) throws ExtractionException; + public abstract CommentsExtractor getCommentsExtractor(ListLinkHandler linkHandler) + throws ExtractionException; /*////////////////////////////////////////////////////////////////////////// // Extractors without link handler //////////////////////////////////////////////////////////////////////////*/ - public SearchExtractor getSearchExtractor(String query, - List contentFilter, - String sortFilter) throws ExtractionException { + public SearchExtractor getSearchExtractor(final String query, + final List contentFilter, + final String sortFilter) throws ExtractionException { return getSearchExtractor(getSearchQHFactory() .fromQuery(query, contentFilter, sortFilter)); } - public ChannelExtractor getChannelExtractor(String id, - List contentFilter, - String sortFilter) throws ExtractionException { + public ChannelExtractor getChannelExtractor(final String id, + final List contentFilter, + final String sortFilter) + throws ExtractionException { return getChannelExtractor(getChannelLHFactory() .fromQuery(id, contentFilter, sortFilter)); } - public PlaylistExtractor getPlaylistExtractor(String id, - List contentFilter, - String sortFilter) throws ExtractionException { + public PlaylistExtractor getPlaylistExtractor(final String id, + final List contentFilter, + final String sortFilter) + throws ExtractionException { return getPlaylistExtractor(getPlaylistLHFactory() .fromQuery(id, contentFilter, sortFilter)); } @@ -246,28 +254,28 @@ public PlaylistExtractor getPlaylistExtractor(String id, // Short extractors overloads //////////////////////////////////////////////////////////////////////////*/ - public SearchExtractor getSearchExtractor(String query) throws ExtractionException { + public SearchExtractor getSearchExtractor(final String query) throws ExtractionException { return getSearchExtractor(getSearchQHFactory().fromQuery(query)); } - public ChannelExtractor getChannelExtractor(String url) throws ExtractionException { + public ChannelExtractor getChannelExtractor(final String url) throws ExtractionException { return getChannelExtractor(getChannelLHFactory().fromUrl(url)); } - public PlaylistExtractor getPlaylistExtractor(String url) throws ExtractionException { + public PlaylistExtractor getPlaylistExtractor(final String url) throws ExtractionException { return getPlaylistExtractor(getPlaylistLHFactory().fromUrl(url)); } - public StreamExtractor getStreamExtractor(String url) throws ExtractionException { + public StreamExtractor getStreamExtractor(final String url) throws ExtractionException { return getStreamExtractor(getStreamLHFactory().fromUrl(url)); } - public CommentsExtractor getCommentsExtractor(String url) throws ExtractionException { - ListLinkHandlerFactory llhf = getCommentsLHFactory(); - if (llhf == null) { + public CommentsExtractor getCommentsExtractor(final String url) throws ExtractionException { + final ListLinkHandlerFactory listLinkHandlerFactory = getCommentsLHFactory(); + if (listLinkHandlerFactory == null) { return null; } - return getCommentsExtractor(llhf.fromUrl(url)); + return getCommentsExtractor(listLinkHandlerFactory.fromUrl(url)); } /*////////////////////////////////////////////////////////////////////////// @@ -320,7 +328,8 @@ public List getSupportedCountries() { * the user prefer (using {@link NewPipe#getPreferredLocalization()}), then it will: *

    *
  • Check if the exactly localization is supported by this service.
  • - *
  • If not, check if a less specific localization is available, using only the language code.
  • + *
  • If not, check if a less specific localization is available, using only the language + * code.
  • *
  • Fallback to the {@link Localization#DEFAULT default} localization.
  • *
*/ @@ -333,8 +342,9 @@ public Localization getLocalization() { } // Fallback to the first supported language that matches the preferred language - for (Localization supportedLanguage : getSupportedLocalizations()) { - if (supportedLanguage.getLanguageCode().equals(preferredLocalization.getLanguageCode())) { + for (final Localization supportedLanguage : getSupportedLocalizations()) { + if (supportedLanguage.getLanguageCode() + .equals(preferredLocalization.getLanguageCode())) { return supportedLanguage; } } @@ -343,8 +353,8 @@ public Localization getLocalization() { } /** - * Returns the country that should be used to fetch content in this service. It will get which country - * the user prefer (using {@link NewPipe#getPreferredContentCountry()}), then it will: + * Returns the country that should be used to fetch content in this service. It will get which + * country the user prefer (using {@link NewPipe#getPreferredContentCountry()}), then it will: *