-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreddit.js
31 lines (30 loc) · 1.36 KB
/
reddit.js
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
function getRedditImages(AMOUNT, SUBREDDIT, CALLBACK){
var images = [];
var responce;
var first = "";
fetch(`https://www.reddit.com/r/${SUBREDDIT}/.json?limit=1`)
.then(unformattedResponse => unformattedResponse.json())
.then(formattedResponce => {
first = formattedResponce.data.children[0].data.after;
})
.catch(err => console.error(err));
fetch(`https://www.reddit.com/r/${SUBREDDIT}/.json?limit=${AMOUNT}&after=${first}`)
.then(unformattedResponse => unformattedResponse.json())
.then(formattedResponce => {
responce = formattedResponce;
for(var i=0; i<formattedResponce.data.children.length; i++){
var image = "no image";
if((responce.data.children[i].data).hasOwnProperty("preview")){
if(responce.data.children[i].data.preview.images[0].variants.hasOwnProperty("gif")){
image = responce.data.children[i].data.preview.images[0].variants.gif.source.url;
}else{
image = responce.data.children[i].data.preview.images[0].source.url;
}
image = image.replaceAll("amp;", "");
}
images.push(image);
}
CALLBACK(images);
})
.catch(err => console.error(err));
}