-
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.
- Loading branch information
Showing
4 changed files
with
150 additions
and
1 deletion.
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
36 changes: 36 additions & 0 deletions
36
src/main/java/kr/inuappcenterportal/inuportal/controller/CafeteriaController.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,36 @@ | ||
package kr.inuappcenterportal.inuportal.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import kr.inuappcenterportal.inuportal.dto.NoticeListResponseDto; | ||
import kr.inuappcenterportal.inuportal.dto.ResponseDto; | ||
import kr.inuappcenterportal.inuportal.service.CafeteriaService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
@Tag(name="Cafeterias", description = "학식 API") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@CrossOrigin(origins = "*") | ||
@RequestMapping("/api/cafeterias") | ||
public class CafeteriaController { | ||
private final CafeteriaService cafeteriaService; | ||
@Operation(summary = "학식 메뉴 가져오기",description = "url 파라미터에 식당 이름을 보내주세요.(학생식당, 제1기숙사식당, 2호관(교직원)식당, 27호관식당, 사범대식당)") | ||
@ApiResponses({ | ||
@ApiResponse(responseCode = "200",description = "학식 메뉴 가져오기 성공",content = @Content(schema = @Schema(implementation = NoticeListResponseDto.class))), | ||
}) | ||
@GetMapping("") | ||
public ResponseEntity<ResponseDto<List<String>>> getMenu(@RequestParam String cafeteria){ | ||
return new ResponseEntity<>(new ResponseDto<>(cafeteriaService.getCafeteria(cafeteria),"학식 메뉴 가져오기 성공"), HttpStatus.OK); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
src/main/java/kr/inuappcenterportal/inuportal/service/CafeteriaService.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,102 @@ | ||
package kr.inuappcenterportal.inuportal.service; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import lombok.RequiredArgsConstructor; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.WebElement; | ||
import org.openqa.selenium.chrome.ChromeDriver; | ||
import org.openqa.selenium.chrome.ChromeOptions; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CafeteriaService { | ||
private final RedisService redisService; | ||
private final String url = "https://www.inu.ac.kr/inu/643/subview.do"; | ||
@Value("${installPath}") | ||
private String installPath; | ||
|
||
@PostConstruct | ||
public void initCafeteria() throws InterruptedException { | ||
crawlingCafeteria(); | ||
} | ||
|
||
@Scheduled(cron = "0 5 0 * * ?") | ||
public void getCafeteria() throws InterruptedException { | ||
crawlingCafeteria(); | ||
} | ||
|
||
|
||
public void crawlingCafeteria() throws InterruptedException{ | ||
System.setProperty("webdriver.chrome.driver",installPath); | ||
ChromeOptions options = new ChromeOptions(); | ||
options.addArguments("--remote-allow-origins=*"); | ||
options.addArguments("headless"); | ||
options.addArguments("--no-sandbox"); | ||
options.addArguments("--disable-dev-shm-usage"); | ||
WebDriver webDriver = new ChromeDriver(options); | ||
|
||
try{ | ||
webDriver.get(url); | ||
Thread.sleep(1500); | ||
|
||
List<WebElement> foods = webDriver.findElements(By.className("con")); | ||
redisService.storeMeal("학생식당",1,foods.get(1).getText()); | ||
redisService.storeMeal("학생식당",2,foods.get(2).getText()); | ||
redisService.storeMeal("학생식당",3,foods.get(3).getText()); | ||
|
||
WebElement linkElement = webDriver.findElement(By.xpath("//a[span[text()='제1기숙사식당']]")); | ||
linkElement.click(); | ||
Thread.sleep(1500); | ||
|
||
foods = webDriver.findElements(By.className("con")); | ||
redisService.storeMeal("제1기숙사식당",1,foods.get(0).getText()); | ||
redisService.storeMeal("제1기숙사식당",2,foods.get(1).getText()); | ||
redisService.storeMeal("제1기숙사식당",3,foods.get(2).getText()); | ||
|
||
linkElement = webDriver.findElement(By.xpath("//a[span[text()='2호관(교직원)식당']]")); | ||
linkElement.click(); | ||
Thread.sleep(1500); | ||
|
||
foods = webDriver.findElements(By.className("con")); | ||
redisService.storeMeal("2호관(교직원)식당",1,"-"); | ||
redisService.storeMeal("2호관(교직원)식당",2,foods.get(0).getText()); | ||
redisService.storeMeal("2호관(교직원)식당",3,foods.get(1).getText()); | ||
|
||
linkElement = webDriver.findElement(By.xpath("//a[span[text()='27호관식당']]")); | ||
linkElement.click(); | ||
Thread.sleep(1500); | ||
|
||
foods = webDriver.findElements(By.className("con")); | ||
redisService.storeMeal("27호관식당",1,foods.get(0).getText()); | ||
redisService.storeMeal("27호관식당",2,foods.get(1).getText()); | ||
redisService.storeMeal("27호관식당",3,foods.get(2).getText()); | ||
|
||
linkElement = webDriver.findElement(By.xpath("//a[span[text()='사범대식당']]")); | ||
linkElement.click(); | ||
Thread.sleep(1500); | ||
|
||
foods = webDriver.findElements(By.className("con")); | ||
redisService.storeMeal("사범대식당",1,"-"); | ||
redisService.storeMeal("사범대식당",2,foods.get(0).getText()); | ||
redisService.storeMeal("사범대식당",3,foods.get(1).getText()); | ||
|
||
} finally { | ||
webDriver.quit(); | ||
} | ||
} | ||
|
||
public List<String> getCafeteria(String cafeteria){ | ||
List<String> menu = new ArrayList<>(); | ||
for(int i = 1 ; i<4;i++){ | ||
menu.add(redisService.getMeal(cafeteria,i)); | ||
} | ||
return menu; | ||
} | ||
} |
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