Skip to content

Commit

Permalink
Improve code readability by unifying parseSellerReviews implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
panic08 authored Nov 25, 2024
2 parents 53d9e0f + d8c44ce commit f083bf5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 32 deletions.
13 changes: 12 additions & 1 deletion core/src/main/java/ru/funpay4j/client/FunPayParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,23 @@ public interface FunPayParser {
*
* @param userId user id by which seller reviews pages will be parsed
* @param pages number of pages indicating how many seller reviews will be parsed
* @return sellerReviews
* @throws FunPayApiException if the other api-related exception
* @throws UserNotFoundException if the user with id does not found/seller
*/
List<SellerReview> parseSellerReviews(long userId, int pages) throws FunPayApiException, UserNotFoundException;

/**
* Parse seller reviews with stars filter
*
* @param userId user id by which seller reviews pages will be parsed
* @param pages number of pages indicating how many seller reviews will be parsed
* @param starsFilter number of stars by which the reviews will be parsed
* @return sellerReviews
* @throws FunPayApiException if the other api-related exception
* @throws UserNotFoundException if the user with id does not found/seller
*/
List<SellerReview> parseSellerReviews(long userId, int pages, Integer starsFilter) throws FunPayApiException, UserNotFoundException;
List<SellerReview> parseSellerReviews(long userId, int pages, int starsFilter) throws FunPayApiException, UserNotFoundException;

/**
* Parse csrf-token and PHPSESSID
Expand Down
82 changes: 52 additions & 30 deletions core/src/main/java/ru/funpay4j/client/jsoup/JsoupFunPayParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,61 @@ public User parseUser(long userId) throws FunPayApiException, UserNotFoundExcept
* {@inheritDoc}
*/
@Override
public List<SellerReview> parseSellerReviews(long userId, int pages, Integer starsFilter) throws FunPayApiException, UserNotFoundException {
public List<SellerReview> parseSellerReviews(long userId, int pages) throws FunPayApiException, UserNotFoundException {
return parseSellerReviewsInternal(userId, pages, null);
}

/**
* {@inheritDoc}
*/
@Override
public List<SellerReview> parseSellerReviews(long userId, int pages, int starsFilter) throws FunPayApiException, UserNotFoundException {
return parseSellerReviewsInternal(userId, pages, String.valueOf(starsFilter));
}

/**
* {@inheritDoc}
*/
@Override
public CsrfTokenAndPHPSESSID parseCsrfTokenAndPHPSESSID(@NonNull String goldenKey) throws FunPayApiException {
//We send a request to /unknown URL that doesn't exist to get a page where it will be reported that the page doesn't exist.
// This is necessary because such a page is the smallest size
try (Response funPayHtmlResponse = httpClient.newCall(new Request.Builder().get().url(baseURL + "/unknown/")
.addHeader("Cookie", "golden_key=" + goldenKey).build()).execute()) {
String funPayHtmlPageBody = funPayHtmlResponse.body().string();

Document funPayDocument = Jsoup.parse(funPayHtmlPageBody);

String dataAppData = funPayDocument.getElementsByTag("body").attr("data-app-data");

String csrfToken = JsonParser.parseString(dataAppData).getAsJsonObject().get("csrf-token").getAsString();
//Use this regex to get the value of the PHPSESSID key from the Set-Cookie header
String PHPSESSID = funPayHtmlResponse.header("Set-Cookie").replaceAll(".*PHPSESSID=([^;]*).*", "$1");

return CsrfTokenAndPHPSESSID.builder()
.csrfToken(csrfToken)
.PHPSESSID(PHPSESSID)
.build();
} catch (IOException e) {
throw new FunPayApiException(e.getLocalizedMessage());
}
}

/**
* Common method to parse seller reviews
*
* @param userId user id by which seller reviews pages will be parsed
* @param pages number of pages indicating how many seller reviews will be parsed
* @param starsFilter number of stars filter, can be null
* @return sellerReviews
* @throws FunPayApiException if the other api-related exception
* @throws UserNotFoundException if the user with id does not found/seller
*/
private List<SellerReview> parseSellerReviewsInternal(long userId, int pages, String starsFilter) throws FunPayApiException, UserNotFoundException {
List<SellerReview> currentSellerReviews = new ArrayList<>();

String userIdFormData = String.valueOf(userId);
String starsFilterFormData = starsFilter == null ? "" : String.valueOf(starsFilter);
String starsFilterFormData = starsFilter == null ? "" : starsFilter;
String continueArg = null;

for (int currentPageCount = 0; currentPageCount < pages; currentPageCount++) {
Expand Down Expand Up @@ -539,34 +589,6 @@ public List<SellerReview> parseSellerReviews(long userId, int pages, Integer sta
return currentSellerReviews;
}

/**
* {@inheritDoc}
*/
@Override
public CsrfTokenAndPHPSESSID parseCsrfTokenAndPHPSESSID(@NonNull String goldenKey) throws FunPayApiException {
//We send a request to /unknown URL that doesn't exist to get a page where it will be reported that the page doesn't exist.
// This is necessary because such a page is the smallest size
try (Response funPayHtmlResponse = httpClient.newCall(new Request.Builder().get().url(baseURL + "/unknown/")
.addHeader("Cookie", "golden_key=" + goldenKey).build()).execute()) {
String funPayHtmlPageBody = funPayHtmlResponse.body().string();

Document funPayDocument = Jsoup.parse(funPayHtmlPageBody);

String dataAppData = funPayDocument.getElementsByTag("body").attr("data-app-data");

String csrfToken = JsonParser.parseString(dataAppData).getAsJsonObject().get("csrf-token").getAsString();
//Use this regex to get the value of the PHPSESSID key from the Set-Cookie header
String PHPSESSID = funPayHtmlResponse.header("Set-Cookie").replaceAll(".*PHPSESSID=([^;]*).*", "$1");

return CsrfTokenAndPHPSESSID.builder()
.csrfToken(csrfToken)
.PHPSESSID(PHPSESSID)
.build();
} catch (IOException e) {
throw new FunPayApiException(e.getLocalizedMessage());
}
}

private void extractReviewsFromReviewsHtml(Document reviewsHtml, List<SellerReview> currentSellerReviews) {
List<Element> reviewContainerElements = reviewsHtml.getElementsByClass("review-container");

Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/ru/funpay4j/core/FunPayExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ public User execute(GetUser command) throws FunPayApiException, UserNotFoundExce
* @throws UserNotFoundException if the user with id does not found/seller
*/
public List<SellerReview> execute(GetSellerReviews command) throws FunPayApiException, UserNotFoundException {
return funPayParser.parseSellerReviews(command.getUserId(), command.getPages(), command.getStarsFilter());
if (command.getStarsFilter() != null) {
return funPayParser.parseSellerReviews(command.getUserId(), command.getPages(), command.getStarsFilter());
} else {
return funPayParser.parseSellerReviews(command.getUserId(), command.getPages());
}
}
}

0 comments on commit f083bf5

Please sign in to comment.