-
Notifications
You must be signed in to change notification settings - Fork 0
/
StockXRequest.java
52 lines (49 loc) · 2.06 KB
/
StockXRequest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package GUI.Overlays.ItemCreation;
import java.util.ArrayList;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.jsoup.Jsoup;
/**
* @author Teeds - Theo K
*/
public class StockXRequest {
String search;
CreationSearchArea area;
/**
* Creates a request search object
* @param area the parent panel to add the found searches to
* @param name the name of the item to search
*/
public StockXRequest(CreationSearchArea area, String name) {
this.search = name;
this.area = area;
run();
}
/**
* Sends a request to StockX search and returns all products found
*/
private void run() {
try {
String url = "https://stockx.com/api/browse?&_search="+search+"&dataType=product";
String json = Jsoup.connect(url).ignoreContentType(true).execute().body();
JSONParser parser = new JSONParser();
Object obj = parser.parse(json);
JSONObject jsonObject = (JSONObject) obj;
JSONArray groupOptions = (JSONArray) jsonObject.get("Products");
Iterator<JSONObject> iterator = groupOptions.iterator();
int x = 0;
while (area.getStockXSearch().equals(search) && x < 20 && iterator.hasNext()) {
JSONObject product = iterator.next();
StockXProduct product_info = new StockXProduct((String) product.get("title"), (long) product.get("retailPrice"), (String) product.get("brand"), (String) product.get("shortDescription"), (String) product.get("colorway"), (String) product.get("styleId"), (String) product.get("releaseDate"));
product_info.setSearched(search);
product_info.setSmallImageUrl((String) ((JSONObject)product.get("media")).get("thumbUrl"));
area.addSearchItem(product_info);
x++;
}
} catch(Exception e) {
e.printStackTrace();
}
}
}