-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalysis.java
202 lines (162 loc) · 7.64 KB
/
Analysis.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package analysis;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import org.json.simple.*;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.json.simple.parser.JSONParser;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Analysis {
static ArrayList<String> appid = new ArrayList();
static ArrayList<String> setPrice = new ArrayList();
static ArrayList<String> singleGame = new ArrayList();
static String cookie;
static ArrayList<String> gamename = new ArrayList();
static Double price;
static int acceptableQuantity;
static Logger log = Logger.getLogger(Analysis.class.getName());
static String log4jConfPath = "log4j.properties";
public static void main(String[] args) throws Exception {
PropertyConfigurator.configure(log4jConfPath);
Scanner sc = new Scanner(System.in);
//See wiki - https://github.com/VaibhavBarot/Steam-card-analysis/wiki for info on how to get cookie
log.info("Enter cookie");
cookie = sc.nextLine();
//The minimum profitable (possibly breakeven) for games
log.info("Enter set price");
price = sc.nextDouble();
//Number of total cards sold per week
log.info("Enter quantity per week");
acceptableQuantity = sc.nextInt();
getCSV();
getCardLists();
log.info("Press enter to exit");
System.in.read();
}
@SuppressWarnings({"CallToPrintStackTrace", "SleepWhileInLoop"})
public static void getCardLists() throws Exception {
int totalGames=1;
int totalQuantityPerGame;
PrintWriter pw = new PrintWriter(
new BufferedWriter(
new FileWriter("Profit.txt", true)
), true
);
for (String i : appid) {
log.info("(" + totalGames + "\\" + appid.size() + ")");
int retries = 0;
if (retries < 3) {
try {
Document doc = Jsoup.connect("https://steamcommunity.com/market/search?category_753_Game%5B%5D=tag_app_" + i + "&category_753_cardborder%5B%5D=tag_cardborder_0&category_753_item_class%5B%5D=tag_item_class_2&appid=753").get();
Elements card_names = doc.getElementsByClass("market_listing_item_name");
card_names.forEach(element -> singleGame.add(element.text()));
totalQuantityPerGame = getPriceHistory(i);
singleGame.clear();
Thread.sleep(1000);
if (totalQuantityPerGame >= acceptableQuantity ) {
log.info(gamename.get(appid.indexOf(i)));
log.info("Appid :" + i);
log.info("Total Quantity Sold = " + totalQuantityPerGame);
log.info("Status : Accepted");
pw.println(gamename.get(appid.indexOf(i)) + "-" +i);
} else {
log.info(gamename.get(appid.indexOf(i)));
log.info("Appid :" + i);
log.info("Total Quantity Sold = " + totalQuantityPerGame);
log.info("Status : Ignored");
}
} catch (HttpStatusException http) {
if (http.getStatusCode() == 429) {
// 4 minutes is safe, does not work below it
log.warn("Too many requests,waiting upto 4 minutes");
Thread.sleep(240000);
retries++;
}
}
}
totalGames++;
}
}
@SuppressWarnings({"UseSpecificCatch", "CallToPrintStackTrace"})
static public ArrayList<String> getCSV() {
try {
// Look wiki on how to get csv of games
Reader in = new FileReader("game.csv");
Iterable<CSVRecord> records = CSVFormat.EXCEL.withFirstRecordAsHeader().parse(in);
//Using CSV common from Apache for csv files
for (CSVRecord record : records) {
//While parsing java does not accept comma for numerical values like 2,300
if (Double.parseDouble(record.get(5).replace(",",""))>= price) {
appid.add(record.get(15));
gamename.add(record.get(0));
setPrice.add(record.get(5));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return appid;
}
@SuppressWarnings({"CallToPrintStackTrace"})
static public int getPriceHistory(String currentAppid) throws Exception {
int currentQuantity = 0;
for (String i : singleGame) {
try {
// Common steam link for fetching items, change parameters accordingly
String priceLink = "https://steamcommunity.com/market/pricehistory/?country=US¤cy=1&appid=753&market_hash_name=";
// Standard url encoding - https://www.w3schools.com/tags/ref_urlencode.asp
String pricehistory = Jsoup.connect(priceLink + currentAppid + "-" + i.replace("&","%26").replace(" ", "%20").replace(",","%2C").replace("#","%23")).cookie("steamLoginSecure", cookie).ignoreContentType(true).execute().body();
currentQuantity += analyze(pricehistory);
Thread.sleep(1000);
} catch (HttpStatusException http) {
if (http.getStatusCode() == 500) {
// Sometimes some characters don't parse properly
log.warn("Internal Server Error, cannot get price history for " + i);
}
}
}
return currentQuantity;
}
static public int analyze(String pricehistory) throws Exception {
int totalQuantityPerGame = 0;
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd yyyy");
Long dayDifference;
Date todayDate = new Date();
Date soldDate;
JSONParser parser = new JSONParser();
JSONObject prices = (JSONObject) parser.parse(pricehistory);
// Fetching all price histories
JSONArray array = (JSONArray) prices.get("prices");
for (Object dates : array) {
JSONArray day = (JSONArray) dates;
String dateInString = (String) day.get(0);
soldDate = formatter.parse(dateInString);
dayDifference = todayDate.getTime() - soldDate.getTime();
dayDifference = dayDifference / (1000 * 60 * 60 * 24);
totalQuantityPerGame = totalQuantityPerGame + quantitySold(dayDifference, day);
}
return totalQuantityPerGame;
}
static public int quantitySold(Long dayDifference, JSONArray k) {
int quantitySold = 0;
String currentQuantity;
//change number of days accordingly
if (dayDifference <= 7) {
currentQuantity = (String) (k.get(2));
quantitySold = quantitySold + Integer.parseInt(currentQuantity);
}
return quantitySold;
}
}