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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
package kr.syeyoung.dungeonsguide.utils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeSet;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
public class AhUtils {
public static volatile Map<String, AuctionData> auctions = new HashMap<String, AuctionData>();
private static Map<String, AuctionData> semi_auctions = new HashMap<String, AuctionData>();
public static Timer timer = new Timer();
public static int totalAuctions = 0;
public static void registerTimer() {
timer.schedule(new TimerTask() {
public void run() {
AhUtils.loadAuctions();
}
}, 0L, 1800000L);
}
public static void loadAuctions() {
try {
int i = 0;
do {
} while (loadPage(i++));
loadBazaar();
auctions = semi_auctions;
semi_auctions = new HashMap<String, AuctionData>();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void loadBazaar() throws IOException {
System.out.println("Fetching bazaar data");
URL url = new URL("https://api.hypixel.net/skyblock/bazaar");
InputStreamReader reader = new InputStreamReader(url.openStream());
JsonObject object = (JsonObject)(new JsonParser()).parse(reader);
boolean success = object.get("success").getAsBoolean();
if (!success)
return;
JsonObject element = object.getAsJsonObject("products");
for (Map.Entry<String, JsonElement> product : (Iterable<Map.Entry<String, JsonElement>>)element.entrySet()) {
String id = product.getKey();
AuctionData auctionData = semi_auctions.get(id);
boolean notexisted = (auctionData == null);
if (notexisted)
auctionData = new AuctionData(id);
auctionData.sellPrice = ((JsonElement)product.getValue()).getAsJsonObject().getAsJsonObject("quick_status").get("sellPrice").getAsInt();
auctionData.buyPrice = ((JsonElement)product.getValue()).getAsJsonObject().getAsJsonObject("quick_status").get("buyPrice").getAsInt();
if (notexisted)
semi_auctions.put(id, auctionData);
}
}
public static boolean loadPage(int page) throws IOException {
System.out.println("Fetching page " + page + " of auctions");
URL url = new URL("https://api.hypixel.net/skyblock/auctions?page=" + page);
InputStreamReader reader = new InputStreamReader(url.openStream());
JsonObject object = (JsonObject)(new JsonParser()).parse(reader);
boolean success = object.get("success").getAsBoolean();
if (!success)
return false;
int maxPage = object.get("totalPages").getAsInt();
int totalAuctions = object.get("totalAuctions").getAsInt();
System.out.println("Fetched page " + page + "/" + maxPage + " of auctions! (" + totalAuctions + " total)");
JsonArray array = object.get("auctions").getAsJsonArray();
for (JsonElement element2 : array) {
JsonObject element = element2.getAsJsonObject();
JsonElement isBin = element.get("bin");
if (isBin == null || !isBin.getAsBoolean())
continue;
byte[] itemData = Base64.decode(element.get("item_bytes").getAsString().replace("\\u003d", "="));
NBTTagCompound nbtTagCompound = CompressedStreamTools.readCompressed(new ByteArrayInputStream(itemData));
NBTTagCompound acutalItem = (NBTTagCompound)nbtTagCompound.getTagList("i", 10).get(0);
NBTTagCompound attributes = acutalItem.getCompoundTag("tag").getCompoundTag("ExtraAttributes");
String id = attributes.getString("id");
if (id.equals("ENCHANTED_BOOK")) {
NBTTagCompound enchants = attributes.getCompoundTag("enchantments");
Set<String> keys = enchants.getKeySet();
if (keys.size() != 1)
continue;
String ench = keys.iterator().next();
int lv = enchants.getInteger(ench);
id = id + "::" + ench + "-" + lv;
}
AuctionData auctionData = semi_auctions.get(id);
boolean notexisted = (auctionData == null);
if (notexisted)
auctionData = new AuctionData(id);
auctionData.prices.add(element.get("starting_bid").getAsInt());
if (notexisted)
semi_auctions.put(id, auctionData);
}
return (page < maxPage);
}
public static class AuctionData {
public String id;
public SortedSet<Integer> prices;
public int sellPrice = -1;
public int buyPrice = -1;
public AuctionData(String id) {
this.id = id;
this.prices = new TreeSet<Integer>();
}
}
}
|