From ce2a88e56ff2e7c819429b8fff55f20775a5706d Mon Sep 17 00:00:00 2001 From: Fynn Godau Date: Sun, 22 Dec 2019 13:28:02 +0100 Subject: [PATCH] Add bandcamp search suggestion extractor --- .../services/bandcamp/BandcampService.java | 7 +-- .../BandcampSuggestionExtractor.java | 51 +++++++++++++++++++ .../BandcampSuggestionExtractorTest.java | 41 +++++++++++++++ 3 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampSuggestionExtractor.java create mode 100644 extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampSuggestionExtractorTest.java diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampService.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampService.java index 211f7310..103ff6ec 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampService.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampService.java @@ -10,10 +10,7 @@ import org.schabi.newpipe.extractor.kiosk.KioskList; import org.schabi.newpipe.extractor.linkhandler.*; import org.schabi.newpipe.extractor.playlist.PlaylistExtractor; import org.schabi.newpipe.extractor.search.SearchExtractor; -import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampChannelExtractor; -import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampPlaylistExtractor; -import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampSearchExtractor; -import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampStreamExtractor; +import org.schabi.newpipe.extractor.services.bandcamp.extractors.*; import org.schabi.newpipe.extractor.services.bandcamp.linkHandler.BandcampChannelLinkHandlerFactory; import org.schabi.newpipe.extractor.services.bandcamp.linkHandler.BandcampPlaylistLinkHandlerFactory; import org.schabi.newpipe.extractor.services.bandcamp.linkHandler.BandcampSearchQueryHandlerFactory; @@ -69,7 +66,7 @@ public class BandcampService extends StreamingService { @Override public SuggestionExtractor getSuggestionExtractor() { - return null; + return new BandcampSuggestionExtractor(this); } @Override diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampSuggestionExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampSuggestionExtractor.java new file mode 100644 index 00000000..6cf5d70b --- /dev/null +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampSuggestionExtractor.java @@ -0,0 +1,51 @@ +// Created by Fynn Godau 2019, licensed GNU GPL version 3 or later + +package org.schabi.newpipe.extractor.services.bandcamp.extractors; + +import org.json.JSONArray; +import org.json.JSONObject; +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; + +import static org.schabi.newpipe.extractor.services.bandcamp.linkHandler.BandcampSearchQueryHandlerFactory.CHARSET_UTF_8; + +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 suggestionList(String query) throws IOException, ExtractionException { + Downloader downloader = NewPipe.getDownloader(); + + JSONObject fuzzyResults = new JSONObject( + downloader.get(AUTOCOMPLETE_URL + URLEncoder.encode(query, CHARSET_UTF_8)).responseBody() + ); + + JSONArray jsonArray = fuzzyResults.getJSONObject("auto") + .getJSONArray("results"); + + ArrayList suggestions = new ArrayList<>(); + + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject fuzzyResult = jsonArray.getJSONObject(i); + String res = fuzzyResult.getString("name"); + + if (!suggestions.contains(res)) suggestions.add(res); + } + + + + return suggestions; + } +} diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampSuggestionExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampSuggestionExtractorTest.java new file mode 100644 index 00000000..77b4f26b --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampSuggestionExtractorTest.java @@ -0,0 +1,41 @@ +// Created by Fynn Godau 2019, licensed GNU GPL version 3 or later + +package org.schabi.newpipe.extractor.services.bandcamp; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.schabi.newpipe.DownloaderTestImpl; +import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampSuggestionExtractor; + +import java.io.IOException; +import java.util.List; + +import static org.junit.Assert.assertTrue; +import static org.schabi.newpipe.extractor.ServiceList.bandcamp; + +/** + * Tests for {@link BandcampSuggestionExtractor} + */ +public class BandcampSuggestionExtractorTest { + + private static BandcampSuggestionExtractor extractor; + + @BeforeClass + public static void setUp() { + NewPipe.init(DownloaderTestImpl.getInstance()); + extractor = (BandcampSuggestionExtractor) bandcamp.getSuggestionExtractor(); + } + + @Test + public void testSearchExample() throws IOException, ExtractionException { + List c418 = extractor.suggestionList("c418"); + + // When looking for c418, one should find C418 + assertTrue(c418.contains("C418")); + + // There should be five results, but we can't be sure of that forever + assertTrue(c418.size() > 2); + } +}