diff --git a/pom.xml b/pom.xml index 0eb93ef..c3b03be 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 -org.example +nl.herpiederpiee.appie_scraper AppieBonusScraper 1.0-SNAPSHOT @@ -17,6 +17,11 @@ playwright 1.55.0 + + org.json + json + 20250517 + diff --git a/src/main/java/nl/herpiederpiee/appie_scraper/BonusItem.java b/src/main/java/nl/herpiederpiee/appie_scraper/BonusItem.java new file mode 100644 index 0000000..1f142b3 --- /dev/null +++ b/src/main/java/nl/herpiederpiee/appie_scraper/BonusItem.java @@ -0,0 +1,23 @@ +package nl.herpiederpiee.appie_scraper; + +import com.microsoft.playwright.ElementHandle; + +public class BonusItem { + String title; + String bonusText; + String category; + String imageURL; + + BonusItem(ElementHandle element) { + this.title = element.getAttribute("title"); + + ElementHandle bonusElement = element.querySelector(".promotion-label-base_textContainer__DFx6D"); + this.bonusText = bonusElement.innerHTML().replaceAll("<[^>]*>", " "); + + ElementHandle categoryContainer = element.evaluateHandle("el => el.closest('section')").asElement(); + this.category = categoryContainer.getAttribute("id"); + + + this.imageURL = element.querySelector(".promotion-card-image_img__Ca5n8").getAttribute("data-src"); + } +} diff --git a/src/main/java/nl/herpiederpiee/appie_scraper/Main.java b/src/main/java/nl/herpiederpiee/appie_scraper/Main.java new file mode 100644 index 0000000..dc733f9 --- /dev/null +++ b/src/main/java/nl/herpiederpiee/appie_scraper/Main.java @@ -0,0 +1,46 @@ +package nl.herpiederpiee.appie_scraper; +import com.microsoft.playwright.*; +import com.microsoft.playwright.options.WaitUntilState; +import org.json.*; + +import java.util.ArrayList; +import java.util.Random; +import java.util.concurrent.TimeUnit; + +public class Main { + + + public static void main(String[] args) { + 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"); + System.out.println("Dom Content Loaded!"); + + TimeUnit.SECONDS.sleep(5); + ArrayList bonusItems = new ArrayList(); + int counter = 0; + Locator bonusElements = bonusPagina.locator(".promotion-card_root__tQA3z"); + for (ElementHandle bonusElement : bonusElements.elementHandles()){ + BonusItem bonusItem = new BonusItem(bonusElement); + bonusItems.add(bonusItem); + counter++; + } + System.out.println("Amount of items: " + counter); + // get random item from array + Random random = new Random(); + BonusItem chosenItem = bonusItems.get(random.nextInt(bonusItems.size())); + System.out.println("Random Item:\n"+chosenItem.title+" => "+chosenItem.bonusText + " ("+chosenItem.category+")\nImage URL:"+chosenItem.imageURL); + + + + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java deleted file mode 100644 index 3431bd5..0000000 --- a/src/main/java/org/example/Main.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.example; - -//TIP To Run code, press or -// click the icon in the gutter. -public class Main { - public static void main(String[] args) { - - } -}