it all seems to work somewhat well
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package nl.herpiederpiee.appie_scraper;
|
||||
|
||||
import com.microsoft.playwright.*;
|
||||
import xyz.nextn.levenshteindistance.LevenshteinDistance;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class BonusManager {
|
||||
ArrayList<BonusItem> bonusItems = new ArrayList<BonusItem>();;
|
||||
|
||||
public void updateBonusItems(){
|
||||
try (Playwright playwright = Playwright.create()) {
|
||||
|
||||
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
|
||||
|
||||
BrowserContext context = browser.newContext();
|
||||
Page bonusPagina = context.newPage();
|
||||
|
||||
|
||||
bonusPagina.navigate("https://www.ah.nl/bonus");
|
||||
TimeUnit.SECONDS.sleep(5); // wait for page to actaully fully load
|
||||
|
||||
Locator bonusElements = bonusPagina.locator(".promotion-card_root__tQA3z");
|
||||
for (ElementHandle bonusElement : bonusElements.elementHandles()){
|
||||
BonusItem bonusItem = new BonusItem(bonusElement);
|
||||
|
||||
// exclude annoying elements
|
||||
if (bonusItem.category.equals( "onlineOnly")) continue;
|
||||
if (bonusItem.category.equals( "gall")) continue;
|
||||
if (bonusItem.category.equals( "gall-card")) continue;
|
||||
if (bonusItem.category.equals( "etos")) continue;
|
||||
|
||||
this.bonusItems.add(bonusItem);
|
||||
}
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<BonusItem> getBonusItems(String name){
|
||||
ArrayList<Pair<BonusItem, Integer>> list = new ArrayList<>();
|
||||
|
||||
for (BonusItem bonusItem : bonusItems) {
|
||||
Integer score = fuzzyMatchScore(name, bonusItem.title);
|
||||
list.add(Pair.pair(bonusItem, score));
|
||||
}
|
||||
list.sort((a, b) -> Integer.compare(b.second, a.second));
|
||||
|
||||
ArrayList<BonusItem> top10 = new ArrayList<>();
|
||||
int i = 0;
|
||||
while (top10.size() < 10) {
|
||||
top10.add(list.get(i).first);
|
||||
i++;
|
||||
}
|
||||
|
||||
return top10;
|
||||
}
|
||||
|
||||
public int fuzzyMatchScore(String query, String title) {
|
||||
query = query.toLowerCase();
|
||||
title = title.toLowerCase();
|
||||
|
||||
if (title.contains(query)) {
|
||||
return 100; // perfect match
|
||||
}
|
||||
|
||||
int best = Integer.MAX_VALUE;
|
||||
|
||||
int qlen = query.length();
|
||||
int tlen = title.length();
|
||||
|
||||
for (int i = 0; i <= tlen - qlen; i++) {
|
||||
String sub = title.substring(i, i + qlen);
|
||||
int dist = LevenshteinDistance.calculate(query, sub);
|
||||
if (dist < best) best = dist;
|
||||
}
|
||||
|
||||
// Convert distance to similarity percentage
|
||||
int score = (int)(100.0 * (1.0 - (best / (double) qlen)));
|
||||
|
||||
return Math.max(0, Math.min(100, score));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user