Skip to content

Commit

Permalink
Merge pull request IT-Pick#90 from emes-g/dev
Browse files Browse the repository at this point in the history
Feat: 나무위키, 구글 랭킹 구현
  • Loading branch information
minseok1015 authored Aug 15, 2024
2 parents 60d5cbc + 549f104 commit 8bb8e35
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public enum CommunityType {
TOTAL("total"),
NAVER("naver"),
NATE("nate"),
ZUM("zum");
ZUM("zum"),
GOOGLE("google"),
NAMUWIKI("namuwiki");

private final String communityType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
public enum RankingWeight {
NAVER(5),
NATE(3),
ZUM(2);
ZUM(2),
GOOGLE(4),
NAMUWIKI(1);

private final int rankingWeight;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class RankService {

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

for (String comm : communitiesToCheck) {
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/store/itpick/backend/service/SeleniumService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ public class SeleniumService {
private final ReferenceService referenceService;
private final CommunityPeriodService communityPeriodService;



// ChromeDriver 연결 (WEB_DRIVER_PATH 값 주입되고 사용해야 하므로 PostConstruct)
public void initDriver() {
seleniumUtil.initDriver();
driver = seleniumUtil.getDriver();
}

public List<Reference> useDriverForZum(String url) {
// initDriver(); // 로컬에서 테스트 위해 잠시 호출
driver.get(url);

Actions actions = new Actions(driver);
Expand Down Expand Up @@ -109,9 +108,8 @@ public List<Reference> useDriverForZum(String url) {
return null;
}



public List<Reference> useDriverForNaver(String url) {
// initDriver(); // 로컬에서 테스트 위해 잠시 호출
driver.get(url);

try {
Expand Down Expand Up @@ -143,8 +141,8 @@ public List<Reference> useDriverForNaver(String url) {

return null;
}

public List<Reference> useDriverForMnate(String url) {
// initDriver(); // 로컬에서 테스트 위해 잠시 호출
driver.get(url);
Actions actions = new Actions(driver);

Expand Down Expand Up @@ -212,7 +210,7 @@ public List<Reference> useDriverForGoogle(String url) throws IOException {


/**구글 관련 Redis저장**/
// redis.saveRealtime(CommunityType.NATE, PeriodType.BY_REAL_TIME, keywordList);
redis.saveRealtime(CommunityType.GOOGLE, PeriodType.BY_REAL_TIME, keywordList);

// 링크 수집
List<String> linksList = new ArrayList<>();
Expand Down Expand Up @@ -459,6 +457,7 @@ public void quitDriver() {


public String useDriverForNamuwiki(String url) {
// initDriver(); // 로컬에서 테스트 위해 잠시 호출
driver.get(url);

Actions actions = new Actions(driver);
Expand All @@ -484,8 +483,7 @@ public String useDriverForNamuwiki(String url) {
}

/**나무위키 관련 Redis저장**/

// redis.saveRealtime(CommunityType.NATE, PeriodType.BY_REAL_TIME, keywordList);
redis.saveRealtime(CommunityType.NAMUWIKI, PeriodType.BY_REAL_TIME, keywordList);

// 링크 수집
List<String> linksList = new ArrayList<>();
Expand Down
30 changes: 10 additions & 20 deletions src/main/java/store/itpick/backend/util/Redis.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,10 @@ public class Redis {
@Autowired
private RedisTemplate<String, Object> redisTemplate;

// @Scheduled(cron = "0/5 * * * * ?")
// public void save() {
// ZSetOperations<String, Object> zSetOperations = redisTemplate.opsForZSet();
// String key = makeKey("nate", PeriodType.BY_WEEK);
//
// zSetOperations.add(key, "남자 양궁", 4);
// zSetOperations.add(key, "여자 양궁", 5);
// zSetOperations.add(key, "카온플", 2);
// zSetOperations.add(key, "요거트 스무디", 3);
// zSetOperations.add(key, "버즈", 6);
// redisTemplate.expire(key, Duration.ofSeconds(20));
// }

public String saveRealtime(CommunityType communityType, PeriodType periodType, List<String> keywordList) {
ZSetOperations<String, Object> zSetOperations = redisTemplate.opsForZSet();
String key = makeKey(communityType, periodType, "not needed");

// List<String> originalKeywordList = new ArrayList<>();
// for (Object originalKeyword : Objects.requireNonNull(zSetOperations.reverseRange(key, 0, 9))) {
// originalKeywordList.add((String) originalKeyword);
// }

if (keywordList.size() >= 10) {
redisTemplate.delete(key);
}
Expand Down Expand Up @@ -90,7 +72,7 @@ public void saveWeek() {
List<String> weekKeyList = getKeyList(PeriodType.BY_WEEK, mondayOfPreviousWeek.format(dateTimeFormatter));

for (List<String> dayKeyList : dayKeyListOfPreviousWeek) { // 지난주 월요일부터 일요일까지, 각 커뮤니티의 키 리스트
for (int i = 0; i < dayKeyList.size(); i++) { // naver, nate, zum에 대하여
for (int i = 0; i < dayKeyList.size(); i++) { // naver, nate, zum, google, namuwiki에 대하여
int score = 10;
redisTemplate.delete(weekKeyList.get(i)); // 기존 키 삭제
for (Object dayKeyword : Objects.requireNonNull(zSetOperations.reverseRange(dayKeyList.get(i), 0, 9))) {
Expand All @@ -111,7 +93,7 @@ public void saveTotalRanking(PeriodType periodType) {
case BY_WEEK -> DateUtils.localDateToString(DateUtils.getMondayOfPreviousWeek());
};
String totalKey = makeKey(CommunityType.TOTAL, periodType, date);
List<String> keyList = getKeyList(periodType, date); // key for naver, nate, zum
List<String> keyList = getKeyList(periodType, date); // key for naver, nate, zum, google, namuwiki
for (String key : keyList) {
int weight = getWeight(key);
int rank = 1;
Expand Down Expand Up @@ -172,6 +154,8 @@ private static List<String> getKeyList(PeriodType periodType, String date) {
keyList.add(makeKey(CommunityType.NAVER, periodType, date));
keyList.add(makeKey(CommunityType.NATE, periodType, date));
keyList.add(makeKey(CommunityType.ZUM, periodType, date));
keyList.add(makeKey(CommunityType.GOOGLE, periodType, date));
keyList.add(makeKey(CommunityType.NAMUWIKI, periodType, date));
return keyList;
}

Expand All @@ -185,6 +169,12 @@ private static int getWeight(String key) {
if (key.startsWith(CommunityType.ZUM.value())) {
return RankingWeight.ZUM.get();
}
if (key.startsWith(CommunityType.GOOGLE.value())) {
return RankingWeight.GOOGLE.get();
}
if (key.startsWith(CommunityType.NAMUWIKI.value())) {
return RankingWeight.NAMUWIKI.get();
}
return -1;
}
}

0 comments on commit 8bb8e35

Please sign in to comment.