-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat : 수동 스크래핑 서비스 로직 추가 * refactor : 점심 크롤링 리팩토링 - 스케줄러의 비즈니스 로직을 서비스단으로 분리 * feat : 수동 스크래핑 컨트롤러 추가 * refactor : 크롤링 관련 패키지 구조 변경 * refactor : 추상화 리팩토링
- Loading branch information
1 parent
1e4a553
commit 3ce38a9
Showing
18 changed files
with
320 additions
and
221 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
2 changes: 1 addition & 1 deletion
2
...unch/task/domain/FreshmealProperties.java → ...ain/lunch/domain/FreshmealProperties.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
2 changes: 1 addition & 1 deletion
2
...lunch/task/domain/WelstoryProperties.java → ...main/lunch/domain/WelstoryProperties.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
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
2 changes: 1 addition & 1 deletion
2
...domain/lunch/task/dto/GetScrapReqDto.java → ...ound/domain/lunch/dto/GetScrapReqDto.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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/ssafy/ssafsound/domain/lunch/dto/GetScrapResDto.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,10 @@ | ||
package com.ssafy.ssafsound.domain.lunch.dto; | ||
|
||
import com.ssafy.ssafsound.domain.lunch.domain.Lunch; | ||
import com.ssafy.ssafsound.domain.meta.domain.MetaData; | ||
|
||
import java.util.List; | ||
|
||
public interface GetScrapResDto { | ||
List<Lunch> getLunches(MetaData campus); | ||
} |
27 changes: 25 additions & 2 deletions
27
...ain/lunch/task/dto/GetWelstoryResDto.java → ...d/domain/lunch/dto/GetWelstoryResDto.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
91 changes: 91 additions & 0 deletions
91
src/main/java/com/ssafy/ssafsound/domain/lunch/service/FreshmealInfoProvider.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,91 @@ | ||
package com.ssafy.ssafsound.domain.lunch.service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.ssafy.ssafsound.domain.lunch.domain.FreshmealProperties; | ||
import com.ssafy.ssafsound.domain.lunch.domain.Lunch; | ||
import com.ssafy.ssafsound.domain.lunch.dto.GetFreshmealResDto; | ||
import com.ssafy.ssafsound.domain.lunch.dto.GetScrapReqDto; | ||
import com.ssafy.ssafsound.global.common.json.JsonParser; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class FreshmealInfoProvider implements ScrapInfoProvider { | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
private final FreshmealProperties freshmealProperties; | ||
|
||
@Override | ||
public List<Lunch> scrapLunchInfo(List<GetScrapReqDto> getScrapReqDtos) { | ||
|
||
List<Lunch> lunches = new ArrayList<>(); | ||
|
||
for (GetScrapReqDto getScrapReqDto : getScrapReqDtos) { | ||
Map<String, String> parameters = makeScrapParameters(); | ||
|
||
String url = makeScrapUri(this.freshmealProperties.getUrl(), parameters); | ||
|
||
ResponseEntity<String> response = restTemplate.exchange( | ||
url, | ||
HttpMethod.GET, | ||
makeHeader(), | ||
String.class | ||
); | ||
|
||
try { | ||
lunches.addAll(JsonParser.getMapper() | ||
.readValue(response.getBody(), GetFreshmealResDto.class) | ||
.getLunches(getScrapReqDto.getCampus())); | ||
} catch (JsonProcessingException e) { | ||
log.error(e.getMessage()); | ||
e.printStackTrace(); | ||
|
||
throw new RuntimeException(); | ||
} | ||
} | ||
|
||
return lunches; | ||
} | ||
|
||
private HttpEntity makeHeader() { | ||
HttpHeaders header = new HttpHeaders(); | ||
header.add("Content-Type", "application/json"); | ||
header.add("Accept", MediaType.APPLICATION_JSON_VALUE); | ||
return new HttpEntity(header); | ||
} | ||
|
||
private Map<String, String> makeScrapParameters() { | ||
Map<String, String> parameters = new HashMap<>(); | ||
|
||
parameters.put("storeIdx", this.freshmealProperties.getStoreIdx()); | ||
parameters.put("weekType", this.freshmealProperties.getWeekType()); | ||
|
||
return parameters; | ||
} | ||
|
||
private String makeScrapUri(String baseUri, Map<String, String> parameters) { | ||
|
||
UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); | ||
for (Map.Entry<String, String> entry : parameters.entrySet()) { | ||
uri.queryParam(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
return uri.toUriString(); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/main/java/com/ssafy/ssafsound/domain/lunch/service/LunchScrapService.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,83 @@ | ||
package com.ssafy.ssafsound.domain.lunch.service; | ||
|
||
import com.ssafy.ssafsound.domain.auth.dto.AuthenticatedMember; | ||
import com.ssafy.ssafsound.domain.auth.exception.AuthErrorInfo; | ||
import com.ssafy.ssafsound.domain.auth.exception.AuthException; | ||
import com.ssafy.ssafsound.domain.lunch.dto.GetLunchListReqDto; | ||
import com.ssafy.ssafsound.domain.lunch.dto.GetScrapReqDto; | ||
import com.ssafy.ssafsound.domain.lunch.exception.LunchErrorInfo; | ||
import com.ssafy.ssafsound.domain.lunch.exception.LunchException; | ||
import com.ssafy.ssafsound.domain.lunch.repository.LunchRepository; | ||
import com.ssafy.ssafsound.domain.member.exception.MemberErrorInfo; | ||
import com.ssafy.ssafsound.domain.meta.domain.Campus; | ||
import com.ssafy.ssafsound.domain.meta.domain.MetaData; | ||
import com.ssafy.ssafsound.domain.meta.service.MetaDataConsumer; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDate; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class LunchScrapService { | ||
|
||
private final LunchRepository lunchRepository; | ||
private final MetaDataConsumer metaDataConsumer; | ||
private final ScrapInfoProviderFactory scrapInfoProviderFactory; | ||
|
||
@Transactional(readOnly = false) | ||
public void scrapLunchManually(AuthenticatedMember authenticatedMember, GetLunchListReqDto getLunchListReqDto) { | ||
|
||
String memberRole = Objects.requireNonNull(authenticatedMember.getMemberRole(), MemberErrorInfo.MEMBER_ROLE_TYPE_NOT_FOUND.getMessage()); | ||
|
||
if (!memberRole.equals("admin")) { | ||
throw new AuthException(AuthErrorInfo.UNAUTHORIZED_ERROR); | ||
} | ||
|
||
MetaData campus = metaDataConsumer.getMetaData("CAMPUS", getLunchListReqDto.getCampus()); | ||
|
||
if (getLunchListReqDto.getCampus().equals(Campus.DAEJEON.getName())) { | ||
throw new LunchException(LunchErrorInfo.SCRAPING_ERROR); | ||
} | ||
|
||
if (getLunchListReqDto.getCampus().equals(Campus.GWANGJU.getName())) { | ||
scrapFreshmealLunch(campus); | ||
} else { | ||
scrapWelstoryLunch(getLunchListReqDto.getDate(), campus); | ||
} | ||
} | ||
|
||
@Transactional(readOnly = false) | ||
public void scrapWelstoryLunch(LocalDate date, MetaData... campuses) { | ||
|
||
ScrapInfoProvider scraper = scrapInfoProviderFactory.getProviderFrom("welstory"); | ||
|
||
List<GetScrapReqDto> requests = Arrays.stream(campuses) | ||
.map(campus -> GetScrapReqDto.builder() | ||
.campus(campus) | ||
.menuDt(date) | ||
.build()) | ||
.collect(Collectors.toList()); | ||
|
||
lunchRepository.saveAll(scraper.scrapLunchInfo(requests)); | ||
} | ||
|
||
@Transactional(readOnly = false) | ||
public void scrapFreshmealLunch(MetaData... campuses) { | ||
|
||
ScrapInfoProvider scraper = scrapInfoProviderFactory.getProviderFrom("freshmeal"); | ||
|
||
List<GetScrapReqDto> requests = Arrays.stream(campuses) | ||
.map(campus -> GetScrapReqDto.builder() | ||
.campus(campus) | ||
.build()) | ||
.collect(Collectors.toList()); | ||
|
||
lunchRepository.saveAll(scraper.scrapLunchInfo(requests)); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/ssafy/ssafsound/domain/lunch/service/ScrapInfoProvider.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,10 @@ | ||
package com.ssafy.ssafsound.domain.lunch.service; | ||
|
||
import com.ssafy.ssafsound.domain.lunch.domain.Lunch; | ||
import com.ssafy.ssafsound.domain.lunch.dto.GetScrapReqDto; | ||
|
||
import java.util.List; | ||
|
||
public interface ScrapInfoProvider { | ||
List<Lunch> scrapLunchInfo(List<GetScrapReqDto> getScrapReqDtos); | ||
} |
5 changes: 4 additions & 1 deletion
5
...task/domain/ScrapInfoProviderFactory.java → ...nch/service/ScrapInfoProviderFactory.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
Oops, something went wrong.