Skip to content

Commit

Permalink
Merge pull request #64 from minseok1015/dev
Browse files Browse the repository at this point in the history
feat : weelky 관련자료 완료, 스케줄러에 driver 끄는거 없앰,
  • Loading branch information
minseok1015 authored Aug 10, 2024
2 parents d4b846b + b9f2040 commit c4a9ddf
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public void updateNate(){
keywordService.performDailyTasksNate();
}

@GetMapping("/update/zum")
public void updateZum(){
keywordService.performDailyTasksZum();
}

@GetMapping("/naver")
public List<Reference> getRankFromSignal() {
String url = "https://www.signal.bz/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public interface KeywordRepository extends JpaRepository<Keyword, Long> {

Optional<Keyword> findByKeyword(String keyword);
List<Keyword> findByCommunityPeriods(CommunityPeriod communityPeriod);
@Query("SELECT k FROM Keyword k JOIN k.communityPeriods cp " +
"WHERE k.keyword = :keyword AND cp.community = :community " +
"ORDER BY k.updateAt DESC")
Optional<Keyword> findTop1ByKeywordAndCommunityOrderByUpdatedAtDesc(@Param("keyword") String keyword,
@Param("community") String community);




@Query("SELECT k FROM Keyword k JOIN k.communityPeriods cp WHERE cp.community = 'naver' ORDER BY k.updateAt DESC")
Expand Down
70 changes: 35 additions & 35 deletions src/main/java/store/itpick/backend/service/RankService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,48 @@ public class RankService {
private CommunityPeriodRepository communityPeriodRepository;

public RankResponseDTO getReferenceByKeyword(String community, String period, String keyword) {
List<String> communitiesToCheck;
if ("total".equals(community)) {
communitiesToCheck = List.of("naver", "zum", "nate");
} else {
communitiesToCheck = List.of(community);
}
List<String> communitiesToCheck = "total".equals(community)
? List.of("naver", "zum", "nate")
: List.of(community);

for (String comm : communitiesToCheck) {
// CommunityPeriod를 찾기
Optional<CommunityPeriod> communityPeriodOptional = communityPeriodRepository.findByCommunityAndPeriod(comm, period);

if (communityPeriodOptional.isPresent()) {
CommunityPeriod communityPeriod = communityPeriodOptional.get();

// CommunityPeriod에 연결된 키워드 찾기
List<Keyword> keywords = keywordRepository.findByCommunityPeriods(communityPeriod);

// 주어진 키워드가 있는지 확인
Keyword keywordEntity = keywords.stream()
.filter(k -> k.getKeyword().equals(keyword))
.findFirst()
.orElse(null);

if (keywordEntity != null) {
Reference reference = keywordEntity.getReference();

// DTO 생성
RankResponseDTO response = new RankResponseDTO();
response.setKeyword(keywordEntity.getKeyword());
response.setSearchLink(reference.getSearchLink());
response.setNewsTitle(reference.getNewsTitle());
response.setImageUrl(reference.getNewsImage());
response.setNewsContent(reference.getNewsContent());
response.setNewsLink(reference.getNewsLink());

return response;
Optional<Keyword> keywordOptional;

if ("weekly".equals(period)) {
// weekly 기간에 대한 처리
keywordOptional = keywordRepository.findTop1ByKeywordAndCommunityOrderByUpdatedAtDesc(keyword, comm);
} else {
// 다른 기간에 대한 처리
Optional<CommunityPeriod> communityPeriodOptional = communityPeriodRepository.findByCommunityAndPeriod(comm, period);

if (communityPeriodOptional.isPresent()) {
CommunityPeriod communityPeriod = communityPeriodOptional.get();
keywordOptional = keywordRepository.findTop1ByKeywordAndCommunityOrderByUpdatedAtDesc(keyword, comm);
} else {
continue;
}
}

if (keywordOptional.isPresent()) {
Keyword latestKeyword = keywordOptional.get();
Reference reference = latestKeyword.getReference();

RankResponseDTO response = new RankResponseDTO();
response.setKeyword(latestKeyword.getKeyword());
response.setSearchLink(reference.getSearchLink());
response.setNewsTitle(reference.getNewsTitle());
response.setImageUrl(reference.getNewsImage());
response.setNewsContent(reference.getNewsContent());
response.setNewsLink(reference.getNewsLink());

return response;
}
}

// 키워드가 없거나 커뮤니티/기간이 없을 경우 null 반환 또는 예외 처리
return null;
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void performScheduledTasks() {

private boolean isDailyTaskTime() {
LocalTime now = LocalTime.now();
return now.getHour() == 18 && now.getMinute() == 0;
return now.getHour() == 11 && now.getMinute() == 10;
}

private boolean isMonday() {
Expand All @@ -96,8 +96,6 @@ private void performHourlyTasks() {

} catch (Exception e) {
log.error("Error during hourly task", e);
} finally {
seleniumService.quitDriver(); // 작업 후 드라이버 종료
}
}

Expand Down
53 changes: 23 additions & 30 deletions src/main/java/store/itpick/backend/service/SeleniumService.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ public void processKeywordsAndReferences(String communityName, List<String> keyw
List<Keyword> keywordsToSave = new ArrayList<>();
int size = Math.min(keywordList.size(), linksList.size());


for (int i = 0; i < size; i++) {
String keywordContent = keywordList.get(i);
Keyword keyword = new Keyword();
Expand All @@ -307,16 +306,17 @@ public void processKeywordsAndReferences(String communityName, List<String> keyw
referenceService.saveAll(references);

// 4. CommunityPeriod 객체 생성 및 저장
CommunityPeriod communityPeriod = new CommunityPeriod();
communityPeriod.setCommunity(communityName);
communityPeriod.setPeriod("realtime");
CommunityPeriod finalCommunityPeriod; // 최종적으로 사용될 CommunityPeriod 객체 선언

try {
Optional<CommunityPeriod> existingCommunityPeriodOptional = communityPeriodService.findByCommunityAndPeriod(communityName, "realtime");
if (existingCommunityPeriodOptional.isPresent()) {
communityPeriod = existingCommunityPeriodOptional.get();
finalCommunityPeriod = existingCommunityPeriodOptional.get();
} else {
communityPeriodService.save(communityPeriod);
CommunityPeriod newCommunityPeriod = new CommunityPeriod();
newCommunityPeriod.setCommunity(communityName);
newCommunityPeriod.setPeriod("realtime");
finalCommunityPeriod = communityPeriodService.save(newCommunityPeriod);
}
} catch (DataIntegrityViolationException e) {
log.error("CommunityPeriod 저장 중 예외 발생: " + e.getMessage());
Expand All @@ -326,50 +326,43 @@ public void processKeywordsAndReferences(String communityName, List<String> keyw
// 1. 키워드 존재 여부에 따라 구분
for (int i = 0; i < size; i++) {
Reference reference = references.get(i);


String keywordContent = keywordList.get(i);
Optional<Keyword> existingKeywordOptional = keywordService.findByKeyword(keywordContent);


if (existingKeywordOptional.isPresent()) {
//중복되는 키워드가 있는 경우 reference 만 업데이트 해줌
// 중복되는 키워드가 있는 경우 reference만 업데이트
Keyword existingKeyword = existingKeywordOptional.get();
existingKeyword.setReference(reference);

keywordsToUpdate.add(existingKeyword); // 기존 키워드를 업데이트 대상으로 추가
// 중복되는 CommunityPeriod가 이미 존재하는지 확인
boolean isCommunityPeriodAlreadyAdded = existingKeyword.getCommunityPeriods().stream()
.anyMatch(cp -> cp.getCommunity().equals(finalCommunityPeriod.getCommunity())
&& cp.getPeriod().equals(finalCommunityPeriod.getPeriod()));

if (!isCommunityPeriodAlreadyAdded) {
// 중복되지 않는 경우에만 CommunityPeriod 추가
existingKeyword.getCommunityPeriods().add(finalCommunityPeriod);
}

// 기존 키워드를 업데이트 대상으로 추가
keywordsToUpdate.add(existingKeyword);
} else {
//중복되는 키워드가 없는 경우
Keyword keyword= new Keyword();
Keyword keyword = new Keyword();
keyword.setKeyword(keywordContent);
keyword.setReference(reference);
keyword.getCommunityPeriods().add(communityPeriod); // CommunityPeriod 추가
keyword.getCommunityPeriods().add(finalCommunityPeriod); // CommunityPeriod 추가
keywordsToSave.add(keyword); // 새로운 키워드를 저장 대상으로 추가
}
}







//// // 5. 키워드에 참조 설정 및 기존 키워드 업데이트
// for (int i = 0; i < keywordsToSave.size(); i++) {
// Keyword keyword = keywordsToSave.get(i);
// Reference reference = references.get(i);
// keyword.setReference(reference);
// keyword.getCommunityPeriods().add(communityPeriod); // CommunityPeriod 추가
// }


try {
// 모든 키워드와 커뮤니티 기간 저장
keywordService.saveAll(keywordsToSave); // 새로운 키워드 저장
for (Keyword keyword : keywordsToUpdate) {
keywordService.save(keyword); // 기존 키워드 업데이트
}
communityPeriodService.save(communityPeriod);
communityPeriodService.save(finalCommunityPeriod);
} catch (DataIntegrityViolationException e) {
log.error("중복된 키워드가 존재합니다: " + e.getMessage());
throw new RuntimeException("중복된 키워드가 존재합니다.", e);
Expand All @@ -382,6 +375,7 @@ public void processKeywordsAndReferences(String communityName, List<String> keyw




public void quitDriver() {
seleniumUtil.quitDriver();
}
Expand Down Expand Up @@ -425,7 +419,6 @@ public String useDriverForNamuwiki(String url) {
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"app\"]/div[1]/div[2]/div/div[6]/div[4]/div/ul")));
System.out.println(ul.getText());

quitDriver();
return null;
}

Expand Down

0 comments on commit c4a9ddf

Please sign in to comment.