Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 음식 프리뷰 이미지 추가 #33

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/main/java/com/gdsc/goodeat/domain/FoodScraper.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public List<FoodInfo> scrape(final List<String> foodList) {
List<FoodInfo> foodInfoList = new ArrayList<>();

for (String foodName : foodList) {
FoodInfo foodInfo;
try {
driver.get(TASTEATLAS_URL);

Expand All @@ -51,17 +52,22 @@ public List<FoodInfo> scrape(final List<String> foodList) {

// get search result & enter page
WebElement searchResult = getSearchResult();
System.out.println(searchResult.getAttribute("src").replace("?mw=150", ""));
String preview = searchResult.getAttribute("src").replace("?mw=150", "");
searchResult.click();
driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICIT_WAIT_DURATION);

// extract info
foodInfoList.add(extractFoodInfo());
foodInfo = extractFoodInfo();
foodInfo.setPreviewImage(preview);
} catch (FoodException e) {
foodInfoList.add(new FoodInfo(FOOD_IMG_NOT_FOUND_URL, ""));
foodInfo = new FoodInfo(FOOD_IMG_NOT_FOUND_URL, FOOD_IMG_NOT_FOUND_URL, "");
} catch (NoSuchElementException e) {
driver.quit();
throw new FoodException(FOOD_SCRAPING_FAILED);
}

foodInfoList.add(foodInfo);
}

driver.quit();
Expand All @@ -81,7 +87,7 @@ private FoodInfo extractFoodInfo() {
FoodInfo foodInfo = new FoodInfo();

try {
foodInfo.setImage(driver.findElement(By.cssSelector(IMAGE_SELECTOR)).getAttribute("src"));
foodInfo.setImage(driver.findElement(By.cssSelector(IMAGE_SELECTOR)).getAttribute("src").replace("?mw=1300", ""));
} catch (NoSuchElementException e) {
foodInfo.setImage(FOOD_IMG_NOT_FOUND_URL);
}
Expand All @@ -101,6 +107,7 @@ private FoodInfo extractFoodInfo() {
public static class FoodInfo {

private String image;
private String previewImage;
private String description;

public FoodInfo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private List<FoodInfo> createFoodInfos(
return foodScraper.scrape(menuItemNames).stream()
.map(foodInfo -> new FoodInfo(
foodInfo.getImage(),
foodInfo.getPreviewImage(),
translationClient.translate(Language.ENGLISH, userLanguage, foodInfo.getDescription())
)).toList();
}
Expand Down
Loading