|
| 1 | +// Created by evermind-zz 2022, licensed GNU GPL version 3 or later |
| 2 | + |
| 3 | +package org.schabi.newpipe.extractor.services.soundcloud.search.filter; |
| 4 | + |
| 5 | +import org.schabi.newpipe.extractor.search.filter.BaseSearchFilters; |
| 6 | +import org.schabi.newpipe.extractor.search.filter.FilterContainer; |
| 7 | +import org.schabi.newpipe.extractor.search.filter.FilterGroup; |
| 8 | +import org.schabi.newpipe.extractor.search.filter.FilterItem; |
| 9 | + |
| 10 | +public final class SoundcloudFilters extends BaseSearchFilters { |
| 11 | + |
| 12 | + public static final int ID_CF_MAIN_GRP = 0; |
| 13 | + public static final int ID_CF_MAIN_ALL = 1; |
| 14 | + public static final int ID_CF_MAIN_TRACKS = 2; |
| 15 | + public static final int ID_CF_MAIN_USERS = 3; |
| 16 | + public static final int ID_CF_MAIN_PLAYLISTS = 4; |
| 17 | + public static final int ID_SF_DATE_GRP = 5; |
| 18 | + public static final int ID_SF_DATE_ALL = 6; |
| 19 | + public static final int ID_SF_DATE_LAST_HOUR = 7; |
| 20 | + public static final int ID_SF_DATE_LAST_DAY = 8; |
| 21 | + public static final int ID_SF_DATE_LAST_WEEK = 9; |
| 22 | + public static final int ID_SF_DATE_LAST_MONTH = 10; |
| 23 | + public static final int ID_SF_DATE_LAST_YEAR = 11; |
| 24 | + public static final int ID_SF_DURATION_GRP = 12; |
| 25 | + public static final int ID_SF_DURATION_ALL = 13; |
| 26 | + public static final int ID_SF_DURATION_SHORT = 14; |
| 27 | + public static final int ID_SF_DURATION_MEDIUM = 15; |
| 28 | + public static final int ID_SF_DURATION_LONG = 16; |
| 29 | + public static final int ID_SF_DURATION_EPIC = 17; |
| 30 | + public static final int ID_SF_LICENSE_GRP = 18; |
| 31 | + public static final int ID_SF_LICENSE_ALL = 19; |
| 32 | + public static final int ID_SF_LICENSE_COMMERCE = 20; |
| 33 | + |
| 34 | + public static final String TRACKS = "tracks"; |
| 35 | + public static final String USERS = "users"; |
| 36 | + public static final String PLAYLISTS = "playlists"; |
| 37 | + public static final String ALL = "all"; |
| 38 | + |
| 39 | + @Override |
| 40 | + public String evaluateSelectedContentFilters() { |
| 41 | + if (selectedContentFilter != null && !selectedContentFilter.isEmpty()) { |
| 42 | + final SoundcloudContentFilterItem contentFilter = |
| 43 | + // we assume there is just one content filter |
| 44 | + (SoundcloudContentFilterItem) selectedContentFilter.get(0); |
| 45 | + if (null != contentFilter) { |
| 46 | + return contentFilter.urlEndpoint; |
| 47 | + } |
| 48 | + } |
| 49 | + return ""; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void init() { |
| 54 | + /* content filters */ |
| 55 | + groupsFactory.addFilterItem(new SoundcloudContentFilterItem( |
| 56 | + ID_CF_MAIN_ALL, ALL, "")); |
| 57 | + groupsFactory.addFilterItem(new SoundcloudContentFilterItem( |
| 58 | + ID_CF_MAIN_TRACKS, TRACKS, "/tracks")); |
| 59 | + groupsFactory.addFilterItem(new SoundcloudContentFilterItem( |
| 60 | + ID_CF_MAIN_USERS, USERS, "/users")); |
| 61 | + groupsFactory.addFilterItem(new SoundcloudContentFilterItem( |
| 62 | + ID_CF_MAIN_PLAYLISTS, PLAYLISTS, "/playlists")); |
| 63 | + |
| 64 | + |
| 65 | + /* Sort filters */ |
| 66 | + /* 'Date' filter items */ |
| 67 | + groupsFactory.addFilterItem(new SoundcloudSortFilterItem( |
| 68 | + ID_SF_DATE_ALL, "all", "")); |
| 69 | + groupsFactory.addFilterItem(new SoundcloudSortFilterItem( |
| 70 | + ID_SF_DATE_LAST_HOUR, "Past hour", "filter.created_at=last_hour")); |
| 71 | + groupsFactory.addFilterItem(new SoundcloudSortFilterItem( |
| 72 | + ID_SF_DATE_LAST_DAY, "Past day", "filter.created_at=last_day")); |
| 73 | + groupsFactory.addFilterItem(new SoundcloudSortFilterItem( |
| 74 | + ID_SF_DATE_LAST_WEEK, "Past week", "filter.created_at=last_week")); |
| 75 | + groupsFactory.addFilterItem(new SoundcloudSortFilterItem( |
| 76 | + ID_SF_DATE_LAST_MONTH, "Past month", "filter.created_at=last_month")); |
| 77 | + groupsFactory.addFilterItem(new SoundcloudSortFilterItem( |
| 78 | + ID_SF_DATE_LAST_YEAR, "Past year", "filter.created_at=last_year")); |
| 79 | + |
| 80 | + /* duration' filter items */ |
| 81 | + groupsFactory.addFilterItem(new SoundcloudSortFilterItem( |
| 82 | + ID_SF_DURATION_ALL, "all", "")); |
| 83 | + groupsFactory.addFilterItem(new SoundcloudSortFilterItem( |
| 84 | + ID_SF_DURATION_SHORT, "< 2 min", "filter.duration=short")); |
| 85 | + groupsFactory.addFilterItem(new SoundcloudSortFilterItem( |
| 86 | + ID_SF_DURATION_MEDIUM, "2-10 min", "filter.duration=medium")); |
| 87 | + groupsFactory.addFilterItem(new SoundcloudSortFilterItem( |
| 88 | + ID_SF_DURATION_LONG, "10-30 min", "filter.duration=long")); |
| 89 | + groupsFactory.addFilterItem(new SoundcloudSortFilterItem( |
| 90 | + ID_SF_DURATION_EPIC, "> 30 min", "filter.duration=epic")); |
| 91 | + |
| 92 | + /* license */ |
| 93 | + groupsFactory.addFilterItem(new SoundcloudSortFilterItem( |
| 94 | + ID_SF_LICENSE_ALL, "all", "")); |
| 95 | + groupsFactory.addFilterItem(new SoundcloudSortFilterItem( |
| 96 | + ID_SF_LICENSE_COMMERCE, "To modify commercially", |
| 97 | + "filter.license=to_modify_commercially")); |
| 98 | + |
| 99 | + final FilterContainer allMainCFGrpSortFilters = new FilterContainer(new FilterGroup[]{ |
| 100 | + groupsFactory.createFilterGroup(ID_SF_DATE_GRP, "Sort by", true, |
| 101 | + ID_SF_DATE_ALL, new FilterItem[]{ |
| 102 | + groupsFactory.getFilterForId(ID_SF_DATE_ALL), |
| 103 | + groupsFactory.getFilterForId(ID_SF_DATE_LAST_HOUR), |
| 104 | + groupsFactory.getFilterForId(ID_SF_DATE_LAST_DAY), |
| 105 | + groupsFactory.getFilterForId(ID_SF_DATE_LAST_WEEK), |
| 106 | + groupsFactory.getFilterForId(ID_SF_DATE_LAST_MONTH), |
| 107 | + groupsFactory.getFilterForId(ID_SF_DATE_LAST_YEAR), |
| 108 | + }, null), |
| 109 | + groupsFactory.createFilterGroup(ID_SF_DURATION_GRP, "Length", true, |
| 110 | + ID_SF_DURATION_ALL, new FilterItem[]{ |
| 111 | + groupsFactory.getFilterForId(ID_SF_DURATION_ALL), |
| 112 | + groupsFactory.getFilterForId(ID_SF_DURATION_SHORT), |
| 113 | + groupsFactory.getFilterForId(ID_SF_DURATION_MEDIUM), |
| 114 | + groupsFactory.getFilterForId(ID_SF_DURATION_LONG), |
| 115 | + groupsFactory.getFilterForId(ID_SF_DURATION_EPIC), |
| 116 | + }, null), |
| 117 | + groupsFactory.createFilterGroup(ID_SF_LICENSE_GRP, "License", true, |
| 118 | + ID_SF_LICENSE_ALL, new FilterItem[]{ |
| 119 | + groupsFactory.getFilterForId(ID_SF_LICENSE_ALL), |
| 120 | + groupsFactory.getFilterForId(ID_SF_LICENSE_COMMERCE), |
| 121 | + }, null), |
| 122 | + }); |
| 123 | + |
| 124 | + /* content filters */ |
| 125 | + addContentFilterGroup(groupsFactory.createFilterGroup(ID_CF_MAIN_GRP, null, true, |
| 126 | + ID_CF_MAIN_ALL, new FilterItem[]{ |
| 127 | + groupsFactory.getFilterForId(ID_CF_MAIN_ALL), |
| 128 | + groupsFactory.getFilterForId(ID_CF_MAIN_TRACKS), |
| 129 | + groupsFactory.getFilterForId(ID_CF_MAIN_USERS), |
| 130 | + groupsFactory.getFilterForId(ID_CF_MAIN_PLAYLISTS), |
| 131 | + }, allMainCFGrpSortFilters)); |
| 132 | + |
| 133 | + addContentFilterSortVariant(ID_CF_MAIN_TRACKS, allMainCFGrpSortFilters); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public String evaluateSelectedSortFilters() { |
| 138 | + final StringBuilder sortQuery = new StringBuilder(); |
| 139 | + if (selectedSortFilter != null) { |
| 140 | + for (final FilterItem item : selectedSortFilter) { |
| 141 | + final SoundcloudSortFilterItem sortItem = |
| 142 | + (SoundcloudSortFilterItem) item; |
| 143 | + if (sortItem != null && !sortItem.query.isEmpty()) { |
| 144 | + sortQuery.append("&").append(sortItem.query); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return sortQuery.toString(); |
| 150 | + } |
| 151 | + |
| 152 | + private static class SoundcloudSortFilterItem extends FilterItem { |
| 153 | + private final String query; |
| 154 | + |
| 155 | + SoundcloudSortFilterItem(final int identifier, final String name, final String query) { |
| 156 | + super(identifier, name); |
| 157 | + this.query = query; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private static final class SoundcloudContentFilterItem extends FilterItem { |
| 162 | + private final String urlEndpoint; |
| 163 | + |
| 164 | + private SoundcloudContentFilterItem(final int identifier, final String name, |
| 165 | + final String urlEndpoint) { |
| 166 | + super(identifier, name); |
| 167 | + this.urlEndpoint = urlEndpoint; |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments