2019-12-22 13:28:02 +01:00
|
|
|
// Created by Fynn Godau 2019, licensed GNU GPL version 3 or later
|
|
|
|
|
|
|
|
package org.schabi.newpipe.extractor.services.bandcamp.extractors;
|
|
|
|
|
2020-03-19 11:18:29 +01:00
|
|
|
import com.grack.nanojson.JsonArray;
|
|
|
|
import com.grack.nanojson.JsonObject;
|
|
|
|
import com.grack.nanojson.JsonParser;
|
|
|
|
import com.grack.nanojson.JsonParserException;
|
2019-12-22 13:28:02 +01:00
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
|
|
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class BandcampSuggestionExtractor extends SuggestionExtractor {
|
|
|
|
|
|
|
|
private static final String AUTOCOMPLETE_URL = "https://bandcamp.com/api/fuzzysearch/1/autocomplete?q=";
|
|
|
|
public BandcampSuggestionExtractor(StreamingService service) {
|
|
|
|
super(service);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<String> suggestionList(String query) throws IOException, ExtractionException {
|
|
|
|
Downloader downloader = NewPipe.getDownloader();
|
|
|
|
|
2020-03-19 11:18:29 +01:00
|
|
|
try {
|
|
|
|
JsonObject fuzzyResults = JsonParser.object().from(
|
2020-04-20 23:03:46 +02:00
|
|
|
downloader.get(AUTOCOMPLETE_URL + URLEncoder.encode(query, "UTF-8")).responseBody()
|
2020-03-19 11:18:29 +01:00
|
|
|
);
|
2019-12-22 13:28:02 +01:00
|
|
|
|
2020-03-19 11:18:29 +01:00
|
|
|
JsonArray jsonArray = fuzzyResults.getObject("auto")
|
|
|
|
.getArray("results");
|
2019-12-22 13:28:02 +01:00
|
|
|
|
2020-03-19 11:18:29 +01:00
|
|
|
ArrayList<String> suggestions = new ArrayList<>();
|
2019-12-22 13:28:02 +01:00
|
|
|
|
2020-04-26 23:02:14 +02:00
|
|
|
for (Object fuzzyResult : jsonArray) {
|
|
|
|
String res = ((JsonObject) fuzzyResult).getString("name");
|
2019-12-22 13:28:02 +01:00
|
|
|
|
2020-03-19 11:18:29 +01:00
|
|
|
if (!suggestions.contains(res)) suggestions.add(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return suggestions;
|
|
|
|
} catch (JsonParserException e) {
|
|
|
|
e.printStackTrace();
|
2019-12-22 13:28:02 +01:00
|
|
|
|
2020-03-19 11:18:29 +01:00
|
|
|
return new ArrayList<>();
|
|
|
|
}
|
2019-12-22 13:28:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|