-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulateImages.js
58 lines (48 loc) · 1.79 KB
/
populateImages.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
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
var mongoose = require('mongoose');
var WordSearchTemplate = require('./models/WordSearchTemplate');
var superagent = require('superagent');
mongoose.connect('mongodb://localhost/wordsearches');
var fs = require('fs'),
request = require('request');
var gm = require('gm');
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
var resizeAndDownload = function(item, callback){
console.log('here');
superagent
.post('http://db.wordsearchcreatorhq.com/wordsearch/createwordsearch')
.send({ Words: ['test'], Pattern: item.pattern.join('\r\n') })
.set('Accept', 'application/json')
.end(function(error, res){
console.log('downloading picture' + res.body);
download('http://db.wordsearchcreatorhq.com/wsearches/' + res.body + '.png', 'wsearches/image' + item.id + '.png', function(){
console.log('done downloading');
gm('wsearches/image' + item.id + '.png')
.resize(300)
//.autoOrient()
.write('wsearches/thumb' + item.id +'.png', function (err) {
if(err) console.log(err);
if (!err) {console.log(' hooray! ');
callback(item);}
});
}, function(error){
console.log(error);
});
});
}
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
WordSearchTemplate.find(function(err, items){
if(err) throw err;
for(var i = 0; i < items.length; i++) {
var item = items[i];
resizeAndDownload(item, function(i){console.log(i.title + ' finished');});
}
});
});