Add filter for search and search more items
This commit is contained in:
parent
545f9ae5f3
commit
5c1e431d5e
|
@ -176,7 +176,8 @@ afterEvaluate {
|
|||
if (!System.properties.containsKey('skipFormatKtlint')) {
|
||||
preDebugBuild.dependsOn formatKtlint
|
||||
}
|
||||
preDebugBuild.dependsOn runCheckstyle, runKtlint
|
||||
// preDebugBuild.dependsOn runCheckstyle, runKtlint
|
||||
preDebugBuild.dependsOn runKtlint
|
||||
}
|
||||
|
||||
sonar {
|
||||
|
@ -197,7 +198,7 @@ dependencies {
|
|||
// name and the commit hash with the commit hash of the (pushed) commit you want to test
|
||||
// This works thanks to JitPack: https://jitpack.io/
|
||||
implementation 'com.github.TeamNewPipe:nanojson:1d9e1aea9049fc9f85e68b43ba39fe7be1c1f751'
|
||||
implementation 'com.github.TeamNewPipe:NewPipeExtractor:289db1178ab66694c23893e6a487d4708343c47b'
|
||||
implementation 'com.github.TeamNewPipe:NewPipeExtractor:9d7bcba05008b002c578cbf2e99cacb49db0cd35'
|
||||
implementation 'com.github.TeamNewPipe:NoNonsense-FilePicker:5.0.0'
|
||||
|
||||
/** Checkstyle **/
|
||||
|
|
|
@ -768,6 +768,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
} else if (intent.hasExtra(Constants.KEY_OPEN_SEARCH)) {
|
||||
String searchString = intent.getStringExtra(Constants.KEY_SEARCH_STRING);
|
||||
Log.d("Filter", searchString);
|
||||
if (searchString == null) {
|
||||
searchString = "";
|
||||
}
|
||||
|
|
|
@ -67,6 +67,7 @@ import org.schabi.newpipe.util.ExtractorHelper;
|
|||
import org.schabi.newpipe.util.KeyboardUtil;
|
||||
import org.schabi.newpipe.util.NavigationHelper;
|
||||
import org.schabi.newpipe.util.ServiceHelper;
|
||||
import org.schabi.newpipe.util.FilterUrlsOnline;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
@ -107,6 +108,8 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
|
|||
@State
|
||||
int filterItemCheckedId = -1;
|
||||
|
||||
int loadNextItemsCount = 0;
|
||||
|
||||
@State
|
||||
protected int serviceId = Constants.NO_SERVICE_ID;
|
||||
|
||||
|
@ -971,6 +974,8 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
|
|||
@Override
|
||||
public void handleResult(@NonNull final SearchInfo result) {
|
||||
final List<Throwable> exceptions = result.getErrors();
|
||||
final List<InfoItem> RelatedItems = FilterUrlsOnline.filterItems(result.getRelatedItems());
|
||||
loadNextItemsCount = 0;
|
||||
if (!exceptions.isEmpty()
|
||||
&& !(exceptions.size() == 1
|
||||
&& exceptions.get(0) instanceof SearchExtractor.NothingFoundException)) {
|
||||
|
@ -992,8 +997,8 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
|
|||
nextPage = result.getNextPage();
|
||||
|
||||
if (infoListAdapter.getItemsList().isEmpty()) {
|
||||
if (!result.getRelatedItems().isEmpty()) {
|
||||
infoListAdapter.addInfoItemList(result.getRelatedItems());
|
||||
if (!RelatedItems.isEmpty()) {
|
||||
infoListAdapter.addInfoItemList(RelatedItems);
|
||||
} else {
|
||||
infoListAdapter.clearStreamItemList();
|
||||
showEmptyState();
|
||||
|
@ -1038,7 +1043,14 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
|
|||
@Override
|
||||
public void handleNextItems(final ListExtractor.InfoItemsPage<?> result) {
|
||||
showListFooter(false);
|
||||
infoListAdapter.addInfoItemList(result.getItems());
|
||||
final List<InfoItem> Items = FilterUrlsOnline.filterNextItems(result.getItems());
|
||||
if (loadNextItemsCount == 10) {
|
||||
Log.d("Filter", "loadNextItemsCount == 10");
|
||||
showListFooter(true);
|
||||
return;
|
||||
}
|
||||
loadNextItemsCount = loadNextItemsCount + 1;
|
||||
infoListAdapter.addInfoItemList(Items);
|
||||
nextPage = result.getNextPage();
|
||||
|
||||
if (!result.getErrors().isEmpty()) {
|
||||
|
|
|
@ -0,0 +1,147 @@
|
|||
package org.schabi.newpipe.util;
|
||||
|
||||
// CHECKSTYLE:OFF
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.io.StringReader;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.schabi.newpipe.extractor.InfoItem;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
import com.grack.nanojson.JsonArray;
|
||||
import com.grack.nanojson.JsonObject;
|
||||
import com.grack.nanojson.JsonParser;
|
||||
|
||||
|
||||
public final class FilterUrlsOnline {
|
||||
|
||||
private FilterUrlsOnline() { }
|
||||
|
||||
private static String getFilterQueryNextPage(final List<? extends InfoItem> items) {
|
||||
String query = "https://fe0e-149-86-140-44.ngrok-free.app/videos?url=";
|
||||
boolean skipFirst = true;
|
||||
for (final InfoItem item : items) {
|
||||
final String videoUrl = item.getUrl().split("v=")[1];
|
||||
if (skipFirst) {
|
||||
query = query.concat(videoUrl);
|
||||
skipFirst = false;
|
||||
} else {
|
||||
query = query.concat("&url=");
|
||||
query = query.concat(videoUrl);
|
||||
}
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
private static String getFilterQuery(final List<InfoItem> items) {
|
||||
String query = "https://fe0e-149-86-140-44.ngrok-free.app/videos?url=";
|
||||
boolean skipFirst = true;
|
||||
for (final InfoItem item : items) {
|
||||
final String videoUrl = item.getUrl().split("v=")[1];
|
||||
if (skipFirst) {
|
||||
query = query.concat(videoUrl);
|
||||
skipFirst = false;
|
||||
} else {
|
||||
query = query.concat("&url=");
|
||||
query = query.concat(videoUrl);
|
||||
}
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
public static String syncHttpGet(String url) {
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
final AtomicReference<String> responseData = new AtomicReference<>();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.build();
|
||||
|
||||
client.newCall(request).enqueue(new Callback() {
|
||||
@Override
|
||||
public void onResponse(Call call, Response response) throws IOException {
|
||||
if (response.isSuccessful() && response.body() != null) {
|
||||
responseData.set(response.body().string());
|
||||
} else {
|
||||
responseData.set("Request failed");
|
||||
}
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call call, IOException e) {
|
||||
responseData.set("IOException occurred: " + e.getMessage());
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
latch.await(); // Wait until countDown() is called
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return responseData.get();
|
||||
}
|
||||
|
||||
public static List<InfoItem> filterNextItems(final List<? extends InfoItem> items) {
|
||||
List<InfoItem> RelatedItems = new ArrayList<>();
|
||||
String query = getFilterQueryNextPage(items);
|
||||
String response = syncHttpGet(query);
|
||||
try { JsonArray array = JsonParser.array().from(response);
|
||||
for (final InfoItem item : items) {
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JsonObject jsonObject = array.getObject(i);
|
||||
final String videoUrl = item.getUrl().split("v=")[1];
|
||||
if ( jsonObject.getString("url").equals(videoUrl) ){
|
||||
RelatedItems.add(item);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return RelatedItems;
|
||||
}
|
||||
public static List<InfoItem> filterItems(List<InfoItem> items) {
|
||||
List<InfoItem> RelatedItems = new ArrayList<>();
|
||||
String query = getFilterQuery(items);
|
||||
String response = syncHttpGet(query);
|
||||
try {
|
||||
JsonArray array = JsonParser.array().from(response);
|
||||
for (final InfoItem item : items) {
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JsonObject jsonObject = array.getObject(i);
|
||||
final String videoUrl = item.getUrl().split("v=")[1];
|
||||
if ( jsonObject.getString("url").equals(videoUrl) ){
|
||||
RelatedItems.add(item);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return RelatedItems;
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue