Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
merge: refactor and apply #18 from 34j(Add sorting filters)
Browse files Browse the repository at this point in the history
Co-authored-by: 34j <[email protected]>
  • Loading branch information
InfinityLoop1308 and 34j committed Sep 16, 2022
1 parent 9e2248d commit d3682b1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -16,10 +18,15 @@ public NiconicoFilters() {
}

@Override
public String evaluateSelectedContentFilters() {
public String evaluateSelectedFilters(final String searchString) {
if (selectedSortFilter != null) {
String sortQuery = "";

final Optional<FilterItem> 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
Expand All @@ -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 "";
Expand All @@ -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 {
Expand All @@ -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);
}
}

}

0 comments on commit d3682b1

Please sign in to comment.