-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimgur.js
More file actions
59 lines (53 loc) · 1.86 KB
/
imgur.js
File metadata and controls
59 lines (53 loc) · 1.86 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
var https = require("https");
var async = require("async");
var Pic = require("./models/picture");
module.exports = function (album,topic,top,time,page) {
// https://api.imgur.com/3/gallery/r/{subreddit}/{time|top}/{if the sort is "top", day | week | month | year | all}/{page - integer}
// maybe I could keep trying a new page
// :top/:time/:page/
// var path = "/3/gallery/ r/funny /top/ month/ 1
// var path = "/3/topics/ funny /top/ month/ 1
var path = "/3/"+album+"/"+topic+"/"+top+"/"+time+"/"+page.toString()
console.log("img",path)
var options = {
protocol: "https:",
host: 'api.imgur.com',
path: path,
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': process.env.VALUE
}
};
var req = https.get(options, function(res) {
var body = "";
res.on('data', function(data) {
body += data;
}).on('end', function() {
var wholebody = JSON.parse(body);
const dateAdded = Date.now().toString()
async.forEach(wholebody.data, function(info, callback) {
if (info.type !== 'image/gif' && info.type !== undefined && ((info.height/info.width) < 2.5) && info.is_album == false && ((info.width/info.height) < 1.8)) {
var link = "https"+info.link.slice(4,info.link.length)
Pic.createPictureNode(info.id, info.title, link, dateAdded, info.type, function(error, match) {
if(error) {
next()
// return callback()
}
return callback()
})
}
}, function(error) {
// future. IF error, get from a different route.
if (error) {
console.log("error with adding pictures..")
} else {
console.log("SUCCESS")
}
})
})
});
req.on('error', function(e) {
console.log('ERROR: ' + e.message);
});
}