diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f820dcbd7..d2dbd9d86 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,3 +40,10 @@ jobs: echo running with mock downloader ./gradlew check --stacktrace -Ddownloader=MOCK fi + + - name: Upload test reports when failure occurs + uses: actions/upload-artifact@v3 + if: failure() + with: + name: NewPipeExtractor-test-reports + path: extractor/build/reports/tests/test/** diff --git a/build.gradle b/build.gradle index 3cc6fa0c4..3498625f3 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 JDK 11 } } diff --git a/checkstyle/checkstyle.xml b/checkstyle/checkstyle.xml new file mode 100644 index 000000000..4a82f6be3 --- /dev/null +++ b/checkstyle/checkstyle.xml @@ -0,0 +1,189 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/checkstyle/suppressions.xml b/checkstyle/suppressions.xml new file mode 100644 index 000000000..e51dee8f4 --- /dev/null +++ b/checkstyle/suppressions.xml @@ -0,0 +1,15 @@ + + + + + + + + + diff --git a/extractor/build.gradle b/extractor/build.gradle index a9b5f7f3e..70b51a338 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' 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 9389e255d..e973b4416 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 org.schabi.newpipe.extractor.localization.TimeAgoParser; 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; + // called like this to prevent checkstyle errors about "hiding a field" private final Downloader downloader; - public Extractor(final StreamingService service, final LinkHandler linkHandler) { + protected 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"); } /** - * @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 abstract class Extractor { * @throws ExtractionException if the pages content is not understood */ public void fetchPage() throws IOException, ExtractionException { - if (pageFetched) return; + if (pageFetched) { + return; + } onFetchPage(downloader); 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,13 @@ public abstract class Extractor { /** * 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; + @SuppressWarnings("HiddenField") + public abstract void onFetchPage(@Nonnull Downloader downloader) + throws IOException, ExtractionException; @Nonnull public String getId() throws ParsingException { @@ -118,11 +128,11 @@ public abstract class Extractor { // 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 +143,8 @@ public abstract class Extractor { @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 3a1980b56..78a15553b 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 abstract class Info implements Serializable { 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 abstract class Info implements Serializable { @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 aead6c7f6..cbcab0a50 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 abstract class InfoItem implements Serializable { 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 1fd4b28a4..b0ac2e14f 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 @@ import java.util.List; * 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 abstract class InfoItemsCollector 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 abstract class ListExtractor extends Extractor { * @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 abstract class ListExtractor extends Extractor { /** * 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 abstract class ListExtractor extends Extractor { */ 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 c06c24c44..8e383f3e1 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 abstract class ListInfo extends Info { return relatedItems; } - public void setRelatedItems(List relatedItems) { + public void setRelatedItems(final List relatedItems) { this.relatedItems = relatedItems; } @@ -44,7 +46,7 @@ public abstract class ListInfo extends Info { 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 6936568a5..442306848 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/MediaFormat.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/MediaFormat.java @@ -22,83 +22,90 @@ package org.schabi.newpipe.extractor; * along with NewPipe. If not, see . */ +import java.util.Arrays; +import java.util.function.Function; + /** * Static data about various media formats support by NewPipe, eg mime type, extension */ +@SuppressWarnings("MethodParamPad") // we want the media format table below to be aligned 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; this.mimeType = mimeType; } + private static T getById(final int id, + final Function field, + final T orElse) { + return Arrays.stream(MediaFormat.values()) + .filter(mediaFormat -> mediaFormat.id == id) + .map(field) + .findFirst() + .orElse(orElse); + } + /** * Return the friendly name of the media format with the supplied id * - * @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number. + * @param id the id of the media format. Currently an arbitrary, NewPipe-specific number. * @return the friendly name of the MediaFormat associated with this ids, * or an empty String if none match it. */ - public static String getNameById(int ident) { - for (MediaFormat vf : MediaFormat.values()) { - if (vf.id == ident) return vf.name; - } - return ""; + public static String getNameById(final int id) { + return getById(id, MediaFormat::getName, ""); } /** * Return the file extension of the media format with the supplied id * - * @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number. + * @param id the id of the media format. Currently an arbitrary, NewPipe-specific number. * @return the file extension of the MediaFormat associated with this ids, * or an empty String if none match it. */ - public static String getSuffixById(int ident) { - for (MediaFormat vf : MediaFormat.values()) { - if (vf.id == ident) return vf.suffix; - } - return ""; + public static String getSuffixById(final int id) { + return getById(id, MediaFormat::getSuffix, ""); } /** * Return the MIME type of the media format with the supplied id * - * @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number. + * @param id the id of the media format. Currently an arbitrary, NewPipe-specific number. * @return the MIME type of the MediaFormat associated with this ids, * or an empty String if none match it. */ - public static String getMimeById(int ident) { - for (MediaFormat vf : MediaFormat.values()) { - if (vf.id == ident) return vf.mimeType; - } - return ""; + public static String getMimeById(final int id) { + return getById(id, MediaFormat::getMimeType, null); } /** @@ -107,11 +114,11 @@ public enum MediaFormat { * @return MediaFormat associated with this mime type, * or null if none match it. */ - public static MediaFormat getFromMimeType(String mimeType) { - for (MediaFormat vf : MediaFormat.values()) { - if (vf.mimeType.equals(mimeType)) return vf; - } - return null; + public static MediaFormat getFromMimeType(final String mimeType) { + return Arrays.stream(MediaFormat.values()) + .filter(mediaFormat -> mediaFormat.mimeType.equals(mimeType)) + .findFirst() + .orElse(null); } /** @@ -120,18 +127,15 @@ public enum MediaFormat { * @param id the id * @return the id of the media format or null. */ - public static MediaFormat getFormatById(int id) { - for (MediaFormat vf : values()) { - if (vf.id == id) return vf; - } - return null; + public static MediaFormat getFormatById(final int id) { + return getById(id, mediaFormat -> mediaFormat, null); } - public static MediaFormat getFromSuffix(String suffix) { - for (MediaFormat vf : values()) { - if (vf.suffix.equals(suffix)) return vf; - } - return null; + public static MediaFormat getFromSuffix(final String suffix) { + return Arrays.stream(MediaFormat.values()) + .filter(mediaFormat -> mediaFormat.suffix.equals(suffix)) + .findFirst() + .orElse(null); } /** 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 da9928e87..79e2650d1 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/MultiInfoItemsCollector.java b/extractor/src/main/java/org/schabi/newpipe/extractor/MultiInfoItemsCollector.java index 1ceb1b139..0c1deb72c 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 service.getServiceId() == serviceId) + .findFirst() + .orElseThrow(() -> new ExtractionException( + "There's no service with the id = \"" + serviceId + "\"")); } - public static StreamingService getService(String serviceName) throws ExtractionException { - for (StreamingService service : ServiceList.all()) { - if (service.getServiceInfo().getName().equals(serviceName)) { - return service; - } - } - throw new ExtractionException("There's no service with the name = \"" + serviceName + "\""); + public static StreamingService getService(final String serviceName) throws ExtractionException { + return ServiceList.all().stream() + .filter(service -> service.getServiceInfo().getName().equals(serviceName)) + .findFirst() + .orElseThrow(() -> 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 +96,18 @@ public class NewPipe { 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 +118,21 @@ public class NewPipe { // 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 +141,7 @@ public class NewPipe { return preferredLocalization == null ? Localization.DEFAULT : preferredLocalization; } - public static void setPreferredLocalization(Localization preferredLocalization) { + public static void setPreferredLocalization(final Localization preferredLocalization) { NewPipe.preferredLocalization = preferredLocalization; } @@ -149,7 +150,7 @@ public class NewPipe { 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 5f49cb2ce..e1b19e7fb 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/ServiceList.java b/extractor/src/main/java/org/schabi/newpipe/extractor/ServiceList.java index 5bfc5692f..18c617f65 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/ServiceList.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/ServiceList.java @@ -31,6 +31,7 @@ import java.util.List; /** * A list of supported services. */ +@SuppressWarnings({"ConstantName", "InnerAssignment"}) // keep unusual names and inner assignments public final class ServiceList { private ServiceList() { //no instance 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 dcde0aff6..94b8ba2d9 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.ExtractionException; 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 abstract class StreamingService { * @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 abstract class StreamingService { } /** - * 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 abstract class StreamingService { /** * 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 abstract class StreamingService { 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 abstract class StreamingService { * 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 abstract class StreamingService { // 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 abstract class StreamingService { * 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 abstract class StreamingService { } // 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 abstract class StreamingService { } /** - * 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: *