-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetter.js
More file actions
66 lines (46 loc) · 2.01 KB
/
Copy pathgetter.js
File metadata and controls
66 lines (46 loc) · 2.01 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
var fetch = require('node-fetch');
/*
I initially used a npm package called redditimage but it
had deprecated unhandled promise exceptions and used
(snekfetch) that was also deprecated and it wasn't
updated for a long time and its github repo was deleted
and I couldn't contact its owner so I eddited his module
and used (node-fetch) and added some other useful functionality
*/
//returns -1 if it doesn't find a non-crossposted jpg/png image
//UPDATE: URL formats have been updated but I still have to return the old format to check it against the hash table
const getURL = function(subreddit){
return new Promise((resolve, reject) => {
fetch(`https://www.reddit.com/r/${subreddit}/random.json?limit=1`)
.then(res => res.json())
.then(json => {
let image;
let oldImage;
let crossParent;
let permalink;
try {
image = json[0].data.children[0].data.url;
oldImage = json[0].data.children[0].data.preview.images[0].source.url;
crossParent = json[0].data.children[0].data.crosspost_parent;
permalink = json[0].data.children[0].data.permalink;
} catch (error) {
image = json.data.children[0].data.url;
oldImage = json[0].data.children[0].data.preview.images[0].source.url;
crossParent = json.data.children[0].data.crosspost_parent;
permalink = json[0].data.children[0].data.permalink;
}
if(!image)
return resolve('-1');
if(crossParent)
return resolve('-1');
let extension = image.substring(image.lastIndexOf('.')+1,image.lastIndexOf('.')+4);
if(extension!='jpg' && extension!='png')
return resolve('-1');
return resolve([image,oldImage,permalink]);
})
.catch((err) => {
return resolve('-1');
});
});
};
module.exports=getURL;