From 4d7eccd1e05d06675cc6a9e705d59be71345585f Mon Sep 17 00:00:00 2001 From: jim Date: Mon, 27 Sep 2021 17:45:14 +0100 Subject: [PATCH] WIP: added initial versions of search API. --- pom.xml | 7 +++ .../java/moe/nekojimi/musicsearcher/Main.java | 2 +- .../moe/nekojimi/musicsearcher/Result.java | 14 +++++ .../musicsearcher/providers/MetaSearcher.java | 32 ++++++++++ .../musicsearcher/providers/Searcher.java | 59 +++++++++++++++++++ .../providers/WebScraperSearcher.java | 37 ++++++++++++ 6 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 src/main/java/moe/nekojimi/musicsearcher/Result.java create mode 100644 src/main/java/moe/nekojimi/musicsearcher/providers/MetaSearcher.java create mode 100644 src/main/java/moe/nekojimi/musicsearcher/providers/Searcher.java create mode 100644 src/main/java/moe/nekojimi/musicsearcher/providers/WebScraperSearcher.java diff --git a/pom.xml b/pom.xml index 08adc63..2ed5929 100644 --- a/pom.xml +++ b/pom.xml @@ -35,4 +35,11 @@ + + + org.jsoup + jsoup + 1.14.2 + + \ No newline at end of file diff --git a/src/main/java/moe/nekojimi/musicsearcher/Main.java b/src/main/java/moe/nekojimi/musicsearcher/Main.java index 17ac700..c438b64 100644 --- a/src/main/java/moe/nekojimi/musicsearcher/Main.java +++ b/src/main/java/moe/nekojimi/musicsearcher/Main.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package moe.nekojimi.javamaventemplate; +package moe.nekojimi.musicsearcher; /** * diff --git a/src/main/java/moe/nekojimi/musicsearcher/Result.java b/src/main/java/moe/nekojimi/musicsearcher/Result.java new file mode 100644 index 0000000..9e1becf --- /dev/null +++ b/src/main/java/moe/nekojimi/musicsearcher/Result.java @@ -0,0 +1,14 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package moe.nekojimi.musicsearcher; + +/** + * + * @author jim + */ +public class Result { + +} diff --git a/src/main/java/moe/nekojimi/musicsearcher/providers/MetaSearcher.java b/src/main/java/moe/nekojimi/musicsearcher/providers/MetaSearcher.java new file mode 100644 index 0000000..8952adc --- /dev/null +++ b/src/main/java/moe/nekojimi/musicsearcher/providers/MetaSearcher.java @@ -0,0 +1,32 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package moe.nekojimi.musicsearcher.providers; + +import java.util.List; +import java.util.Set; +import moe.nekojimi.musicsearcher.Result; + +/** + * + * @author jim + */ +public class MetaSearcher extends Searcher +{ + + private final Set searchers; + + public MetaSearcher() + { + super("meta"); + } + + @Override + public List search(String query) + { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + +} diff --git a/src/main/java/moe/nekojimi/musicsearcher/providers/Searcher.java b/src/main/java/moe/nekojimi/musicsearcher/providers/Searcher.java new file mode 100644 index 0000000..41055fa --- /dev/null +++ b/src/main/java/moe/nekojimi/musicsearcher/providers/Searcher.java @@ -0,0 +1,59 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package moe.nekojimi.musicsearcher.providers; + +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import moe.nekojimi.musicsearcher.Result; + +/** + * + * @author jim + */ +public abstract class Searcher +{ + private final String name; + private final ForkJoinPool executor; + + public Searcher(String name) + { + this.name = name; + this.executor = new ForkJoinPool(); + } + + public List searchAndWait(String query) throws InterruptedException, ExecutionException + { + try + { + return searchAndWait(query, Long.MAX_VALUE, TimeUnit.DAYS); + } catch (TimeoutException ex) + { + throw new RuntimeException("This should never happen!",ex); + } + } + + public List searchAndWait(String query, long limit, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException + { + return search(query).get(limit, unit); + } + + public Future> search(String query) + { + FutureTask> task = new FutureTask<>(() -> + { + return doSearch(query); + }); + executor.execute(task); + return task; + } + + protected abstract List doSearch(String query); +} diff --git a/src/main/java/moe/nekojimi/musicsearcher/providers/WebScraperSearcher.java b/src/main/java/moe/nekojimi/musicsearcher/providers/WebScraperSearcher.java new file mode 100644 index 0000000..4b94682 --- /dev/null +++ b/src/main/java/moe/nekojimi/musicsearcher/providers/WebScraperSearcher.java @@ -0,0 +1,37 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package moe.nekojimi.musicsearcher.providers; + +import java.util.List; +import moe.nekojimi.musicsearcher.Result; +import org.jsoup.Jsoup; + +/** + * + * @author jim + */ +public class WebScraperSearcher extends Searcher +{ + private String searchUrl; + + private String resultItem; + private String artistItem; + private String titleItem; + private String linkHrefItem; + private String albumArtistItem; + + public WebScraperSearcher(String name) + { + super(name); + } + + @Override + protected List doSearch(String query) + { + Jsoup. + } + +}