diff options
author | Akshay <[email protected]> | 2024-11-05 17:42:41 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2024-11-05 17:42:41 +0000 |
commit | 16139326f2fb5aea23614ac779171e31390b8f72 (patch) | |
tree | 851ab235a6cbbab4f27ee6fd92f584501873e030 /src | |
parent | ff789b09c45e0a9dcc4f39cdf5948e3c589a765a (diff) |
add biome
Diffstat (limited to 'src')
-rw-r--r-- | src/geddit.js | 894 | ||||
-rw-r--r-- | src/index.js | 21 | ||||
-rw-r--r-- | src/mixins/comment.pug | 4 | ||||
-rw-r--r-- | src/routes/index.js | 96 |
4 files changed, 568 insertions, 447 deletions
diff --git a/src/geddit.js b/src/geddit.js index 64e1564..d7e54e2 100644 --- a/src/geddit.js +++ b/src/geddit.js | |||
@@ -1,390 +1,510 @@ | |||
1 | class Geddit { | 1 | class Geddit { |
2 | constructor() { | 2 | constructor() { |
3 | this.host = "https://www.reddit.com"; | 3 | this.host = "https://www.reddit.com"; |
4 | this.parameters = { | 4 | this.parameters = { |
5 | limit: 25, | 5 | limit: 25, |
6 | include_over_18: true, | 6 | include_over_18: true, |
7 | } | 7 | }; |
8 | this.search_params = { | 8 | this.search_params = { |
9 | limit: 25, | 9 | limit: 25, |
10 | include_over_18: true, | 10 | include_over_18: true, |
11 | type: "sr,link,user", | 11 | type: "sr,link,user", |
12 | } | 12 | }; |
13 | } | 13 | } |
14 | 14 | ||
15 | async getSubmissions(sort = null, subreddit = null, options = {}) { | 15 | async getSubmissions(sort = null, subreddit = null, options = {}) { |
16 | let params = { | 16 | const params = { |
17 | limit: 20, | 17 | limit: 20, |
18 | include_over_18: true, | 18 | include_over_18: true, |
19 | } | 19 | }; |
20 | 20 | ||
21 | sort = sort ? sort : "hot"; | 21 | sort = sort ? sort : "hot"; |
22 | subreddit = subreddit ? "/r/" + subreddit : ""; | 22 | subreddit = subreddit ? "/r/" + subreddit : ""; |
23 | 23 | ||
24 | return await fetch(this.host + subreddit + `/${sort}.json?` + new URLSearchParams(Object.assign(params, options))) | 24 | return await fetch( |
25 | .then(res => res.json()) | 25 | this.host + |
26 | .then(json => json.data) | 26 | subreddit + |
27 | .then(data => ({ | 27 | `/${sort}.json?` + |
28 | after: data.after, | 28 | new URLSearchParams(Object.assign(params, options)), |
29 | posts: data.children | 29 | ) |
30 | })) | 30 | .then((res) => res.json()) |
31 | .catch(err => null); | 31 | .then((json) => json.data) |
32 | 32 | .then((data) => ({ | |
33 | } | 33 | after: data.after, |
34 | 34 | posts: data.children, | |
35 | async getDomainHot(domain, options = this.parameters) { | 35 | })) |
36 | return await fetch(this.host + "/domain/" + domain + "/hot.json?" + new URLSearchParams(options)) | 36 | .catch((err) => null); |
37 | .then(res => res.json()) | 37 | } |
38 | .then(json => json.data) | 38 | |
39 | .then(data => ({ | 39 | async getDomainHot(domain, options = this.parameters) { |
40 | after: data.after, | 40 | return await fetch( |
41 | posts: data.children | 41 | this.host + |
42 | })) | 42 | "/domain/" + |
43 | .catch(err => null); | 43 | domain + |
44 | } | 44 | "/hot.json?" + |
45 | 45 | new URLSearchParams(options), | |
46 | async getDomainBest(domain, options = this.parameters) { | 46 | ) |
47 | return await fetch(this.host + "/domain/" + domain + "/best.json?" + new URLSearchParams(options)) | 47 | .then((res) => res.json()) |
48 | .then(res => res.json()) | 48 | .then((json) => json.data) |
49 | .then(json => json.data) | 49 | .then((data) => ({ |
50 | .then(data => ({ | 50 | after: data.after, |
51 | after: data.after, | 51 | posts: data.children, |
52 | posts: data.children | 52 | })) |
53 | })) | 53 | .catch((err) => null); |
54 | .catch(err => null); | 54 | } |
55 | } | 55 | |
56 | 56 | async getDomainBest(domain, options = this.parameters) { | |
57 | async getDomainTop(domain, options = this.parameters) { | 57 | return await fetch( |
58 | return await fetch(this.host + "/domain/" + domain + "/top.json?" + new URLSearchParams(options)) | 58 | this.host + |
59 | .then(res => res.json()) | 59 | "/domain/" + |
60 | .then(json => json.data) | 60 | domain + |
61 | .then(data => ({ | 61 | "/best.json?" + |
62 | after: data.after, | 62 | new URLSearchParams(options), |
63 | posts: data.children | 63 | ) |
64 | })) | 64 | .then((res) => res.json()) |
65 | .catch(err => null); | 65 | .then((json) => json.data) |
66 | } | 66 | .then((data) => ({ |
67 | 67 | after: data.after, | |
68 | async getDomainNew(domain, options = this.parameters) { | 68 | posts: data.children, |
69 | return await fetch(this.host + "/domain/" + domain + "/new.json?" + new URLSearchParams(options)) | 69 | })) |
70 | .then(res => res.json()) | 70 | .catch((err) => null); |
71 | .then(json => json.data) | 71 | } |
72 | .then(data => ({ | 72 | |
73 | after: data.after, | 73 | async getDomainTop(domain, options = this.parameters) { |
74 | posts: data.children | 74 | return await fetch( |
75 | })) | 75 | this.host + |
76 | .catch(err => null); | 76 | "/domain/" + |
77 | } | 77 | domain + |
78 | 78 | "/top.json?" + | |
79 | async getDomainRising(domain, options = this.parameters) { | 79 | new URLSearchParams(options), |
80 | return await fetch(this.host + "/domain/" + domain + "/rising.json?" + new URLSearchParams(options)) | 80 | ) |
81 | .then(res => res.json()) | 81 | .then((res) => res.json()) |
82 | .then(json => json.data) | 82 | .then((json) => json.data) |
83 | .then(data => ({ | 83 | .then((data) => ({ |
84 | after: data.after, | 84 | after: data.after, |
85 | posts: data.children | 85 | posts: data.children, |
86 | })) | 86 | })) |
87 | .catch(err => null); | 87 | .catch((err) => null); |
88 | } | 88 | } |
89 | 89 | ||
90 | async getDomainControversial(domain, options = this.parameters) { | 90 | async getDomainNew(domain, options = this.parameters) { |
91 | return await fetch(this.host + "/domain/" + domain + "/controversial.json?" + new URLSearchParams(options)) | 91 | return await fetch( |
92 | .then(res => res.json()) | 92 | this.host + |
93 | .then(json => json.data) | 93 | "/domain/" + |
94 | .then(data => ({ | 94 | domain + |
95 | after: data.after, | 95 | "/new.json?" + |
96 | posts: data.children | 96 | new URLSearchParams(options), |
97 | })) | 97 | ) |
98 | .catch(err => null); | 98 | .then((res) => res.json()) |
99 | } | 99 | .then((json) => json.data) |
100 | 100 | .then((data) => ({ | |
101 | async getSubreddit(subreddit) { | 101 | after: data.after, |
102 | return await fetch(this.host + "/r/" + subreddit + "/about.json") | 102 | posts: data.children, |
103 | .then(res => res.json()) | 103 | })) |
104 | .then(json => json.data) | 104 | .catch((err) => null); |
105 | .catch(err => null); | 105 | } |
106 | } | 106 | |
107 | 107 | async getDomainRising(domain, options = this.parameters) { | |
108 | async getSubredditRules(subreddit) { | 108 | return await fetch( |
109 | return await fetch(this.host + "/r/" + subreddit + "/about/rules.json") | 109 | this.host + |
110 | .then(res => res.json()) | 110 | "/domain/" + |
111 | .then(json => json.data) | 111 | domain + |
112 | .catch(err => null); | 112 | "/rising.json?" + |
113 | } | 113 | new URLSearchParams(options), |
114 | 114 | ) | |
115 | async getSubredditModerators(subreddit) { | 115 | .then((res) => res.json()) |
116 | return await fetch(this.host + "/r/" + subreddit + "/about/moderators.json") | 116 | .then((json) => json.data) |
117 | .then(res => res.json()) | 117 | .then((data) => ({ |
118 | .then(json => json.data) | 118 | after: data.after, |
119 | .then(data = ({ | 119 | posts: data.children, |
120 | users: data.children | 120 | })) |
121 | })) | 121 | .catch((err) => null); |
122 | .catch(err => null); | 122 | } |
123 | } | 123 | |
124 | 124 | async getDomainControversial(domain, options = this.parameters) { | |
125 | async getSubredditWikiPages(subreddit) { | 125 | return await fetch( |
126 | return await fetch(this.host + "/r/" + subreddit + "/wiki/pages.json") | 126 | this.host + |
127 | .then(res => res.json()) | 127 | "/domain/" + |
128 | .then(json => json.data) | 128 | domain + |
129 | .catch(err => null); | 129 | "/controversial.json?" + |
130 | } | 130 | new URLSearchParams(options), |
131 | 131 | ) | |
132 | async getSubredditWikiPage(subreddit, page) { | 132 | .then((res) => res.json()) |
133 | return await fetch(this.host + "/r/" + subreddit + "/wiki/" + page + ".json") | 133 | .then((json) => json.data) |
134 | .then(res => res.json()) | 134 | .then((data) => ({ |
135 | .then(json => json.data) | 135 | after: data.after, |
136 | .catch(err => null); | 136 | posts: data.children, |
137 | } | 137 | })) |
138 | 138 | .catch((err) => null); | |
139 | async getSubredditWikiPageRevisions(subreddit, page) { | 139 | } |
140 | return await fetch(this.host + "/r/" + subreddit + "/wiki/revisions" + page + ".json") | 140 | |
141 | .then(res => res.json()) | 141 | async getSubreddit(subreddit) { |
142 | .then(json => json.data.children) | 142 | return await fetch(this.host + "/r/" + subreddit + "/about.json") |
143 | .catch(err => null); | 143 | .then((res) => res.json()) |
144 | } | 144 | .then((json) => json.data) |
145 | 145 | .catch((err) => null); | |
146 | async getPopularSubreddits(options = this.parameters) { | 146 | } |
147 | return await fetch(this.host + "/subreddits/popular.json?" + new URLSearchParams(options)) | 147 | |
148 | .then(res => res.json()) | 148 | async getSubredditRules(subreddit) { |
149 | .then(json => json.data) | 149 | return await fetch(this.host + "/r/" + subreddit + "/about/rules.json") |
150 | .then(data => ({ | 150 | .then((res) => res.json()) |
151 | after: data.after, | 151 | .then((json) => json.data) |
152 | subreddits: data.children | 152 | .catch((err) => null); |
153 | })) | 153 | } |
154 | .catch(err => null); | 154 | |
155 | } | 155 | async getSubredditModerators(subreddit) { |
156 | 156 | return await fetch(this.host + "/r/" + subreddit + "/about/moderators.json") | |
157 | async getNewSubreddits(options = this.parameters) { | 157 | .then((res) => res.json()) |
158 | return await fetch(this.host + "/subreddits/new.json?" + new URLSearchParams(options)) | 158 | .then((json) => json.data) |
159 | .then(res => res.json()) | 159 | .then( |
160 | .then(json => json.data) | 160 | (data = { |
161 | .then(data => ({ | 161 | users: data.children, |
162 | after: data.after, | 162 | }), |
163 | subreddits: data.children | 163 | ) |
164 | })) | 164 | .catch((err) => null); |
165 | .catch(err => null); | 165 | } |
166 | } | 166 | |
167 | 167 | async getSubredditWikiPages(subreddit) { | |
168 | async getPremiumSubreddits(options = this.parameters) { | 168 | return await fetch(this.host + "/r/" + subreddit + "/wiki/pages.json") |
169 | return await fetch(this.host + "/subreddits/premium.json?" + new URLSearchParams(options)) | 169 | .then((res) => res.json()) |
170 | .then(res => res.json()) | 170 | .then((json) => json.data) |
171 | .then(json => json.data) | 171 | .catch((err) => null); |
172 | .then(data => ({ | 172 | } |
173 | after: data.after, | 173 | |
174 | subreddits: data.children | 174 | async getSubredditWikiPage(subreddit, page) { |
175 | })) | 175 | return await fetch( |
176 | .catch(err => null); | 176 | this.host + "/r/" + subreddit + "/wiki/" + page + ".json", |
177 | } | 177 | ) |
178 | 178 | .then((res) => res.json()) | |
179 | async getDefaultSubreddits(options = this.parameters) { | 179 | .then((json) => json.data) |
180 | return await fetch(this.host + "/subreddits/default.json?" + new URLSearchParams(options)) | 180 | .catch((err) => null); |
181 | .then(res => res.json()) | 181 | } |
182 | .then(json => json.data) | 182 | |
183 | .then(data => ({ | 183 | async getSubredditWikiPageRevisions(subreddit, page) { |
184 | after: data.after, | 184 | return await fetch( |
185 | subreddits: data.children | 185 | this.host + "/r/" + subreddit + "/wiki/revisions" + page + ".json", |
186 | })) | 186 | ) |
187 | .catch(err => null); | 187 | .then((res) => res.json()) |
188 | } | 188 | .then((json) => json.data.children) |
189 | 189 | .catch((err) => null); | |
190 | async getPopularUsers(options = this.parameters) { | 190 | } |
191 | return await fetch(this.host + "/users/popular.json?" + new URLSearchParams(options)) | 191 | |
192 | .then(res => res.json()) | 192 | async getPopularSubreddits(options = this.parameters) { |
193 | .then(json => json.data) | 193 | return await fetch( |
194 | .then(data => ({ | 194 | this.host + "/subreddits/popular.json?" + new URLSearchParams(options), |
195 | after: data.after, | 195 | ) |
196 | users: data.children | 196 | .then((res) => res.json()) |
197 | })) | 197 | .then((json) => json.data) |
198 | .catch(err => null); | 198 | .then((data) => ({ |
199 | } | 199 | after: data.after, |
200 | 200 | subreddits: data.children, | |
201 | async getNewUsers(options = this.parameters) { | 201 | })) |
202 | return await fetch(this.host + "/users/new.json?" + new URLSearchParams(options)) | 202 | .catch((err) => null); |
203 | .then(res => res.json()) | 203 | } |
204 | .then(json => json.data) | 204 | |
205 | .then(data => ({ | 205 | async getNewSubreddits(options = this.parameters) { |
206 | after: data.after, | 206 | return await fetch( |
207 | users: data.children | 207 | this.host + "/subreddits/new.json?" + new URLSearchParams(options), |
208 | })) | 208 | ) |
209 | .catch(err => null); | 209 | .then((res) => res.json()) |
210 | } | 210 | .then((json) => json.data) |
211 | 211 | .then((data) => ({ | |
212 | async searchSubmissions(query, options = {}) { | 212 | after: data.after, |
213 | options.q = query; | 213 | subreddits: data.children, |
214 | options.type = "link"; | 214 | })) |
215 | 215 | .catch((err) => null); | |
216 | let params = { | 216 | } |
217 | limit: 25, | 217 | |
218 | include_over_18: true | 218 | async getPremiumSubreddits(options = this.parameters) { |
219 | } | 219 | return await fetch( |
220 | 220 | this.host + "/subreddits/premium.json?" + new URLSearchParams(options), | |
221 | return await fetch(this.host + "/search.json?" + new URLSearchParams(Object.assign(params, options))) | 221 | ) |
222 | .then(res => res.json()) | 222 | .then((res) => res.json()) |
223 | .then(json => json.data) | 223 | .then((json) => json.data) |
224 | .then(data => ({ | 224 | .then((data) => ({ |
225 | after: data.after, | 225 | after: data.after, |
226 | items: data.children | 226 | subreddits: data.children, |
227 | })) | 227 | })) |
228 | .catch(err => null); | 228 | .catch((err) => null); |
229 | } | 229 | } |
230 | 230 | ||
231 | async searchSubreddits(query, options = {}) { | 231 | async getDefaultSubreddits(options = this.parameters) { |
232 | options.q = query; | 232 | return await fetch( |
233 | 233 | this.host + "/subreddits/default.json?" + new URLSearchParams(options), | |
234 | let params = { | 234 | ) |
235 | limit: 25, | 235 | .then((res) => res.json()) |
236 | include_over_18: true | 236 | .then((json) => json.data) |
237 | } | 237 | .then((data) => ({ |
238 | 238 | after: data.after, | |
239 | return await fetch(this.host + "/subreddits/search.json?" + new URLSearchParams(Object.assign(params, options))) | 239 | subreddits: data.children, |
240 | .then(res => res.json()) | 240 | })) |
241 | .then(json => json.data) | 241 | .catch((err) => null); |
242 | .then(data => ({ | 242 | } |
243 | after: data.after, | 243 | |
244 | items: data.children | 244 | async getPopularUsers(options = this.parameters) { |
245 | })) | 245 | return await fetch( |
246 | .catch(err => null); | 246 | this.host + "/users/popular.json?" + new URLSearchParams(options), |
247 | } | 247 | ) |
248 | 248 | .then((res) => res.json()) | |
249 | async searchUsers(query, options = {}) { | 249 | .then((json) => json.data) |
250 | options.q = query; | 250 | .then((data) => ({ |
251 | 251 | after: data.after, | |
252 | let params = { | 252 | users: data.children, |
253 | limit: 25, | 253 | })) |
254 | include_over_18: true | 254 | .catch((err) => null); |
255 | } | 255 | } |
256 | 256 | ||
257 | return await fetch(this.host + "/users/search.json?" + new URLSearchParams(Object.assign(params, options))) | 257 | async getNewUsers(options = this.parameters) { |
258 | .then(res => res.json()) | 258 | return await fetch( |
259 | .then(json => json.data) | 259 | this.host + "/users/new.json?" + new URLSearchParams(options), |
260 | .then(data => ({ | 260 | ) |
261 | after: data.after, | 261 | .then((res) => res.json()) |
262 | items: data.children | 262 | .then((json) => json.data) |
263 | })) | 263 | .then((data) => ({ |
264 | .catch(err => null); | 264 | after: data.after, |
265 | } | 265 | users: data.children, |
266 | 266 | })) | |
267 | async searchAll(query, subreddit = null, options = {}) { | 267 | .catch((err) => null); |
268 | options.q = query; | 268 | } |
269 | subreddit = subreddit ? "/r/" + subreddit : ""; | 269 | |
270 | 270 | async searchSubmissions(query, options = {}) { | |
271 | let params = { | 271 | options.q = query; |
272 | limit: 25, | 272 | options.type = "link"; |
273 | include_over_18: true, | 273 | |
274 | type: "sr,link,user", | 274 | const params = { |
275 | } | 275 | limit: 25, |
276 | 276 | include_over_18: true, | |
277 | return await fetch(this.host + subreddit + "/search.json?" + new URLSearchParams(Object.assign(params, options))) | 277 | }; |
278 | .then(res => res.json()) | 278 | |
279 | .then(json => Array.isArray(json) ? ({ | 279 | return await fetch( |
280 | after: json[1].data.after, | 280 | this.host + |
281 | items: json[0].data.children.concat(json[1].data.children) | 281 | "/search.json?" + |
282 | }) : ({ | 282 | new URLSearchParams(Object.assign(params, options)), |
283 | after: json.data.after, | 283 | ) |
284 | items: json.data.children | 284 | .then((res) => res.json()) |
285 | })) | 285 | .then((json) => json.data) |
286 | .catch(err => null); | 286 | .then((data) => ({ |
287 | } | 287 | after: data.after, |
288 | 288 | items: data.children, | |
289 | async getSubmission(id) { | 289 | })) |
290 | return await fetch(this.host + "/by_id/" + id + ".json") | 290 | .catch((err) => null); |
291 | .then(res => res.json()) | 291 | } |
292 | .then(json => json.data.children[0].data) | 292 | |
293 | .catch(err => null); | 293 | async searchSubreddits(query, options = {}) { |
294 | } | 294 | options.q = query; |
295 | 295 | ||
296 | async getSubmissionComments(id, options = this.parameters) { | 296 | const params = { |
297 | return await fetch(this.host + "/comments/" + id + ".json?" + new URLSearchParams(options)) | 297 | limit: 25, |
298 | .then(res => res.json()) | 298 | include_over_18: true, |
299 | .then(json => ({ | 299 | }; |
300 | submission: json[0].data.children[0], | 300 | |
301 | comments: json[1].data.children | 301 | return await fetch( |
302 | })) | 302 | this.host + |
303 | .catch(err => null); | 303 | "/subreddits/search.json?" + |
304 | } | 304 | new URLSearchParams(Object.assign(params, options)), |
305 | 305 | ) | |
306 | async getSubredditComments(subreddit, options = this.parameters) { | 306 | .then((res) => res.json()) |
307 | return await fetch(this.host + "/r/" + subreddit + "/comments.json?" + new URLSearchParams(options)) | 307 | .then((json) => json.data) |
308 | .then(res => res.json()) | 308 | .then((data) => ({ |
309 | .then(json => json.data.children) | 309 | after: data.after, |
310 | .catch(err => null); | 310 | items: data.children, |
311 | } | 311 | })) |
312 | 312 | .catch((err) => null); | |
313 | async getUser(username) { | 313 | } |
314 | return await fetch(this.host + "/user/" + username + "/about.json") | 314 | |
315 | .then(res => res.json()) | 315 | async searchUsers(query, options = {}) { |
316 | .then(json => json.data) | 316 | options.q = query; |
317 | .catch(err => null); | 317 | |
318 | } | 318 | const params = { |
319 | 319 | limit: 25, | |
320 | async getUserOverview(username, options = this.parameters) { | 320 | include_over_18: true, |
321 | return await fetch(this.host + "/user/" + username + "/overview.json?" + new URLSearchParams(options)) | 321 | }; |
322 | .then(res => res.json()) | 322 | |
323 | .then(json => json.data) | 323 | return await fetch( |
324 | .then(data => ({ | 324 | this.host + |
325 | after: data.after, | 325 | "/users/search.json?" + |
326 | items: data.children | 326 | new URLSearchParams(Object.assign(params, options)), |
327 | })) | 327 | ) |
328 | .catch(err => null); | 328 | .then((res) => res.json()) |
329 | } | 329 | .then((json) => json.data) |
330 | 330 | .then((data) => ({ | |
331 | async getUserComments(username, options = this.parameters) { | 331 | after: data.after, |
332 | return await fetch(this.host + "/user/" + username + "/comments.json?" + new URLSearchParams(options)) | 332 | items: data.children, |
333 | .then(res => res.json()) | 333 | })) |
334 | .then(json => json.data) | 334 | .catch((err) => null); |
335 | .then(data => ({ | 335 | } |
336 | after: data.after, | 336 | |
337 | items: data.children | 337 | async searchAll(query, subreddit = null, options = {}) { |
338 | })) | 338 | options.q = query; |
339 | .catch(err => null); | 339 | subreddit = subreddit ? "/r/" + subreddit : ""; |
340 | } | 340 | |
341 | 341 | const params = { | |
342 | async getUserSubmissions(username, options = this.parameters) { | 342 | limit: 25, |
343 | return await fetch(this.host + "/user/" + username + "/submitted.json?" + new URLSearchParams(options)) | 343 | include_over_18: true, |
344 | .then(res => res.json()) | 344 | type: "sr,link,user", |
345 | .then(json => json.data) | 345 | }; |
346 | .then(data => ({ | 346 | |
347 | after: data.after, | 347 | return await fetch( |
348 | items: data.children | 348 | this.host + |
349 | })) | 349 | subreddit + |
350 | .catch(err => null); | 350 | "/search.json?" + |
351 | } | 351 | new URLSearchParams(Object.assign(params, options)), |
352 | 352 | ) | |
353 | async getLiveThread(id) { | 353 | .then((res) => res.json()) |
354 | return await fetch(this.host + "/live/" + id + "/about.json") | 354 | .then((json) => |
355 | .then(res => res.json()) | 355 | Array.isArray(json) |
356 | .then(json => json.data) | 356 | ? { |
357 | .catch(err => null); | 357 | after: json[1].data.after, |
358 | } | 358 | items: json[0].data.children.concat(json[1].data.children), |
359 | 359 | } | |
360 | async getLiveThreadUpdates(id, options = this.parameters) { | 360 | : { |
361 | return await fetch(this.host + "/live/" + id + ".json?" + new URLSearchParams(options)) | 361 | after: json.data.after, |
362 | .then(res => res.json()) | 362 | items: json.data.children, |
363 | .then(json => json.data.children) | 363 | }, |
364 | .catch(err => null); | 364 | ) |
365 | } | 365 | .catch((err) => null); |
366 | 366 | } | |
367 | 367 | ||
368 | async getLiveThreadContributors(id, options = this.parameters) { | 368 | async getSubmission(id) { |
369 | return await fetch(this.host + "/live/" + id + "/contributors.json?" + new URLSearchParams(options)) | 369 | return await fetch(this.host + "/by_id/" + id + ".json") |
370 | .then(res => res.json()) | 370 | .then((res) => res.json()) |
371 | .then(json => json.data.children) | 371 | .then((json) => json.data.children[0].data) |
372 | .catch(err => null); | 372 | .catch((err) => null); |
373 | } | 373 | } |
374 | 374 | ||
375 | async getLiveThreadDiscussions(id, options = this.parameters) { | 375 | async getSubmissionComments(id, options = this.parameters) { |
376 | return await fetch(this.host + "/live/" + id + "/discussions.json?" + new URLSearchParams(options)) | 376 | return await fetch( |
377 | .then(res => res.json()) | 377 | this.host + "/comments/" + id + ".json?" + new URLSearchParams(options), |
378 | .then(json => json.data.children) | 378 | ) |
379 | .catch(err => null); | 379 | .then((res) => res.json()) |
380 | } | 380 | .then((json) => ({ |
381 | 381 | submission: json[0].data.children[0], | |
382 | async getLiveThreadsNow(options = this.parameters) { | 382 | comments: json[1].data.children, |
383 | return await fetch(this.host + "/live/happening_now.json?" + new URLSearchParams(options)) | 383 | })) |
384 | .then(res => res.json()) | 384 | .catch((err) => null); |
385 | .then(json => json.data.children) | 385 | } |
386 | .catch(err => null); | 386 | |
387 | } | 387 | async getSubredditComments(subreddit, options = this.parameters) { |
388 | return await fetch( | ||
389 | this.host + | ||
390 | "/r/" + | ||
391 | subreddit + | ||
392 | "/comments.json?" + | ||
393 | new URLSearchParams(options), | ||
394 | ) | ||
395 | .then((res) => res.json()) | ||
396 | .then((json) => json.data.children) | ||
397 | .catch((err) => null); | ||
398 | } | ||
399 | |||
400 | async getUser(username) { | ||
401 | return await fetch(this.host + "/user/" + username + "/about.json") | ||
402 | .then((res) => res.json()) | ||
403 | .then((json) => json.data) | ||
404 | .catch((err) => null); | ||
405 | } | ||
406 | |||
407 | async getUserOverview(username, options = this.parameters) { | ||
408 | return await fetch( | ||
409 | this.host + | ||
410 | "/user/" + | ||
411 | username + | ||
412 | "/overview.json?" + | ||
413 | new URLSearchParams(options), | ||
414 | ) | ||
415 | .then((res) => res.json()) | ||
416 | .then((json) => json.data) | ||
417 | .then((data) => ({ | ||
418 | after: data.after, | ||
419 | items: data.children, | ||
420 | })) | ||
421 | .catch((err) => null); | ||
422 | } | ||
423 | |||
424 | async getUserComments(username, options = this.parameters) { | ||
425 | return await fetch( | ||
426 | this.host + | ||
427 | "/user/" + | ||
428 | username + | ||
429 | "/comments.json?" + | ||
430 | new URLSearchParams(options), | ||
431 | ) | ||
432 | .then((res) => res.json()) | ||
433 | .then((json) => json.data) | ||
434 | .then((data) => ({ | ||
435 | after: data.after, | ||
436 | items: data.children, | ||
437 | })) | ||
438 | .catch((err) => null); | ||
439 | } | ||
440 | |||
441 | async getUserSubmissions(username, options = this.parameters) { | ||
442 | return await fetch( | ||
443 | this.host + | ||
444 | "/user/" + | ||
445 | username + | ||
446 | "/submitted.json?" + | ||
447 | new URLSearchParams(options), | ||
448 | ) | ||
449 | .then((res) => res.json()) | ||
450 | .then((json) => json.data) | ||
451 | .then((data) => ({ | ||
452 | after: data.after, | ||
453 | items: data.children, | ||
454 | })) | ||
455 | .catch((err) => null); | ||
456 | } | ||
457 | |||
458 | async getLiveThread(id) { | ||
459 | return await fetch(this.host + "/live/" + id + "/about.json") | ||
460 | .then((res) => res.json()) | ||
461 | .then((json) => json.data) | ||
462 | .catch((err) => null); | ||
463 | } | ||
464 | |||
465 | async getLiveThreadUpdates(id, options = this.parameters) { | ||
466 | return await fetch( | ||
467 | this.host + "/live/" + id + ".json?" + new URLSearchParams(options), | ||
468 | ) | ||
469 | .then((res) => res.json()) | ||
470 | .then((json) => json.data.children) | ||
471 | .catch((err) => null); | ||
472 | } | ||
473 | |||
474 | async getLiveThreadContributors(id, options = this.parameters) { | ||
475 | return await fetch( | ||
476 | this.host + | ||
477 | "/live/" + | ||
478 | id + | ||
479 | "/contributors.json?" + | ||
480 | new URLSearchParams(options), | ||
481 | ) | ||
482 | .then((res) => res.json()) | ||
483 | .then((json) => json.data.children) | ||
484 | .catch((err) => null); | ||
485 | } | ||
486 | |||
487 | async getLiveThreadDiscussions(id, options = this.parameters) { | ||
488 | return await fetch( | ||
489 | this.host + | ||
490 | "/live/" + | ||
491 | id + | ||
492 | "/discussions.json?" + | ||
493 | new URLSearchParams(options), | ||
494 | ) | ||
495 | .then((res) => res.json()) | ||
496 | .then((json) => json.data.children) | ||
497 | .catch((err) => null); | ||
498 | } | ||
499 | |||
500 | async getLiveThreadsNow(options = this.parameters) { | ||
501 | return await fetch( | ||
502 | this.host + "/live/happening_now.json?" + new URLSearchParams(options), | ||
503 | ) | ||
504 | .then((res) => res.json()) | ||
505 | .then((json) => json.data.children) | ||
506 | .catch((err) => null); | ||
507 | } | ||
388 | } | 508 | } |
389 | 509 | ||
390 | export { Geddit } | 510 | export { Geddit }; |
diff --git a/src/index.js b/src/index.js index 099ee4d..164ad49 100644 --- a/src/index.js +++ b/src/index.js | |||
@@ -1,18 +1,17 @@ | |||
1 | const express = require('express'); | 1 | const express = require("express"); |
2 | const path = require('path'); | 2 | const path = require("path"); |
3 | const routes = require('./routes/index'); | 3 | const routes = require("./routes/index"); |
4 | const geddit = require('./geddit.js'); | 4 | const geddit = require("./geddit.js"); |
5 | 5 | ||
6 | const app = express(); | 6 | const app = express(); |
7 | 7 | ||
8 | app.set('views', path.join(__dirname, 'views')); | 8 | app.set("views", path.join(__dirname, "views")); |
9 | app.set('view engine', 'pug'); | 9 | app.set("view engine", "pug"); |
10 | 10 | ||
11 | app.use(express.static(path.join(__dirname, 'public'))); | 11 | app.use(express.static(path.join(__dirname, "public"))); |
12 | app.use('/', routes); | 12 | app.use("/", routes); |
13 | 13 | ||
14 | const port = process.env.READIT_PORT; | 14 | const port = process.env.READIT_PORT; |
15 | const server = app.listen(port?port:3000, () => { | 15 | const server = app.listen(port ? port : 3000, () => { |
16 | console.log(`started on ${server.address().port}`); | 16 | console.log(`started on ${server.address().port}`); |
17 | }); | 17 | }); |
18 | |||
diff --git a/src/mixins/comment.pug b/src/mixins/comment.pug index 4249030..d02c221 100644 --- a/src/mixins/comment.pug +++ b/src/mixins/comment.pug | |||
@@ -19,10 +19,10 @@ mixin infoContainer(data) | |||
19 | 19 | ||
20 | mixin comment(com, isfirst) | 20 | mixin comment(com, isfirst) |
21 | - var data = com.data | 21 | - var data = com.data |
22 | - var kind = com.kind | 22 | - console.log(com) |
23 | - var hasReplyData = hasReplies(data) | 23 | - var hasReplyData = hasReplies(data) |
24 | 24 | ||
25 | if kind == "more" | 25 | if com.kind == "more" |
26 | div(class=`more ${isfirst ? 'first' : ''}`) | 26 | div(class=`more ${isfirst ? 'first' : ''}`) |
27 | | #{data.count} more #{fmttxt(data.count, 'comment')} | 27 | | #{data.count} more #{fmttxt(data.count, 'comment')} |
28 | else | 28 | else |
diff --git a/src/routes/index.js b/src/routes/index.js index 54a5b18..01c17fa 100644 --- a/src/routes/index.js +++ b/src/routes/index.js | |||
@@ -1,79 +1,81 @@ | |||
1 | const express = require('express'); | 1 | const express = require("express"); |
2 | const he = require('he'); | 2 | const he = require("he"); |
3 | const router = express.Router(); | 3 | const router = express.Router(); |
4 | const geddit = require('../geddit.js'); | 4 | const geddit = require("../geddit.js"); |
5 | const G = new geddit.Geddit(); | 5 | const G = new geddit.Geddit(); |
6 | 6 | ||
7 | |||
8 | // GET / | 7 | // GET / |
9 | router.get('/', async (req, res) => { | 8 | router.get("/", async (req, res) => { |
10 | res.redirect("/r/all") | 9 | res.redirect("/r/all"); |
11 | }); | 10 | }); |
12 | 11 | ||
13 | // GET /r/:id | 12 | // GET /r/:id |
14 | router.get('/r/:subreddit', async (req, res) => { | 13 | router.get("/r/:subreddit", async (req, res) => { |
15 | var subreddit = req.params.subreddit; | 14 | var subreddit = req.params.subreddit; |
16 | var query = req.query? req.query : {}; | 15 | var query = req.query ? req.query : {}; |
17 | if (!query.sort) { | 16 | if (!query.sort) { |
18 | query.sort = 'hot'; | 17 | query.sort = "hot"; |
19 | } | 18 | } |
20 | 19 | ||
21 | var postsReq = G.getSubmissions(query.sort, `${subreddit}`, query); | 20 | var postsReq = G.getSubmissions(query.sort, `${subreddit}`, query); |
22 | var aboutReq = G.getSubreddit(`${subreddit}`); | 21 | var aboutReq = G.getSubreddit(`${subreddit}`); |
23 | 22 | ||
24 | var [posts, about] = await Promise.all([postsReq, aboutReq]); | 23 | var [posts, about] = await Promise.all([postsReq, aboutReq]); |
25 | 24 | ||
26 | res.render('index', { subreddit, posts, about, query }); | 25 | res.render("index", { subreddit, posts, about, query }); |
27 | }); | 26 | }); |
28 | 27 | ||
29 | // GET /comments/:id | 28 | // GET /comments/:id |
30 | router.get('/comments/:id', async (req, res) => { | 29 | router.get("/comments/:id", async (req, res) => { |
31 | var id = req.params.id; | 30 | var id = req.params.id; |
32 | 31 | ||
33 | response = await G.getSubmissionComments(id); | 32 | var params = { |
33 | limit: 50, | ||
34 | }; | ||
35 | response = await G.getSubmissionComments(id, params); | ||
34 | 36 | ||
35 | res.render('comments', unescape_submission(response)); | 37 | res.render("comments", unescape_submission(response)); |
36 | }); | 38 | }); |
37 | 39 | ||
38 | // GET /subs | 40 | // GET /subs |
39 | router.get('/subs', async (req, res) => { | 41 | router.get("/subs", async (req, res) => { |
40 | res.render('subs'); | 42 | res.render("subs"); |
41 | }); | 43 | }); |
42 | 44 | ||
43 | // GET /media | 45 | // GET /media |
44 | router.get('/media/*', async (req, res) => { | 46 | router.get("/media/*", async (req, res) => { |
45 | var url = req.params[0]; | 47 | var url = req.params[0]; |
46 | console.log(`making request to ${url}`); | 48 | console.log(`making request to ${url}`); |
47 | return await fetch(url, { | 49 | return await fetch(url, { |
48 | headers: { | 50 | headers: { |
49 | Accept: "*/*", | 51 | Accept: "*/*", |
50 | } | 52 | }, |
51 | }); | 53 | }); |
52 | }); | 54 | }); |
53 | 55 | ||
54 | module.exports = router; | 56 | module.exports = router; |
55 | 57 | ||
56 | function unescape_submission(response) { | 58 | function unescape_submission(response) { |
57 | var post = response.submission.data; | 59 | var post = response.submission.data; |
58 | var comments = response.comments; | 60 | var comments = response.comments; |
59 | 61 | ||
60 | if (post.selftext_html) { | 62 | if (post.selftext_html) { |
61 | post.selftext_html = he.decode(post.selftext_html); | 63 | post.selftext_html = he.decode(post.selftext_html); |
62 | } | 64 | } |
63 | comments.forEach(unescape_comment); | 65 | comments.forEach(unescape_comment); |
64 | 66 | ||
65 | return { post, comments }; | 67 | return { post, comments }; |
66 | } | 68 | } |
67 | 69 | ||
68 | function unescape_comment(comment) { | 70 | function unescape_comment(comment) { |
69 | if (comment.data.body_html) { | 71 | if (comment.data.body_html) { |
70 | comment.data.body_html = he.decode(comment.data.body_html); | 72 | comment.data.body_html = he.decode(comment.data.body_html); |
71 | } | 73 | } |
72 | if (comment.data.replies) { | 74 | if (comment.data.replies) { |
73 | if(comment.data.replies.data) { | 75 | if (comment.data.replies.data) { |
74 | if(comment.data.replies.data.children) { | 76 | if (comment.data.replies.data.children) { |
75 | comment.data.replies.data.children.forEach(unescape_comment); | 77 | comment.data.replies.data.children.forEach(unescape_comment); |
76 | } | 78 | } |
77 | } | 79 | } |
78 | } | 80 | } |
79 | } | 81 | } |