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
|
package ml.docilealligator.infinityforreddit.subreddit;
import android.os.Handler;
import androidx.annotation.Nullable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import ml.docilealligator.infinityforreddit.utils.Utils;
public class ParseSubredditData {
public static void parseSubredditDataSync(Handler handler, @Nullable String response,
FetchSubredditData.FetchSubredditDataListener fetchSubredditDataListener) {
if (response == null) {
handler.post(() -> fetchSubredditDataListener.onFetchSubredditDataFail(false));
return;
}
try {
JSONObject data = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY);
int nCurrentOnlineSubscribers = data.getInt(JSONUtils.ACTIVE_USER_COUNT_KEY);
SubredditData subredditData = parseSubredditDataSync(data, true);
if (subredditData == null) {
handler.post(() -> fetchSubredditDataListener.onFetchSubredditDataFail(false));
} else {
handler.post(() -> fetchSubredditDataListener.onFetchSubredditDataSuccess(subredditData, nCurrentOnlineSubscribers));
}
} catch (JSONException e) {
e.printStackTrace();
handler.post(() -> fetchSubredditDataListener.onFetchSubredditDataFail(false));
}
}
public static void parseSubredditListingDataSync(Handler handler, @Nullable String response, boolean nsfw,
FetchSubredditData.FetchSubredditListingDataListener fetchSubredditListingDataListener) {
if (response == null) {
handler.post(fetchSubredditListingDataListener::onFetchSubredditListingDataFail);
return;
}
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray children = jsonObject.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
ArrayList<SubredditData> subredditListingData = new ArrayList<>();
for (int i = 0; i < children.length(); i++) {
JSONObject data = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
SubredditData subredditData = parseSubredditDataSync(data, nsfw);
if (subredditData != null) {
subredditListingData.add(subredditData);
}
}
String after = jsonObject.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
handler.post(() -> fetchSubredditListingDataListener.onFetchSubredditListingDataSuccess(subredditListingData, after));
} catch (JSONException e) {
e.printStackTrace();
handler.post(fetchSubredditListingDataListener::onFetchSubredditListingDataFail);
}
}
public static void parseSubredditListingData(Executor executor, Handler handler, @Nullable String response, boolean nsfw,
ParseSubredditListingDataListener parseSubredditListingDataListener) {
if (response == null) {
parseSubredditListingDataListener.onParseSubredditListingDataFail();
return;
}
executor.execute(() -> {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray children = jsonObject.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
ArrayList<SubredditData> subredditListingData = new ArrayList<>();
for (int i = 0; i < children.length(); i++) {
JSONObject data = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
SubredditData subredditData = parseSubredditDataSync(data, nsfw);
if (subredditData != null) {
subredditListingData.add(subredditData);
}
}
String after = jsonObject.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
handler.post(() -> parseSubredditListingDataListener.onParseSubredditListingDataSuccess(subredditListingData, after));
} catch (JSONException e) {
e.printStackTrace();
handler.post(parseSubredditListingDataListener::onParseSubredditListingDataFail);
}
});
}
@Nullable
private static SubredditData parseSubredditDataSync(JSONObject subredditDataJsonObject, boolean nsfw) throws JSONException {
boolean isNSFW = !subredditDataJsonObject.isNull(JSONUtils.OVER18_KEY) && subredditDataJsonObject.getBoolean(JSONUtils.OVER18_KEY);
if (!nsfw && isNSFW) {
return null;
}
String id = subredditDataJsonObject.getString(JSONUtils.NAME_KEY);
String subredditFullName = subredditDataJsonObject.getString(JSONUtils.DISPLAY_NAME_KEY);
String description = subredditDataJsonObject.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim();
String sidebarDescription = Utils.modifyMarkdown(subredditDataJsonObject.getString(JSONUtils.DESCRIPTION_KEY).trim());
long createdUTC = subredditDataJsonObject.getLong(JSONUtils.CREATED_UTC_KEY) * 1000;
String suggestedCommentSort = subredditDataJsonObject.getString(JSONUtils.SUGGESTED_COMMENT_SORT_KEY);
String bannerImageUrl;
if (subredditDataJsonObject.isNull(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY)) {
bannerImageUrl = "";
} else {
bannerImageUrl = subredditDataJsonObject.getString(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY);
}
if (bannerImageUrl.equals("") && !subredditDataJsonObject.isNull(JSONUtils.BANNER_IMG_KEY)) {
bannerImageUrl = subredditDataJsonObject.getString(JSONUtils.BANNER_IMG_KEY);
}
String iconUrl;
if (subredditDataJsonObject.isNull(JSONUtils.COMMUNITY_ICON_KEY)) {
iconUrl = "";
} else {
iconUrl = subredditDataJsonObject.getString(JSONUtils.COMMUNITY_ICON_KEY);
}
if (iconUrl.equals("") && !subredditDataJsonObject.isNull(JSONUtils.ICON_IMG_KEY)) {
iconUrl = subredditDataJsonObject.getString(JSONUtils.ICON_IMG_KEY);
}
int nSubscribers = 0;
if (!subredditDataJsonObject.isNull(JSONUtils.SUBSCRIBERS_KEY)) {
nSubscribers = subredditDataJsonObject.getInt(JSONUtils.SUBSCRIBERS_KEY);
}
return new SubredditData(id, subredditFullName, iconUrl, bannerImageUrl, description,
sidebarDescription, nSubscribers, createdUTC, suggestedCommentSort, isNSFW);
}
public interface ParseSubredditListingDataListener {
void onParseSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after);
void onParseSubredditListingDataFail();
}
}
|