Skip to content

Commit

Permalink
[Feat] 학식 메뉴 크롤링 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hen715 committed May 1, 2024
1 parent 8696b6f commit 72b46f7
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws
.requestMatchers(HttpMethod.POST,"/api/members/**","/api/members").permitAll()
.requestMatchers("/api/members/all").hasRole("ADMIN")
.requestMatchers("/api/members/**","/api/members").hasAnyRole("USER","ADMIN")
.requestMatchers(HttpMethod.GET,"/api/posts/**","/api/posts","/api/fires/**").permitAll()
.requestMatchers(HttpMethod.GET,"/api/posts/**","/api/posts","/api/fires/**","/api/cafeterias").permitAll()
.requestMatchers("/api/posts/**","/api/posts","/api/fires/**","/api/fires").hasAnyRole("USER","ADMIN")
.requestMatchers("/api/replies/**").hasAnyRole("USER","ADMIN")
.requestMatchers("/api/search","/api/search/**","/api/notices","/api/notices/**","api/schedules","api/schedules/**").permitAll()
Expand Down
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);
}
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ public String getFireAiImage(Long id){
return image;
}

public void storeMeal(String cafeteria,int num,String menu){
String key = cafeteria+"-"+num;
log.info("식단저장 : {}",key);
redisTemplate.opsForValue().set(key,menu);
}

public String getMeal(String cafeteria,int num){
String key = cafeteria+"-"+num;
return redisTemplate.opsForValue().get(key);
}



}

0 comments on commit 72b46f7

Please sign in to comment.