forked from TheCyaniteProject/exit_code_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatreon.java
More file actions
102 lines (90 loc) · 3.5 KB
/
Copy pathPatreon.java
File metadata and controls
102 lines (90 loc) · 3.5 KB
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
package exitcode;
import javafx.application.*;
import org.json.JSONArray;
import org.json.JSONObject;
import exitcode.Main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
// Internal dependancies: Main, Logger, LoadingScreen
public class Patreon {
private static String creatorID = ""; // Remove before commiting publicly!
public static void updatePatronList() {
JSONArray pledges;
Main.patrons.clear();
if (Patreon.patreonIsAvailable()) {
LoadingScreen.setLoadingBar(0.25);
LoadingScreen.setTaskText("Fetching Patron List");
pledges = getJSON("https://api.patreon.com/oauth2/api/" ,"campaigns/844621/pledges", creatorID).getJSONArray("data");
for(int i = 0; i < pledges.length(); i++) {
if (pledges.getJSONObject(i).getJSONObject("attributes").getInt("amount_cents") >= 500) {
Main.patrons.add(
getJSON("https://www.patreon.com/api/user/",
pledges.getJSONObject(i).getJSONObject("relationships").getJSONObject("patron").getJSONObject("data").getInt("id"))
.getJSONObject("data").getJSONObject("attributes").getString("full_name")
);
}
}
LoadingScreen.setLoadingBar(1.0);
} else {
LoadingScreen.setLoadingBar(1.0);
Main.patrons.add("Could not connect to Patreon");
Logger.warn("No internet connection");
}
}
private static boolean patreonIsAvailable() {
LoadingScreen.setTaskText("Checking Internet Connection");
try {
final URL url = new URL("https://www.patreon.com");
final URLConnection conn = url.openConnection();
conn.connect();
return true;
} catch (MalformedURLException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
return false;
}
}
private static JSONObject getJSON(String prefix, Integer suffix) { // Move to ExitParser later
try
{
URL url = new URL(prefix.concat(suffix.toString()));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
BufferedReader stream = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder result = new StringBuilder();
String line;
while ((line = stream.readLine()) != null) {
result.append(line);
}
stream.close();
return new JSONObject(result.toString());
}
catch (MalformedURLException e) {}
catch (IOException e) {}
return null;
}
private static JSONObject getJSON(String prefix, String suffix, String _accessToken) { // Move to ExitParser later
try {
URL url = new URL(prefix.concat(suffix));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Bearer ".concat(_accessToken));
BufferedReader stream = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder result = new StringBuilder();
String line;
while ((line = stream.readLine()) != null)
{
result.append(line);
}
stream.close();
return new JSONObject(result.toString());
} catch (Exception e) {
/* Ignore these specific exceptions */
}
return null;
}
}