generated from Nekojimi/JavaMavenTemplate
Compare commits
No commits in common. "3ceab2659baad195992f4f34e2c0857f06da686a" and "4df2f55922c09cd6b11cc5d63abfb74ccba3be5a" have entirely different histories.
3ceab2659b
...
4df2f55922
|
@ -92,7 +92,7 @@ public abstract class Searcher
|
|||
{
|
||||
if (comparison == null)
|
||||
continue;
|
||||
double score = StringUtils.getJaroWinklerDistance(query.getTextSearch().toLowerCase(), comparison.toLowerCase());
|
||||
double score = StringUtils.getJaroWinklerDistance(query.getTextSearch(), comparison);
|
||||
if (score > fullTextScore)
|
||||
fullTextScore = score;
|
||||
}
|
||||
|
|
|
@ -9,18 +9,13 @@ import com.amihaiemil.eoyaml.YamlMapping;
|
|||
import com.amihaiemil.eoyaml.YamlNode;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.Authenticator;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -47,22 +42,13 @@ public class WebScraperSearcher extends Searcher
|
|||
protected String resultLinkSelector;
|
||||
protected String resultAlbumArtistSelector;
|
||||
|
||||
protected String linkFormat;
|
||||
|
||||
protected Map<String,String> searchFields = new HashMap<>();
|
||||
protected Map<String, String> searchHeaders = new HashMap<>();
|
||||
|
||||
protected Parser<?,?> parser;
|
||||
|
||||
private final HttpClient client;
|
||||
|
||||
public WebScraperSearcher(String name)
|
||||
{
|
||||
super(name);
|
||||
client = HttpClient.newBuilder()
|
||||
.followRedirects(HttpClient.Redirect.ALWAYS)
|
||||
.authenticator(Authenticator.getDefault())
|
||||
.build();
|
||||
}
|
||||
|
||||
public WebScraperSearcher(YamlMapping yaml) throws MalformedURLException
|
||||
|
@ -84,18 +70,8 @@ public class WebScraperSearcher extends Searcher
|
|||
}
|
||||
|
||||
YamlMapping searchFieldMap = yaml.yamlMapping("search_fields");
|
||||
if (searchFieldMap != null)
|
||||
{
|
||||
for (YamlNode key: searchFieldMap.keys())
|
||||
searchFields.put(key.asScalar().value(), searchFieldMap.string(key));
|
||||
}
|
||||
|
||||
YamlMapping searchHeaderMap = yaml.yamlMapping("search_headers");
|
||||
if (searchHeaderMap != null)
|
||||
{
|
||||
for (YamlNode key : searchHeaderMap.keys())
|
||||
searchHeaders.put(key.asScalar().value(), searchHeaderMap.string(key));
|
||||
}
|
||||
|
||||
String formatName = yaml.string("format");
|
||||
switch(formatName)
|
||||
|
@ -107,13 +83,6 @@ public class WebScraperSearcher extends Searcher
|
|||
default:
|
||||
throw new IllegalArgumentException("Format " + formatName + " is unknown.");
|
||||
}
|
||||
|
||||
linkFormat = yaml.string("link_format");
|
||||
|
||||
client = HttpClient.newBuilder()
|
||||
.followRedirects(HttpClient.Redirect.ALWAYS)
|
||||
// .authenticator(Authenticator.getDefault())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -122,19 +91,9 @@ public class WebScraperSearcher extends Searcher
|
|||
try
|
||||
{
|
||||
URL url = fillURL(query);
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder(url.toURI()).GET();
|
||||
|
||||
for (Entry<String, String> header : searchHeaders.entrySet())
|
||||
builder = builder.header(header.getKey(), fillHeader(header.getValue()));
|
||||
|
||||
HttpRequest request = builder.build();
|
||||
HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
|
||||
|
||||
if (response.statusCode() == 200)
|
||||
return processResults(parser, response.body());
|
||||
else
|
||||
throw new RuntimeException("Got status code " + response.statusCode() + new String(response.body().readAllBytes()));
|
||||
} catch (IOException | URISyntaxException | InterruptedException ex)
|
||||
InputStream input = url.openStream();
|
||||
return processResults(parser, input);
|
||||
} catch (IOException ex)
|
||||
{
|
||||
Logger.getLogger(WebScraperSearcher.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return List.of();
|
||||
|
@ -154,7 +113,7 @@ public class WebScraperSearcher extends Searcher
|
|||
}
|
||||
if (searchFields.containsKey("secret"))
|
||||
{
|
||||
builder.addParameter(searchFields.get("secret"), getSecret());
|
||||
builder.addParameter(searchFields.get("secret"), SecretStore.get().getSecret(name));
|
||||
}
|
||||
return builder.build().toURL();
|
||||
}
|
||||
|
@ -164,20 +123,6 @@ public class WebScraperSearcher extends Searcher
|
|||
}
|
||||
}
|
||||
|
||||
private String getSecret()
|
||||
{
|
||||
return SecretStore.get().getSecret(name);
|
||||
}
|
||||
|
||||
private String fillHeader(String value)
|
||||
{
|
||||
String ret = value;
|
||||
if (ret.contains("$SECRET"))
|
||||
ret = ret.replaceAll("\\$SECRET", getSecret());
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected String transformSearchString(String search)
|
||||
{
|
||||
return search;
|
||||
|
@ -206,12 +151,7 @@ public class WebScraperSearcher extends Searcher
|
|||
res.setTitle(parser.getField(ele, resultTitleSelector).trim());
|
||||
// Link
|
||||
if (resultLinkSelector != null)
|
||||
{
|
||||
if (linkFormat == null)
|
||||
res.setLink(parser.getURLField(ele, rootURL, resultLinkSelector));
|
||||
else
|
||||
res.setLink(new URL(linkFormat.replaceAll("\\$LINK", parser.getField(ele, resultLinkSelector))));
|
||||
}
|
||||
|
||||
// Artist + Album
|
||||
if (resultAlbumArtistSelector != null)
|
||||
|
|
Loading…
Reference in New Issue