diff options
Diffstat (limited to 'src/geddit.js')
-rw-r--r-- | src/geddit.js | 392 |
1 files changed, 392 insertions, 0 deletions
diff --git a/src/geddit.js b/src/geddit.js new file mode 100644 index 0000000..825c3a9 --- /dev/null +++ b/src/geddit.js | |||
@@ -0,0 +1,392 @@ | |||
1 | class Geddit { | ||
2 | constructor() { | ||
3 | this.host = "https://www.reddit.com"; | ||
4 | this.parameters = { | ||
5 | limit: 25, | ||
6 | include_over_18: true, | ||
7 | } | ||
8 | this.search_params = { | ||
9 | limit: 25, | ||
10 | include_over_18: true, | ||
11 | type: "sr,link,user", | ||
12 | } | ||
13 | } | ||
14 | |||
15 | async getSubmissions(sort = null, subreddit = null, options = {}) { | ||
16 | let params = { | ||
17 | limit: 25, | ||
18 | include_over_18: true, | ||
19 | } | ||
20 | |||
21 | sort = sort ? sort : "hot"; | ||
22 | subreddit = subreddit ? "/r/" + subreddit : ""; | ||
23 | |||
24 | return await fetch(this.host + subreddit + `/${sort}.json?` + new URLSearchParams(Object.assign(params, options))) | ||
25 | .then(res => res.json()) | ||
26 | .then(json => json.data) | ||
27 | .then(data => ({ | ||
28 | after: data.after, | ||
29 | posts: data.children | ||
30 | })) | ||
31 | .catch(err => null); | ||
32 | |||
33 | } | ||
34 | |||
35 | async getDomainHot(domain, options = this.parameters) { | ||
36 | return await fetch(this.host + "/domain/" + domain + "/hot.json?" + new URLSearchParams(options)) | ||
37 | .then(res => res.json()) | ||
38 | .then(json => json.data) | ||
39 | .then(data => ({ | ||
40 | after: data.after, | ||
41 | posts: data.children | ||
42 | })) | ||
43 | .catch(err => null); | ||
44 | } | ||
45 | |||
46 | async getDomainBest(domain, options = this.parameters) { | ||
47 | return await fetch(this.host + "/domain/" + domain + "/best.json?" + new URLSearchParams(options)) | ||
48 | .then(res => res.json()) | ||
49 | .then(json => json.data) | ||
50 | .then(data => ({ | ||
51 | after: data.after, | ||
52 | posts: data.children | ||
53 | })) | ||
54 | .catch(err => null); | ||
55 | } | ||
56 | |||
57 | async getDomainTop(domain, options = this.parameters) { | ||
58 | return await fetch(this.host + "/domain/" + domain + "/top.json?" + new URLSearchParams(options)) | ||
59 | .then(res => res.json()) | ||
60 | .then(json => json.data) | ||
61 | .then(data => ({ | ||
62 | after: data.after, | ||
63 | posts: data.children | ||
64 | })) | ||
65 | .catch(err => null); | ||
66 | } | ||
67 | |||
68 | async getDomainNew(domain, options = this.parameters) { | ||
69 | return await fetch(this.host + "/domain/" + domain + "/new.json?" + new URLSearchParams(options)) | ||
70 | .then(res => res.json()) | ||
71 | .then(json => json.data) | ||
72 | .then(data => ({ | ||
73 | after: data.after, | ||
74 | posts: data.children | ||
75 | })) | ||
76 | .catch(err => null); | ||
77 | } | ||
78 | |||
79 | async getDomainRising(domain, options = this.parameters) { | ||
80 | return await fetch(this.host + "/domain/" + domain + "/rising.json?" + new URLSearchParams(options)) | ||
81 | .then(res => res.json()) | ||
82 | .then(json => json.data) | ||
83 | .then(data => ({ | ||
84 | after: data.after, | ||
85 | posts: data.children | ||
86 | })) | ||
87 | .catch(err => null); | ||
88 | } | ||
89 | |||
90 | async getDomainControversial(domain, options = this.parameters) { | ||
91 | return await fetch(this.host + "/domain/" + domain + "/controversial.json?" + new URLSearchParams(options)) | ||
92 | .then(res => res.json()) | ||
93 | .then(json => json.data) | ||
94 | .then(data => ({ | ||
95 | after: data.after, | ||
96 | posts: data.children | ||
97 | })) | ||
98 | .catch(err => null); | ||
99 | } | ||
100 | |||
101 | async getSubreddit(subreddit) { | ||
102 | return await fetch(this.host + "/r/" + subreddit + "/about.json") | ||
103 | .then(res => res.json()) | ||
104 | .then(json => json.data) | ||
105 | .catch(err => null); | ||
106 | } | ||
107 | |||
108 | async getSubredditRules(subreddit) { | ||
109 | return await fetch(this.host + "/r/" + subreddit + "/about/rules.json") | ||
110 | .then(res => res.json()) | ||
111 | .then(json => json.data) | ||
112 | .catch(err => null); | ||
113 | } | ||
114 | |||
115 | async getSubredditModerators(subreddit) { | ||
116 | return await fetch(this.host + "/r/" + subreddit + "/about/moderators.json") | ||
117 | .then(res => res.json()) | ||
118 | .then(json => json.data) | ||
119 | .then(data = ({ | ||
120 | users: data.children | ||
121 | })) | ||
122 | .catch(err => null); | ||
123 | } | ||
124 | |||
125 | async getSubredditWikiPages(subreddit) { | ||
126 | return await fetch(this.host + "/r/" + subreddit + "/wiki/pages.json") | ||
127 | .then(res => res.json()) | ||
128 | .then(json => json.data) | ||
129 | .catch(err => null); | ||
130 | } | ||
131 | |||
132 | async getSubredditWikiPage(subreddit, page) { | ||
133 | return await fetch(this.host + "/r/" + subreddit + "/wiki/" + page + ".json") | ||
134 | .then(res => res.json()) | ||
135 | .then(json => json.data) | ||
136 | .catch(err => null); | ||
137 | } | ||
138 | |||
139 | async getSubredditWikiPageRevisions(subreddit, page) { | ||
140 | return await fetch(this.host + "/r/" + subreddit + "/wiki/revisions" + page + ".json") | ||
141 | .then(res => res.json()) | ||
142 | .then(json => json.data.children) | ||
143 | .catch(err => null); | ||
144 | } | ||
145 | |||
146 | async getPopularSubreddits(options = this.parameters) { | ||
147 | return await fetch(this.host + "/subreddits/popular.json?" + new URLSearchParams(options)) | ||
148 | .then(res => res.json()) | ||
149 | .then(json => json.data) | ||
150 | .then(data => ({ | ||
151 | after: data.after, | ||
152 | subreddits: data.children | ||
153 | })) | ||
154 | .catch(err => null); | ||
155 | } | ||
156 | |||
157 | async getNewSubreddits(options = this.parameters) { | ||
158 | return await fetch(this.host + "/subreddits/new.json?" + new URLSearchParams(options)) | ||
159 | .then(res => res.json()) | ||
160 | .then(json => json.data) | ||
161 | .then(data => ({ | ||
162 | after: data.after, | ||
163 | subreddits: data.children | ||
164 | })) | ||
165 | .catch(err => null); | ||
166 | } | ||
167 | |||
168 | async getPremiumSubreddits(options = this.parameters) { | ||
169 | return await fetch(this.host + "/subreddits/premium.json?" + new URLSearchParams(options)) | ||
170 | .then(res => res.json()) | ||
171 | .then(json => json.data) | ||
172 | .then(data => ({ | ||
173 | after: data.after, | ||
174 | subreddits: data.children | ||
175 | })) | ||
176 | .catch(err => null); | ||
177 | } | ||
178 | |||
179 | async getDefaultSubreddits(options = this.parameters) { | ||
180 | return await fetch(this.host + "/subreddits/default.json?" + new URLSearchParams(options)) | ||
181 | .then(res => res.json()) | ||
182 | .then(json => json.data) | ||
183 | .then(data => ({ | ||
184 | after: data.after, | ||
185 | subreddits: data.children | ||
186 | })) | ||
187 | .catch(err => null); | ||
188 | } | ||
189 | |||
190 | async getPopularUsers(options = this.parameters) { | ||
191 | return await fetch(this.host + "/users/popular.json?" + new URLSearchParams(options)) | ||
192 | .then(res => res.json()) | ||
193 | .then(json => json.data) | ||
194 | .then(data => ({ | ||
195 | after: data.after, | ||
196 | users: data.children | ||
197 | })) | ||
198 | .catch(err => null); | ||
199 | } | ||
200 | |||
201 | async getNewUsers(options = this.parameters) { | ||
202 | return await fetch(this.host + "/users/new.json?" + new URLSearchParams(options)) | ||
203 | .then(res => res.json()) | ||
204 | .then(json => json.data) | ||
205 | .then(data => ({ | ||
206 | after: data.after, | ||
207 | users: data.children | ||
208 | })) | ||
209 | .catch(err => null); | ||
210 | } | ||
211 | |||
212 | async searchSubmissions(query, options = {}) { | ||
213 | options.q = query; | ||
214 | options.type = "link"; | ||
215 | |||
216 | let params = { | ||
217 | limit: 25, | ||
218 | include_over_18: true | ||
219 | } | ||
220 | |||
221 | console.log(this.host + "/search.json?" + new URLSearchParams(Object.assign(params, options))); | ||
222 | |||
223 | return await fetch(this.host + "/search.json?" + new URLSearchParams(Object.assign(params, options))) | ||
224 | .then(res => res.json()) | ||
225 | .then(json => json.data) | ||
226 | .then(data => ({ | ||
227 | after: data.after, | ||
228 | items: data.children | ||
229 | })) | ||
230 | .catch(err => null); | ||
231 | } | ||
232 | |||
233 | async searchSubreddits(query, options = {}) { | ||
234 | options.q = query; | ||
235 | |||
236 | let params = { | ||
237 | limit: 25, | ||
238 | include_over_18: true | ||
239 | } | ||
240 | |||
241 | return await fetch(this.host + "/subreddits/search.json?" + new URLSearchParams(Object.assign(params, options))) | ||
242 | .then(res => res.json()) | ||
243 | .then(json => json.data) | ||
244 | .then(data => ({ | ||
245 | after: data.after, | ||
246 | items: data.children | ||
247 | })) | ||
248 | .catch(err => null); | ||
249 | } | ||
250 | |||
251 | async searchUsers(query, options = {}) { | ||
252 | options.q = query; | ||
253 | |||
254 | let params = { | ||
255 | limit: 25, | ||
256 | include_over_18: true | ||
257 | } | ||
258 | |||
259 | return await fetch(this.host + "/users/search.json?" + new URLSearchParams(Object.assign(params, options))) | ||
260 | .then(res => res.json()) | ||
261 | .then(json => json.data) | ||
262 | .then(data => ({ | ||
263 | after: data.after, | ||
264 | items: data.children | ||
265 | })) | ||
266 | .catch(err => null); | ||
267 | } | ||
268 | |||
269 | async searchAll(query, subreddit = null, options = {}) { | ||
270 | options.q = query; | ||
271 | subreddit = subreddit ? "/r/" + subreddit : ""; | ||
272 | |||
273 | let params = { | ||
274 | limit: 25, | ||
275 | include_over_18: true, | ||
276 | type: "sr,link,user", | ||
277 | } | ||
278 | |||
279 | return await fetch(this.host + subreddit + "/search.json?" + new URLSearchParams(Object.assign(params, options))) | ||
280 | .then(res => res.json()) | ||
281 | .then(json => Array.isArray(json) ? ({ | ||
282 | after: json[1].data.after, | ||
283 | items: json[0].data.children.concat(json[1].data.children) | ||
284 | }) : ({ | ||
285 | after: json.data.after, | ||
286 | items: json.data.children | ||
287 | })) | ||
288 | .catch(err => null); | ||
289 | } | ||
290 | |||
291 | async getSubmission(id) { | ||
292 | return await fetch(this.host + "/by_id/" + id + ".json") | ||
293 | .then(res => res.json()) | ||
294 | .then(json => json.data.children[0].data) | ||
295 | .catch(err => null); | ||
296 | } | ||
297 | |||
298 | async getSubmissionComments(id, options = this.parameters) { | ||
299 | return await fetch(this.host + "/comments/" + id + ".json?" + new URLSearchParams(options)) | ||
300 | .then(res => res.json()) | ||
301 | .then(json => ({ | ||
302 | submission: json[0].data.children[0], | ||
303 | comments: json[1].data.children | ||
304 | })) | ||
305 | .catch(err => null); | ||
306 | } | ||
307 | |||
308 | async getSubredditComments(subreddit, options = this.parameters) { | ||
309 | return await fetch(this.host + "/r/" + subreddit + "/comments.json?" + new URLSearchParams(options)) | ||
310 | .then(res => res.json()) | ||
311 | .then(json => json.data.children) | ||
312 | .catch(err => null); | ||
313 | } | ||
314 | |||
315 | async getUser(username) { | ||
316 | return await fetch(this.host + "/user/" + username + "/about.json") | ||
317 | .then(res => res.json()) | ||
318 | .then(json => json.data) | ||
319 | .catch(err => null); | ||
320 | } | ||
321 | |||
322 | async getUserOverview(username, options = this.parameters) { | ||
323 | return await fetch(this.host + "/user/" + username + "/overview.json?" + new URLSearchParams(options)) | ||
324 | .then(res => res.json()) | ||
325 | .then(json => json.data) | ||
326 | .then(data => ({ | ||
327 | after: data.after, | ||
328 | items: data.children | ||
329 | })) | ||
330 | .catch(err => null); | ||
331 | } | ||
332 | |||
333 | async getUserComments(username, options = this.parameters) { | ||
334 | return await fetch(this.host + "/user/" + username + "/comments.json?" + new URLSearchParams(options)) | ||
335 | .then(res => res.json()) | ||
336 | .then(json => json.data) | ||
337 | .then(data => ({ | ||
338 | after: data.after, | ||
339 | items: data.children | ||
340 | })) | ||
341 | .catch(err => null); | ||
342 | } | ||
343 | |||
344 | async getUserSubmissions(username, options = this.parameters) { | ||
345 | return await fetch(this.host + "/user/" + username + "/submitted.json?" + new URLSearchParams(options)) | ||
346 | .then(res => res.json()) | ||
347 | .then(json => json.data) | ||
348 | .then(data => ({ | ||
349 | after: data.after, | ||
350 | items: data.children | ||
351 | })) | ||
352 | .catch(err => null); | ||
353 | } | ||
354 | |||
355 | async getLiveThread(id) { | ||
356 | return await fetch(this.host + "/live/" + id + "/about.json") | ||
357 | .then(res => res.json()) | ||
358 | .then(json => json.data) | ||
359 | .catch(err => null); | ||
360 | } | ||
361 | |||
362 | async getLiveThreadUpdates(id, options = this.parameters) { | ||
363 | return await fetch(this.host + "/live/" + id + ".json?" + new URLSearchParams(options)) | ||
364 | .then(res => res.json()) | ||
365 | .then(json => json.data.children) | ||
366 | .catch(err => null); | ||
367 | } | ||
368 | |||
369 | |||
370 | async getLiveThreadContributors(id, options = this.parameters) { | ||
371 | return await fetch(this.host + "/live/" + id + "/contributors.json?" + new URLSearchParams(options)) | ||
372 | .then(res => res.json()) | ||
373 | .then(json => json.data.children) | ||
374 | .catch(err => null); | ||
375 | } | ||
376 | |||
377 | async getLiveThreadDiscussions(id, options = this.parameters) { | ||
378 | return await fetch(this.host + "/live/" + id + "/discussions.json?" + new URLSearchParams(options)) | ||
379 | .then(res => res.json()) | ||
380 | .then(json => json.data.children) | ||
381 | .catch(err => null); | ||
382 | } | ||
383 | |||
384 | async getLiveThreadsNow(options = this.parameters) { | ||
385 | return await fetch(this.host + "/live/happening_now.json?" + new URLSearchParams(options)) | ||
386 | .then(res => res.json()) | ||
387 | .then(json => json.data.children) | ||
388 | .catch(err => null); | ||
389 | } | ||
390 | } | ||
391 | |||
392 | export { Geddit } | ||