Skip to content

Commit 83037a3

Browse files
authored
Merge pull request #90 from panic08/add-GetSale-command
Add GetOrder command
2 parents 6c80cfd + b2940a3 commit 83037a3

File tree

13 files changed

+1581
-31
lines changed

13 files changed

+1581
-31
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55

66
allprojects {
77
group = 'ru.funpay4j'
8-
version = '1.0.6'
8+
version = '1.0.7'
99

1010
repositories {
1111
mavenCentral()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.panic08.funpay4j.exceptions.order;
16+
17+
/**
18+
* Base class for exception related to the fact that the order is not found
19+
*
20+
* @author panic08
21+
* @since 1.0.7
22+
*/
23+
public class OrderNotFoundException extends RuntimeException {
24+
25+
/**
26+
* Initializes a new OrderNotFoundException exception
27+
*
28+
* @param message exception message
29+
*/
30+
public OrderNotFoundException(String message) {
31+
super(message);
32+
}
33+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.panic08.funpay4j.objects.order;
16+
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
import lombok.AllArgsConstructor;
21+
import lombok.Builder;
22+
import lombok.Data;
23+
24+
import com.panic08.funpay4j.objects.user.ParsedPreviewUser;
25+
26+
/**
27+
* This object represents the parsed FunPay order
28+
*
29+
* @author panic08
30+
* @since 1.0.7
31+
*/
32+
@Data
33+
@AllArgsConstructor
34+
@Builder
35+
public class ParsedOrder {
36+
37+
private String id;
38+
39+
private List<String> statuses;
40+
41+
private String shortDescription;
42+
43+
private String detailedDescription;
44+
45+
private Map<String, String> params;
46+
47+
private double price;
48+
49+
private ParsedPreviewUser other;
50+
}

client/src/main/java/com/panic08/funpay4j/parser/FunPayParser.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import com.panic08.funpay4j.exceptions.InvalidGoldenKeyException;
2121
import com.panic08.funpay4j.exceptions.lot.LotNotFoundException;
2222
import com.panic08.funpay4j.exceptions.offer.OfferNotFoundException;
23+
import com.panic08.funpay4j.exceptions.order.OrderNotFoundException;
2324
import com.panic08.funpay4j.exceptions.user.UserNotFoundException;
2425
import com.panic08.funpay4j.objects.CsrfTokenAndPHPSESSID;
2526
import com.panic08.funpay4j.objects.game.ParsedPromoGame;
2627
import com.panic08.funpay4j.objects.lot.ParsedLot;
2728
import com.panic08.funpay4j.objects.offer.ParsedOffer;
29+
import com.panic08.funpay4j.objects.order.ParsedOrder;
2830
import com.panic08.funpay4j.objects.transaction.ParsedTransaction;
2931
import com.panic08.funpay4j.objects.transaction.ParsedTransactionType;
3032
import com.panic08.funpay4j.objects.user.ParsedSellerReview;
@@ -173,6 +175,18 @@ List<ParsedTransaction> parseTransactions(
173175
List<ParsedTransaction> parseTransactions(String goldenKey, long userId, int pages)
174176
throws FunPayApiException, UserNotFoundException, InvalidGoldenKeyException;
175177

178+
/**
179+
* Parse order authorized
180+
*
181+
* @param goldenKey golden key which will be used to authorize the user
182+
* @param orderId order id by which order will be parsed
183+
* @return order
184+
* @throws FunPayApiException if the other api-related exception
185+
* @throws OrderNotFoundException if the order with id does not found
186+
*/
187+
ParsedOrder parseOrder(String goldenKey, String orderId)
188+
throws FunPayApiException, OrderNotFoundException;
189+
176190
/**
177191
* Parse csrf-token and PHPSESSID
178192
*

client/src/main/java/com/panic08/funpay4j/parser/JsoupFunPayParser.java

Lines changed: 120 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.panic08.funpay4j.exceptions.InvalidGoldenKeyException;
4141
import com.panic08.funpay4j.exceptions.lot.LotNotFoundException;
4242
import com.panic08.funpay4j.exceptions.offer.OfferNotFoundException;
43+
import com.panic08.funpay4j.exceptions.order.OrderNotFoundException;
4344
import com.panic08.funpay4j.exceptions.user.UserNotFoundException;
4445
import com.panic08.funpay4j.objects.CsrfTokenAndPHPSESSID;
4546
import com.panic08.funpay4j.objects.game.ParsedPromoGame;
@@ -48,14 +49,11 @@
4849
import com.panic08.funpay4j.objects.lot.ParsedLotCounter;
4950
import com.panic08.funpay4j.objects.offer.ParsedOffer;
5051
import com.panic08.funpay4j.objects.offer.ParsedPreviewOffer;
52+
import com.panic08.funpay4j.objects.order.ParsedOrder;
5153
import com.panic08.funpay4j.objects.transaction.ParsedTransaction;
5254
import com.panic08.funpay4j.objects.transaction.ParsedTransactionStatus;
5355
import com.panic08.funpay4j.objects.transaction.ParsedTransactionType;
54-
import com.panic08.funpay4j.objects.user.ParsedAdvancedSellerReview;
55-
import com.panic08.funpay4j.objects.user.ParsedPreviewSeller;
56-
import com.panic08.funpay4j.objects.user.ParsedSeller;
57-
import com.panic08.funpay4j.objects.user.ParsedSellerReview;
58-
import com.panic08.funpay4j.objects.user.ParsedUser;
56+
import com.panic08.funpay4j.objects.user.*;
5957

6058
/**
6159
* This implementation of FunPayParser uses Jsoup to parse
@@ -389,34 +387,14 @@ public ParsedOffer parseOffer(long offerId) throws FunPayApiException, OfferNotF
389387
parameters.put(key, value);
390388
}
391389

392-
Element previewSellerUsernameElement =
393-
funPayDocument.getElementsByClass("media-user-name").first().selectFirst("a");
394-
Element previewSellerImgElement =
395-
funPayDocument.getElementsByClass("media-user").first().selectFirst("img");
396390
Element previewSellerReviewCountElement =
397391
funPayDocument.getElementsByClass("text-mini text-light mb5").first();
398392

399-
String previewSellerUsernameElementHrefAttributeValue =
400-
previewSellerUsernameElement.attr("href");
401-
402-
long previewSellerUserId =
403-
Long.parseLong(
404-
previewSellerUsernameElementHrefAttributeValue.substring(
405-
25,
406-
previewSellerUsernameElementHrefAttributeValue.length() - 1));
407-
String previewSellerUsername = previewSellerUsernameElement.text();
408-
String previewSellerAvatarPhotoLink = previewSellerImgElement.attr("src");
409-
410-
// if the previewUser has a regular photo
411-
if (previewSellerAvatarPhotoLink.equals("/img/layout/avatar.png"))
412-
previewSellerAvatarPhotoLink = null;
413-
414393
// Select rating from string like "219 reviews over 2 years"
415394
int previewSellerReviewCount =
416395
Integer.parseInt(
417396
previewSellerReviewCountElement.text().replaceAll("\\D.*", ""));
418-
boolean isPreviewSellerOnline =
419-
funPayDocument.getElementsByClass("media media-user online").first() != null;
397+
ParsedPreviewUser parsedPreviewUser = extractPreviewUserFromProductPage(funPayDocument);
420398

421399
return ParsedOffer.builder()
422400
.id(offerId)
@@ -428,11 +406,11 @@ public ParsedOffer parseOffer(long offerId) throws FunPayApiException, OfferNotF
428406
.parameters(parameters)
429407
.seller(
430408
ParsedPreviewSeller.builder()
431-
.userId(previewSellerUserId)
432-
.username(previewSellerUsername)
433-
.avatarPhotoLink(previewSellerAvatarPhotoLink)
409+
.userId(parsedPreviewUser.getUserId())
410+
.avatarPhotoLink(parsedPreviewUser.getAvatarPhotoLink())
411+
.username(parsedPreviewUser.getUsername())
412+
.isOnline(parsedPreviewUser.isOnline())
434413
.reviewCount(previewSellerReviewCount)
435-
.isOnline(isPreviewSellerOnline)
436414
.build())
437415
.build();
438416
} catch (IOException e) {
@@ -497,6 +475,86 @@ public List<ParsedTransaction> parseTransactions(String goldenKey, long userId,
497475
return parseTransactionsInternal(goldenKey, userId, null, pages);
498476
}
499477

478+
/** {@inheritDoc} */
479+
@Override
480+
public ParsedOrder parseOrder(String goldenKey, String orderId)
481+
throws FunPayApiException, OrderNotFoundException {
482+
try (Response funPayHtmlResponse =
483+
httpClient
484+
.newCall(
485+
new Request.Builder()
486+
.get()
487+
.addHeader("Cookie", "golden_key=" + goldenKey)
488+
.url(baseURL + "/orders/" + orderId + "/")
489+
.build())
490+
.execute()) {
491+
String funPayHtmlPageBody = funPayHtmlResponse.body().string();
492+
493+
Document funPayDocument = Jsoup.parse(funPayHtmlPageBody);
494+
495+
if (isNonExistentFunPayPage(funPayDocument)) {
496+
throw new OrderNotFoundException(
497+
"Order with orderId " + orderId + " does not found");
498+
}
499+
500+
List<String> statuses = new ArrayList<>();
501+
502+
List<Element> pageContentChildren =
503+
funPayDocument.getElementsByClass("page-content").first().children();
504+
505+
Element pageHeader = pageContentChildren.get(0);
506+
for (Element statusElement : pageHeader.children()) {
507+
statuses.add(statusElement.text());
508+
}
509+
510+
Element paramList = pageContentChildren.get(1);
511+
512+
Map<String, String> params = new HashMap<>();
513+
514+
for (Element col : paramList.getElementsByClass("row").first().children()) {
515+
List<Element> paramItemChildren =
516+
col.getElementsByClass("param-item").first().children();
517+
params.put(paramItemChildren.get(0).text(), paramItemChildren.get(1).text());
518+
}
519+
520+
List<Element> paramRows = paramList.getElementsByClass("row");
521+
522+
Double price = null;
523+
524+
for (Element paramRow : paramRows) {
525+
for (Element paramCol : paramRow.children()) {
526+
List<Element> paramItemChildren =
527+
paramCol.getElementsByClass("param-item").first().children();
528+
if (paramItemChildren.get(0).text().equals("Сумма")) {
529+
price =
530+
Double.parseDouble(
531+
paramItemChildren.get(1).children().get(0).text());
532+
} else {
533+
params.put(
534+
paramItemChildren.get(0).text(), paramItemChildren.get(1).text());
535+
}
536+
}
537+
}
538+
539+
List<Element> paramListChildren = paramList.children();
540+
541+
String shortDescription = paramListChildren.get(1).children().get(1).text();
542+
String detailedDescription = paramListChildren.get(2).children().get(1).text();
543+
544+
return ParsedOrder.builder()
545+
.id(orderId)
546+
.statuses(statuses)
547+
.params(params)
548+
.shortDescription(shortDescription)
549+
.detailedDescription(detailedDescription)
550+
.price(price)
551+
.other(extractPreviewUserFromProductPage(funPayDocument))
552+
.build();
553+
} catch (IOException e) {
554+
throw new FunPayApiException(e.getLocalizedMessage());
555+
}
556+
}
557+
500558
/** {@inheritDoc} */
501559
@Override
502560
public CsrfTokenAndPHPSESSID parseCsrfTokenAndPHPSESSID(@NonNull String goldenKey)
@@ -923,6 +981,38 @@ private List<ParsedSellerReview> parseSellerReviewsInternal(
923981
return currentSellerReviews;
924982
}
925983

984+
private ParsedPreviewUser extractPreviewUserFromProductPage(Document funPayDocument) {
985+
System.out.println();
986+
Element previewSellerUsernameElement =
987+
funPayDocument.getElementsByClass("media-user-name").first().selectFirst("a");
988+
Element previewSellerImgElement =
989+
funPayDocument.getElementsByClass("media-user").first().selectFirst("img");
990+
Element previewSellerReviewCountElement =
991+
funPayDocument.getElementsByClass("text-mini text-light mb5").first();
992+
993+
String previewSellerUsernameElementHrefAttributeValue =
994+
previewSellerUsernameElement.attr("href");
995+
996+
long previewSellerUserId =
997+
Long.parseLong(
998+
previewSellerUsernameElementHrefAttributeValue.substring(
999+
25, previewSellerUsernameElementHrefAttributeValue.length() - 1));
1000+
String previewSellerUsername = previewSellerUsernameElement.text();
1001+
String previewSellerAvatarPhotoLink = previewSellerImgElement.attr("src");
1002+
1003+
// if the previewUser has a regular photo
1004+
if (previewSellerAvatarPhotoLink.equals("/img/layout/avatar.png"))
1005+
previewSellerAvatarPhotoLink = null;
1006+
boolean isPreviewSellerOnline =
1007+
funPayDocument.getElementsByClass("media media-user online").first() != null;
1008+
return ParsedPreviewUser.builder()
1009+
.userId(previewSellerUserId)
1010+
.username(previewSellerUsername)
1011+
.avatarPhotoLink(previewSellerAvatarPhotoLink)
1012+
.isOnline(isPreviewSellerOnline)
1013+
.build();
1014+
}
1015+
9261016
private void extractReviewsFromReviewsHtml(
9271017
Document reviewsHtml, List<ParsedSellerReview> currentSellerReviews) {
9281018
List<Element> reviewContainerElements = reviewsHtml.getElementsByClass("review-container");

0 commit comments

Comments
 (0)