generated from Nekojimi/JavaMavenTemplate
Compare commits
6 Commits
0bdf5d614d
...
4df2f55922
Author | SHA1 | Date |
---|---|---|
|
4df2f55922 | |
|
b8c3972f54 | |
|
eff1f5bfdc | |
|
b10f9d3573 | |
|
7e41eb1105 | |
|
95f2db1926 |
5
pom.xml
5
pom.xml
|
@ -73,6 +73,11 @@
|
||||||
<version>1.1.4</version>
|
<version>1.1.4</version>
|
||||||
<classifier>module</classifier>
|
<classifier>module</classifier>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.12.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<name>MusicSearcher</name>
|
<name>MusicSearcher</name>
|
||||||
</project>
|
</project>
|
|
@ -19,6 +19,7 @@ import java.util.Map;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
import moe.nekojimi.musicsearcher.providers.MetaSearcher;
|
||||||
import moe.nekojimi.musicsearcher.providers.Searcher;
|
import moe.nekojimi.musicsearcher.providers.Searcher;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,39 +36,31 @@ public class Main
|
||||||
public static void main(String[] args) throws IOException
|
public static void main(String[] args) throws IOException
|
||||||
{
|
{
|
||||||
// System.out.println("Hello world!");
|
// System.out.println("Hello world!");
|
||||||
YamlInput input = Yaml.createYamlInput(new File("searchproviders.yml"));
|
|
||||||
YamlSequence seq = input.readYamlSequence();
|
|
||||||
for (int i = 0; i < seq.size(); i++)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
YamlMapping map = seq.yamlMapping(i);
|
|
||||||
String type = map.string("type");
|
|
||||||
Class<? extends Searcher> clazz = (Class<? extends Searcher>) Class.forName("moe.nekojimi.musicsearcher.providers." + type);
|
|
||||||
Constructor<? extends Searcher> constructor = clazz.getConstructor(YamlMapping.class);
|
|
||||||
Searcher searcher = constructor.newInstance(map);
|
|
||||||
searchers.put(searcher.getName(), searcher);
|
|
||||||
|
|
||||||
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
|
|
||||||
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println(searchers);
|
System.out.println(searchers);
|
||||||
String query = "everybodys circulation";
|
String query = "eminem lose yourself";
|
||||||
for (Searcher searcher: searchers.values())
|
|
||||||
{
|
MetaSearcher metaSearch = MetaSearcher.loadYAML(new File("searchproviders.yml"));
|
||||||
System.out.println("Searching " + searcher.getName() + " for " + query);
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
List<Result> results = searcher.searchAndWait(Query.fullText(query));
|
List<Result> results = metaSearch.searchAndWait(Query.fullText(query));
|
||||||
for (Result result: results)
|
// for (Searcher searcher: searchers.values())
|
||||||
{
|
// {
|
||||||
|
// System.out.println("Searching " + searcher.getName() + " for " + query);
|
||||||
|
// try
|
||||||
|
// {
|
||||||
|
// List<Result> results = searcher.searchAndWait(Query.fullText(query));
|
||||||
|
for (Result result : results)
|
||||||
System.out.println("\t" + result);
|
System.out.println("\t" + result);
|
||||||
}
|
// } catch (InterruptedException | ExecutionException ex) {
|
||||||
} catch (InterruptedException | ExecutionException ex) {
|
// Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
} catch (ExecutionException | InterruptedException ex)
|
||||||
|
{
|
||||||
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,13 +11,18 @@ import java.net.URL;
|
||||||
*
|
*
|
||||||
* @author jim
|
* @author jim
|
||||||
*/
|
*/
|
||||||
public class Result
|
public class Result implements Comparable<Result>
|
||||||
{
|
{
|
||||||
private URL link;
|
private URL link;
|
||||||
private String artist;
|
private String artist;
|
||||||
private String album;
|
private String album;
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
|
private String sourceName;
|
||||||
|
private String sourceAbbr;
|
||||||
|
|
||||||
|
private double score;
|
||||||
|
|
||||||
public URL getLink() {
|
public URL getLink() {
|
||||||
return link;
|
return link;
|
||||||
}
|
}
|
||||||
|
@ -50,9 +55,36 @@ public class Result
|
||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSourceName()
|
||||||
|
{
|
||||||
|
return sourceName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSource(String name, String abbr)
|
||||||
|
{
|
||||||
|
sourceName = name;
|
||||||
|
sourceAbbr = abbr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSourceAbbr()
|
||||||
|
{
|
||||||
|
return sourceAbbr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getScore()
|
||||||
|
{
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScore(double score)
|
||||||
|
{
|
||||||
|
this.score = score;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString()
|
||||||
return "Result{" + "link=" + link + ", artist=" + artist + ", album=" + album + ", title=" + title + '}';
|
{
|
||||||
|
return "Result{" + "artist=" + artist + ", album=" + album + ", title=" + title + ", sourceAbbr=" + sourceAbbr + ", score=" + score + '}';
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAlbumArtist(String field)
|
public void setAlbumArtist(String field)
|
||||||
|
@ -78,9 +110,17 @@ public class Result
|
||||||
else
|
else
|
||||||
album += word + " ";
|
album += word + " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
artist = artist.trim();
|
||||||
|
album = album.trim();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Result t)
|
||||||
|
{
|
||||||
|
return (int) Math.signum(score - t.score);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,11 +5,26 @@
|
||||||
*/
|
*/
|
||||||
package moe.nekojimi.musicsearcher.providers;
|
package moe.nekojimi.musicsearcher.providers;
|
||||||
|
|
||||||
|
import com.amihaiemil.eoyaml.Yaml;
|
||||||
|
import com.amihaiemil.eoyaml.YamlInput;
|
||||||
|
import com.amihaiemil.eoyaml.YamlMapping;
|
||||||
|
import com.amihaiemil.eoyaml.YamlSequence;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import moe.nekojimi.musicsearcher.Main;
|
||||||
import moe.nekojimi.musicsearcher.Query;
|
import moe.nekojimi.musicsearcher.Query;
|
||||||
import moe.nekojimi.musicsearcher.Result;
|
import moe.nekojimi.musicsearcher.Result;
|
||||||
|
|
||||||
|
@ -24,9 +39,35 @@ public class MetaSearcher extends Searcher
|
||||||
private int minSearchTime = 10000; // ms
|
private int minSearchTime = 10000; // ms
|
||||||
private int maxSearchTime = 30000; // ms
|
private int maxSearchTime = 30000; // ms
|
||||||
|
|
||||||
public MetaSearcher()
|
public MetaSearcher(Collection<Searcher> searchers)
|
||||||
{
|
{
|
||||||
super("meta");
|
super("meta");
|
||||||
|
this.searchers.addAll(searchers);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MetaSearcher loadYAML(File file)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
List<Searcher> searchers = new ArrayList<>();
|
||||||
|
YamlInput input = Yaml.createYamlInput(file);
|
||||||
|
YamlSequence seq = input.readYamlSequence();
|
||||||
|
for (int i = 0; i < seq.size(); i++)
|
||||||
|
{
|
||||||
|
YamlMapping map = seq.yamlMapping(i);
|
||||||
|
String type = map.string("type");
|
||||||
|
Class<? extends Searcher> clazz = (Class<? extends Searcher>) Class.forName("moe.nekojimi.musicsearcher.providers." + type);
|
||||||
|
Constructor<? extends Searcher> constructor = clazz.getConstructor(YamlMapping.class);
|
||||||
|
Searcher searcher = constructor.newInstance(map);
|
||||||
|
searchers.add(searcher);
|
||||||
|
}
|
||||||
|
|
||||||
|
MetaSearcher metaSearch = new MetaSearcher(searchers);
|
||||||
|
return metaSearch;
|
||||||
|
} catch (IOException | ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
|
||||||
|
{
|
||||||
|
throw new IllegalArgumentException("Could not load MetaSearcher config from YAML file " + file.getAbsolutePath(), ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -38,16 +79,26 @@ public class MetaSearcher extends Searcher
|
||||||
{
|
{
|
||||||
CompletableFuture<List<Result>> search = searcher.search(query);
|
CompletableFuture<List<Result>> search = searcher.search(query);
|
||||||
searches.add(search);
|
searches.add(search);
|
||||||
search.whenComplete((t, u) ->
|
// search.whenComplete((t, u) ->
|
||||||
{
|
// {
|
||||||
if (u == null)
|
// if (u == null)
|
||||||
{
|
// {
|
||||||
results.addAll(t);
|
// results.addAll(t);
|
||||||
// searches.remove(search);
|
//// searches.remove(search);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
}
|
}
|
||||||
});
|
// CompletableFuture.allOf((CompletableFuture<?>[]) searches.toArray());
|
||||||
|
for (CompletableFuture<List<Result>> search : searches)
|
||||||
|
try
|
||||||
|
{
|
||||||
|
List<Result> result = search.get(30, TimeUnit.SECONDS);
|
||||||
|
results.addAll(result);
|
||||||
|
} catch (TimeoutException | InterruptedException | ExecutionException ex)
|
||||||
|
{
|
||||||
|
Logger.getLogger(MetaSearcher.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
}
|
}
|
||||||
CompletableFuture.allOf((CompletableFuture<?>[]) searches.toArray());
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,10 @@
|
||||||
package moe.nekojimi.musicsearcher.providers;
|
package moe.nekojimi.musicsearcher.providers;
|
||||||
|
|
||||||
import com.amihaiemil.eoyaml.YamlMapping;
|
import com.amihaiemil.eoyaml.YamlMapping;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.ForkJoinPool;
|
import java.util.concurrent.ForkJoinPool;
|
||||||
|
@ -15,6 +18,7 @@ import java.util.concurrent.TimeoutException;
|
||||||
import moe.nekojimi.musicsearcher.Query;
|
import moe.nekojimi.musicsearcher.Query;
|
||||||
import moe.nekojimi.musicsearcher.QueryFieldUnsupportedException;
|
import moe.nekojimi.musicsearcher.QueryFieldUnsupportedException;
|
||||||
import moe.nekojimi.musicsearcher.Result;
|
import moe.nekojimi.musicsearcher.Result;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -22,18 +26,21 @@ import moe.nekojimi.musicsearcher.Result;
|
||||||
*/
|
*/
|
||||||
public abstract class Searcher
|
public abstract class Searcher
|
||||||
{
|
{
|
||||||
final String name;
|
protected final String name;
|
||||||
|
protected String abbr;
|
||||||
private final ForkJoinPool executor;
|
private final ForkJoinPool executor;
|
||||||
|
|
||||||
public Searcher(String name)
|
public Searcher(String name)
|
||||||
{
|
{
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.abbr = name.substring(0, 2);
|
||||||
this.executor = new ForkJoinPool();
|
this.executor = new ForkJoinPool();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Searcher(YamlMapping yaml)
|
public Searcher(YamlMapping yaml)
|
||||||
{
|
{
|
||||||
this(yaml.string("name"));
|
this(yaml.string("name"));
|
||||||
|
abbr = yaml.string("abbr");
|
||||||
assert yaml.string("type").equals(this.getClass().getSimpleName());
|
assert yaml.string("type").equals(this.getClass().getSimpleName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,10 +68,41 @@ public abstract class Searcher
|
||||||
public CompletableFuture<List<Result>> search(Query query)
|
public CompletableFuture<List<Result>> search(Query query)
|
||||||
{
|
{
|
||||||
CompletableFuture<List<Result>> future = new CompletableFuture<>();
|
CompletableFuture<List<Result>> future = new CompletableFuture<>();
|
||||||
future.completeAsync(()->doSearch(query), executor);
|
future.completeAsync(() -> searchWrapper(query), executor);
|
||||||
return future;
|
return future;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected List<Result> searchWrapper(Query query)
|
||||||
|
{
|
||||||
|
List<Result> results = doSearch(query);
|
||||||
|
|
||||||
|
for (Result result : results)
|
||||||
|
{
|
||||||
|
double artistScore = 1;
|
||||||
|
double titleScore = 1;
|
||||||
|
double albumScore = 1;
|
||||||
|
double fullTextScore = 0;
|
||||||
|
|
||||||
|
Set<String> toCompare = new HashSet<>();
|
||||||
|
toCompare.add(result.getAlbum());
|
||||||
|
toCompare.add(result.getArtist());
|
||||||
|
toCompare.add(result.getTitle());
|
||||||
|
|
||||||
|
for (String comparison : toCompare)
|
||||||
|
{
|
||||||
|
if (comparison == null)
|
||||||
|
continue;
|
||||||
|
double score = StringUtils.getJaroWinklerDistance(query.getTextSearch(), comparison);
|
||||||
|
if (score > fullTextScore)
|
||||||
|
fullTextScore = score;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.setScore(albumScore * artistScore * titleScore * fullTextScore);
|
||||||
|
}
|
||||||
|
Collections.sort(results, Collections.reverseOrder());
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract List<Result> doSearch(Query query) throws QueryFieldUnsupportedException;
|
protected abstract List<Result> doSearch(Query query) throws QueryFieldUnsupportedException;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -142,19 +142,20 @@ public class WebScraperSearcher extends Searcher
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Result res = new Result();
|
Result res = new Result();
|
||||||
|
res.setSource(name, abbr);
|
||||||
// Artist
|
// Artist
|
||||||
if (resultArtistSelector != null)
|
if (resultArtistSelector != null)
|
||||||
res.setArtist(parser.getField(ele, resultArtistSelector));
|
res.setArtist(parser.getField(ele, resultArtistSelector).trim());
|
||||||
// Title
|
// Title
|
||||||
if (resultTitleSelector != null)
|
if (resultTitleSelector != null)
|
||||||
res.setTitle(parser.getField(ele, resultTitleSelector));
|
res.setTitle(parser.getField(ele, resultTitleSelector).trim());
|
||||||
// Link
|
// Link
|
||||||
if (resultLinkSelector != null)
|
if (resultLinkSelector != null)
|
||||||
res.setLink(parser.getURLField(ele, rootURL, resultLinkSelector));
|
res.setLink(parser.getURLField(ele, rootURL, resultLinkSelector));
|
||||||
|
|
||||||
// Artist + Album
|
// Artist + Album
|
||||||
if (resultAlbumArtistSelector != null)
|
if (resultAlbumArtistSelector != null)
|
||||||
res.setAlbumArtist(parser.getField(ele, resultAlbumArtistSelector));
|
res.setAlbumArtist(parser.getField(ele, resultAlbumArtistSelector).trim());
|
||||||
|
|
||||||
// Artist + Title
|
// Artist + Title
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue