-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from SproutMJ/develop
수정사항 적용
- Loading branch information
Showing
11 changed files
with
151 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ public enum ChatType { | |
menu, | ||
ingredient, | ||
recipe, | ||
imageUrl, | ||
link | ||
} |
25 changes: 25 additions & 0 deletions
25
backend/src/main/java/hello/aimju/controller/CrawlingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package hello.aimju.controller; | ||
|
||
import hello.aimju.image.Crawling.Service.CrawlingService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.io.IOException; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping(value = "/api") | ||
public class CrawlingController { | ||
|
||
private final CrawlingService crawlingService; | ||
|
||
@GetMapping("/menu-image/{query}") | ||
public String fetchImage(@PathVariable("query") String query) { | ||
try { | ||
return crawlingService.fetchImageUrl(query); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return "Error: Unable to fetch image"; | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
backend/src/main/java/hello/aimju/image/Crawling/Service/CrawlingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package hello.aimju.image.Crawling.Service; | ||
|
||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
@Service | ||
public class CrawlingService { | ||
|
||
public String fetchImageUrl(String query) throws IOException { | ||
// 쿼리 인코딩 | ||
String encodedQuery = URLEncoder.encode(query, StandardCharsets.UTF_8.toString()); | ||
System.out.println("Original query: " + query); | ||
System.out.println("Encoded query: " + encodedQuery); | ||
|
||
// URL 생성 | ||
String url = "https://www.10000recipe.com/recipe/list.html?q=" + encodedQuery; | ||
System.out.println("URL: " + url); | ||
|
||
// 페이지 가져오기 | ||
Document doc = Jsoup.connect(url) | ||
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36") | ||
.get(); | ||
System.out.println("Document fetched"); | ||
|
||
// 이미지 URL 추출 | ||
Element imgElement = doc.select("div.common_sp_thumb img").first(); | ||
if (imgElement != null) { | ||
String imageUrl = imgElement.attr("src"); | ||
System.out.println("Image URL: " + imageUrl); | ||
return imageUrl; | ||
} else { | ||
System.out.println("No image element found"); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters