From d3682b1670480c42e796ac6f0e5b8de5f88144e0 Mon Sep 17 00:00:00 2001 From: Infinity1309 <96324692+InfinityLoop1309@users.noreply.github.com> Date: Fri, 16 Sep 2022 10:28:23 +0800 Subject: [PATCH] merge: refactor and apply #18 from 34j(Add sorting filters) Co-authored-by: 34j <55338215+34j@users.noreply.github.com> --- .../NiconicoSearchQueryHandlerFactory.java | 12 ++- .../search/filter/NiconicoFilters.java | 87 ++++++++++++++++++- 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/niconico/linkHandler/NiconicoSearchQueryHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/niconico/linkHandler/NiconicoSearchQueryHandlerFactory.java index c81b9212f..ff7c975c0 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/niconico/linkHandler/NiconicoSearchQueryHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/niconico/linkHandler/NiconicoSearchQueryHandlerFactory.java @@ -28,13 +28,12 @@ public String getUrl(final String id, searchFilters.setSelectedSortFilter(selectedSortFilter); searchFilters.setSelectedContentFilter(selectedContentFilter); - final String filterQuery = searchFilters.evaluateSelectedContentFilters(); + final String filterQuery = searchFilters.evaluateSelectedFilters(null); try { return SEARCH_URL + "?q=" + URLEncoder.encode(id, UTF_8) + filterQuery + "&fields=contentId,title,userId,channelId" + ",viewCounter,lengthSeconds,thumbnailUrl,startTime" - + "&_sort=-viewCounter" + "&_offset=0" + "&_limit=" + ITEMS_PER_PAGE + "&_context=" + URLEncoder.encode(NiconicoService.APP_NAME, UTF_8); @@ -52,4 +51,13 @@ public Filter getAvailableContentFilter() { public FilterItem getFilterItem(final int filterId) { return searchFilters.getFilterItem(filterId); } + @Override + public Filter getAvailableSortFilter() { + return searchFilters.getSortFilters(); + } + + @Override + public Filter getContentFilterSortFilterVariant(final int contentFilterId) { + return searchFilters.getContentFilterSortFilterVariant(contentFilterId); + } } \ No newline at end of file diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/niconico/search/filter/NiconicoFilters.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/niconico/search/filter/NiconicoFilters.java index 8f808c776..16387d25b 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/niconico/search/filter/NiconicoFilters.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/niconico/search/filter/NiconicoFilters.java @@ -1,11 +1,13 @@ package org.schabi.newpipe.extractor.services.niconico.search.filter; import org.schabi.newpipe.extractor.search.filter.Filter; +import org.schabi.newpipe.extractor.search.filter.FilterGroup; import org.schabi.newpipe.extractor.search.filter.FilterItem; import org.schabi.newpipe.extractor.search.filter.SearchFiltersBase; -import org.schabi.newpipe.extractor.services.bandcamp.search.filter.BandcampFilters; -public class NiconicoFilters extends SearchFiltersBase { +import java.util.Optional; + +public final class NiconicoFilters extends SearchFiltersBase { private static final String ALL = "All"; private static final String TAGS_ONLY = "TagsOnly"; @@ -16,10 +18,15 @@ public NiconicoFilters() { } @Override - public String evaluateSelectedContentFilters() { + public String evaluateSelectedFilters(final String searchString) { if (selectedSortFilter != null) { String sortQuery = ""; + final Optional ascendingFilter = selectedSortFilter.stream() + .filter(filterItem -> filterItem instanceof NiconicoSortOrderFilterItem) + .findFirst(); + boolean isAscending = ascendingFilter.isPresent(); + if (selectedContentFilter != null && !selectedContentFilter.isEmpty()) { final NiconicoFilters.NiconicoContentFilterItem contentItem = // we assume that there is just one content filter @@ -30,6 +37,17 @@ public String evaluateSelectedContentFilters() { } } } + // only 1 sort filter + if (!selectedSortFilter.isEmpty()) { + final NiconicoFilters.NiconicoSortFilterItem sortItem = + // we assume that there is just one content filter + (NiconicoFilters.NiconicoSortFilterItem) selectedSortFilter.get(0); + if (sortItem != null) { + if (!sortItem.query.isEmpty()) { + sortQuery += "&" + sortItem.getQueryData(isAscending); + } + } + } return sortQuery; } return ""; @@ -49,6 +67,62 @@ protected void init() { builder.getFilterForId(contentFilterAll), builder.getFilterForId(contentFilterTagsOnly), })); + + /* 'Sort by' filter items */ + final int filterViewCount = builder.addSortItem( + new NiconicoSortFilterItem("Views", "_sort=viewCounter")); + final int filterBookmarkCount = builder.addSortItem( + new NiconicoSortFilterItem("Bookmarks", "_sort=mylistCounter")); + final int filterLikeCount = builder.addSortItem( + new NiconicoSortFilterItem("Likes", "_sort=likeCounter")); + final int filterCommentCount = builder.addSortItem( + new NiconicoSortFilterItem("Comments", "_sort=commentCounter")); + + final int filterLength = builder.addSortItem( + new NiconicoSortFilterItem("Length", "_sort=lengthSeconds")); + + final int filterPublishAt = builder.addSortItem( + new NiconicoSortFilterItem("Publish Time", "_sort=startTime")); + final int filterLastCommentedAt = builder.addSortItem( + new NiconicoSortFilterItem("Last Comment Time", "_sort=lastCommentTime")); + + final int filterSortOrderAscending = builder.addSortItem( + new NiconicoSortOrderFilterItem("Ascending")); + + + final Filter allSortFilters = new Filter.Builder(new FilterGroup[]{ + builder.createSortGroup("Sort by", true, new FilterItem[]{ + builder.getFilterForId(filterViewCount), + builder.getFilterForId(filterCommentCount), + builder.getFilterForId(filterBookmarkCount), + builder.getFilterForId(filterLikeCount), + builder.getFilterForId(filterLength), + builder.getFilterForId(filterPublishAt), + builder.getFilterForId(filterLastCommentedAt), + }), + builder.createSortGroup("Sort order", false, new FilterItem[]{ + builder.getFilterForId(filterSortOrderAscending) + }), + }).build(); + addContentFilterSortVariant(contentFilterAll, allSortFilters); + addContentFilterSortVariant(contentFilterTagsOnly, allSortFilters); + } + + private static class NiconicoSortFilterItem extends FilterItem { + protected final String query; + + NiconicoSortFilterItem(final String name, final String query) { + super(Filter.ITEM_IDENTIFIER_UNKNOWN, name); + this.query = query; + } + public String getQueryData(boolean isAscending) { + if (isAscending) { + return query.replace("=", "=+"); + } + else { + return query.replace("=", "=-"); + } + } } public static class NiconicoContentFilterItem extends FilterItem { @@ -59,4 +133,11 @@ public NiconicoContentFilterItem(final String name, final String query) { this.query = query; } } + + private static class NiconicoSortOrderFilterItem extends FilterItem{ + NiconicoSortOrderFilterItem(final String name){ + super(Filter.ITEM_IDENTIFIER_UNKNOWN, name); + } + } + }