Skip to content

Commit

Permalink
Merge pull request EveryUniv#91 from EveryUniv/dev
Browse files Browse the repository at this point in the history
feat: 사용자 프로필 이미지 크롤링 추가
  • Loading branch information
gutanbug authored Jan 30, 2024
2 parents 6d6418f + ae50dd2 commit 7bd36f2
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class DkuUserInfo {
private final String gender;
private final int yearOfAdmission;
private final String studentState;
private final String profileImageUrl;

private final String majorName;
private final String departmentName;
Expand All @@ -24,6 +25,7 @@ public DkuUserInfo(StudentInfo info) {
this.gender = info.getGender();
this.yearOfAdmission = info.getYearOfAdmission();
this.studentState = info.getStudentState();
this.profileImageUrl = info.getProfileImageUrl();
this.majorName = info.getMajorName();
this.departmentName = info.getDepartmentName();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/dku/council/domain/user/model/UserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class UserInfo {
private final String academicStatus;
private final String age;
private final String gender;
private final String profileImage;
private final UserStatus status;

public UserInfo(User user) {
Expand All @@ -31,6 +32,7 @@ public UserInfo(User user) {
this.academicStatus = user.getAcademicStatus();
this.age = user.getAge();
this.gender = user.getGender();
this.profileImage = user.getProfileImage();
this.status = user.getStatus();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class ResponseUserInfoDto {
private final String major;
private final String department;
private final String phoneNumber;
private final String profileImage;
private final Long writePostCount;
private final Long commentedPostCount;
private final Long likedPostCount;
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/dku/council/domain/user/model/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public class User extends BaseEntity {
@Column
private String phone;

@Lob
private String profileImage;

@Enumerated(STRING)
private UserStatus status;

Expand All @@ -82,6 +85,7 @@ private User(@NonNull String studentId,
@NonNull String academicStatus,
@NonNull String age,
@NonNull String gender,
String profileImage,
Integer yearOfAdmission,
UserStatus status,
UserRole role) {
Expand All @@ -94,6 +98,7 @@ private User(@NonNull String studentId,
this.academicStatus = academicStatus;
this.age = age;
this.gender = gender;
this.profileImage = profileImage;
this.yearOfAdmission = yearOfAdmission;
this.status = status;
this.userRole = role;
Expand Down Expand Up @@ -173,14 +178,15 @@ public void changeAge(String age) {
* @param yearOfAdmission 입학년도
* @param studentState 학적상태
*/
public void changeGenericInfo(String studentId, String studentName, Major major, int yearOfAdmission, String studentState, String age, String gender) {
public void changeGenericInfo(String studentId, String studentName, Major major, int yearOfAdmission, String studentState, String age, String gender, String profileImage) {
this.studentId = studentId;
this.name = studentName;
this.major = major;
this.yearOfAdmission = yearOfAdmission;
this.academicStatus = studentState;
this.age = age;
this.gender = gender;
this.profileImage = profileImage;
}

/**
Expand All @@ -195,6 +201,7 @@ public void emptyOutUserInfo() {
this.age = "";
this.gender = "";
this.password = "";
this.profileImage = "";
}

/**
Expand All @@ -210,10 +217,11 @@ public void changeIsDkuChecked() {
* @param age 나이
* @param gender 성별
*/
public void updateDkuInfo(String age, String gender) {
public void updateDkuInfo(String age, String gender, String profileImage) {
this.isDkuChecked = true;
this.age = age;
this.gender = gender;
this.profileImage = profileImage;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public ResponseScrappedStudentInfoDto updateDKUStudent(Long userId, RequestDkuSt
info.getYearOfAdmission(),
info.getStudentState(),
info.getAge(),
info.getGender());
info.getGender(),
info.getProfileImageUrl());

userInfoService.invalidateUserInfo(userId);
return new ResponseScrappedStudentInfoDto(info);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public ResponseVerifyStudentDto validateEmailCode(RequestVerifyEmailCodeDto dto)
Major major = majorRepository.findById(dto.getMajorId())
.orElseThrow(MajorNotFoundException::new);
DkuUserInfo info = new DkuUserInfo(dto.getStudentName(), dto.getStudentId(), dto.getAge(), dto.getGender(),
dto.getYearOfAdmission(), dto.getAcademicStatus(), major.getName(), major.getDepartment());
dto.getYearOfAdmission(), dto.getAcademicStatus(), "" ,major.getName(), major.getDepartment());

dkuAuthRepository.setAuthPayload(signupToken, DKU_AUTH_NAME, info, now);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void signup(RequestSignupDto dto, String signupToken) {
user.changePassword(encryptedPassword);
user.changeIsDkuChecked();
user.changeGenericInfo(studentInfo.getStudentId(), studentInfo.getStudentName(), major,
studentInfo.getYearOfAdmission(), studentInfo.getStudentState(), studentInfo.getAge(), studentInfo.getGender());
studentInfo.getYearOfAdmission(), studentInfo.getStudentState(), studentInfo.getAge(), studentInfo.getGender(), studentInfo.getProfileImageUrl());
userInfoService.invalidateUserInfo(user.getId());
} else {
User user = User.builder()
Expand All @@ -66,6 +66,7 @@ public void signup(RequestSignupDto dto, String signupToken) {
.major(major)
.age(studentInfo.getAge())
.gender(studentInfo.getGender())
.profileImage(studentInfo.getProfileImageUrl())
.yearOfAdmission(studentInfo.getYearOfAdmission())
.academicStatus(studentInfo.getStudentState())
.status(UserStatus.ACTIVE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ResponseUserInfoDto getFullUserInfo(Long userId) {

return new ResponseUserInfoDto(user.getStudentId(), user.getName(),
user.getNickname(), user.getAge(), user.getGender(), year,
major.getName(), major.getDepartment(), phoneNumber,
major.getName(), major.getDepartment(), phoneNumber, user.getProfileImage(),
writePostCount, commentedPostCount, likedPostCount,
petitionCount, agreedPetitionCount, user.getUserRole().isAdmin());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class StudentInfo {
private final String gender;
private final int yearOfAdmission;
private final String studentState;
private final String profileImageUrl;

private final String majorName;
private final String departmentName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ public class DkuStudentService extends DkuScrapper {
private final String studentInfoApiPath;
private final String feeInfoApiPath;

private final String defaultProfileImage;

public DkuStudentService(@ChromeAgentWebClient WebClient webClient,
@Value("${dku.student-info.info-api-path}") String studentInfoApiPath,
@Value("${dku.student-info.fee-api-path}") String feeInfoApiPath) {
@Value("${dku.student-info.fee-api-path}") String feeInfoApiPath,
@Value("${dku.student-info.default-profile-image}") String defaultProfileImage) {
super(webClient);
this.studentInfoApiPath = studentInfoApiPath;
this.feeInfoApiPath = feeInfoApiPath;
this.defaultProfileImage = defaultProfileImage;
}


Expand Down Expand Up @@ -119,6 +123,8 @@ private StudentInfo parseStudentInfoHtml(String html) {
String gender = getElementValueOrThrow(doc, "sexNm");
String age = getAgeOrThrow(doc, "bday");

String profileImage = "";

String major, department = "";
int yearOfAdmission;

Expand All @@ -135,11 +141,21 @@ private StudentInfo parseStudentInfoHtml(String html) {

String etrsYy = getElementValueOrThrow(doc, "etrsYy");
yearOfAdmission = Integer.parseInt(etrsYy);

// 단국대학교 학생 프로필 이미지 크롤링
String src = getSrcFromElement(doc, "#stuInfotop .section_tbl .section_tbl.section_profile .group_photo .photo img");

profileImage = defaultProfileImage + src;
} catch (Throwable t) {
throw new DkuFailedCrawlingException(t);
}

return new StudentInfo(studentName, studentId, age, gender, yearOfAdmission, studentState, major, department);
return new StudentInfo(studentName, studentId, age, gender, yearOfAdmission, studentState, profileImage, major, department);
}

private String getSrcFromElement(Element baseElement, String selector) {
Element element = baseElement.selectFirst(selector);
return (element != null) ? element.attr("src") : null;
}

private String getAgeOrThrow(Document doc, String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void changeDkuAuth(RequestDkuStudentDto dto) {
if (info.getStudentState().equals("재학")) {
User user = userRepository.findByStudentId(dto.getDkuStudentId())
.orElseThrow(UserNotFoundException::new);
user.updateDkuInfo(info.getAge(), info.getGender());
user.updateDkuInfo(info.getAge(), info.getGender(), info.getProfileImageUrl());
}
else {
throw new FailedAuthRefreshException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void setup() {
@DisplayName("studentInfo를 잘 가져오는지")
void getStudentInfo() {
// given
DkuUserInfo info = new DkuUserInfo("name", "1212","20", "남자", 0, "state", "", "");
DkuUserInfo info = new DkuUserInfo("name", "1212","20", "남자", 0, "state","", "", "");
when(dkuAuthRepository.getAuthPayload(any(),
eq(DKU_AUTH_NAME), eq(DkuUserInfo.class), any()))
.thenReturn(Optional.of(info));
Expand Down Expand Up @@ -121,7 +121,7 @@ void verifyStudent() {
String id = "id";
String pwd = "pwd";
DkuAuth auth = new DkuAuth(new LinkedMultiValueMap<>());
StudentInfo info = new StudentInfo("name", "1212", "20", "남자",0, "state",
StudentInfo info = new StudentInfo("name", "1212", "20", "남자",0, "state", "",
"", "");
RequestDkuStudentDto dto = new RequestDkuStudentDto(id, pwd);

Expand Down Expand Up @@ -162,7 +162,7 @@ void updateDKUStudent() {
String pwd = "pwd";
DkuAuth auth = new DkuAuth(new LinkedMultiValueMap<>());
User user = UserMock.createDummyMajor();
StudentInfo info = new StudentInfo("name", "1212", "20", "남자",0, "state",
StudentInfo info = new StudentInfo("name", "1212", "20", "남자",0, "state", "",
"major", "department");
RequestDkuStudentDto dto = new RequestDkuStudentDto(id, pwd);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class SignupServiceTest {
private final String encodedPwd = "Encoded";
private final String phone = "01011112222";
private final String studentId = "id";
private final DkuUserInfo info = new DkuUserInfo("name", studentId, "20", "남자", 0, "재학", "Major", "Department");
private final DkuUserInfo info = new DkuUserInfo("name", studentId, "20", "남자", 0, "재학", "", "Major", "Department");
private final RequestSignupDto dto = new RequestSignupDto("nickname", "pwd");


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DkuStudentServiceTest extends AbstractMockServerTest {
void beforeEach() {
WebClient webClient = WebClient.create();
String path = "http://localhost:" + mockServer.getPort();
this.service = new DkuStudentService(webClient, path, path);
this.service = new DkuStudentService(webClient, path, path, path);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ public void beforeEach() throws NoSuchMethodException, InvocationTargetException
String webinfoLoginPath = properties.get("dku.login.webinfo-api-path");
String infoPath = properties.get("dku.student-info.info-api-path");
String feePath = properties.get("dku.student-info.fee-api-path");
String defaultProfileImage = properties.get("dku.student-info.default-profile-image");

this.authService = new DkuAuthenticationService(webClient, webinfoLoginPath, infoPath);
this.service = new DkuStudentService(webClient, infoPath, feePath);
this.service = new DkuStudentService(webClient, infoPath, feePath, defaultProfileImage);
}

@Test
Expand Down

0 comments on commit 7bd36f2

Please sign in to comment.