-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
98 lines (87 loc) · 3.64 KB
/
routes.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
var mongoose = require('mongoose'),
nconf = require( 'nconf' ),
https = require( 'https' ),
http = require( 'http' ),
url = require( 'url' ),
request = require( 'request' ),
fs = require( 'fs' ),
mkpath = require( 'mkpath' ),
mime = require( 'mime' ),
pairtree = require( 'pairtree' ),
path = require( 'path' ),
Word = mongoose.model('words');
nconf.use('file', { file: __dirname + '/config.json' });
var key = nconf.get('azure:key');
exports.index = function(req, res) {
Word.count({'thumbs.file': { $exists: true }}, function(err, count) {
Word.findOne({ 'thumbs.file': { $exists: true }} ).limit(-1).skip(Math.floor(Math.random()*count)).exec(function(err, word) {
if (err) throw err;
var date = new Date();
console.log(date + "\t" + "index\t" + word.word);
res.render('index.jade', { word: word });
});
});
};
exports.bigpicture = function(req, res) {
Word.count({'images.file': { $exists: true }}, function(err, count) {
Word.findOne({ 'images.file': { $exists: true }} ).limit(-1).skip(Math.floor(Math.random()*count)).exec(function(err, word) {
if (err) throw err;
var date = new Date();
console.log(date + "\t" + "bigpicture\t" + word.word);
res.send(word);
});
});
};
exports.fetchone = function(req, res) {
Word.count({}, function(err, count) {
Word.findOne({thumbs: {$not: {$size: 0}}}).limit(-1).skip(Math.floor(Math.random()*count)).exec(function(error, word) {
if (error) throw error;
console.log('word id: ObjectId("' + word._id +'") word: ' + word.word);
var azureUrl = "https://user:"+key+"@api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query=%27"+word.word+"%27&$top=20&$format=JSON";
var httpsreq = https.request(azureUrl, function(response) {
var data = '';
response.on('data', function(chunk) {
data += chunk;
});
response.on('end', function() {
var pics = JSON.parse(data);
var wd_path = __dirname + '/public/words' + pairtree.path(word.word) + word.word;
var th_path = wd_path + '/thumbs/';
var img_path = wd_path + '/images/';
// azure is returning 'image/jpg' not 'image/jpeg', register it
mime.define({ 'image/jpg': ['jpg']});
mkpath(wd_path, function(err) {
fs.writeFile(wd_path + "/" + word.word + ".json", JSON.stringify(pics, null, 4), function(err) {
if (err) console.log(err);
});
mkpath(th_path, function(e) {
var count = 0;
pics.d.results.forEach(function(result) {
count++;
var parts = url.parse(result.Thumbnail.MediaUrl, true);
var id = parts.query.id;
var file = th_path + id + '.' + mime.extension(result.Thumbnail.ContentType);
request(result.Thumbnail.MediaUrl).pipe(fs.createWriteStream(file));
word.thumbs.push({ file: file });
word.save();
mkpath(img_path, function(e3) {
var img_url = url.parse(result.MediaUrl);
file = img_path + id + '--' + path.basename(img_url.pathname);
request(result.MediaUrl).pipe(fs.createWriteStream(file));
word.images.push({ file: file });
word.save();
});
});
});
});
});
});
httpsreq.end();
httpsreq.on('error', function(e) {
console.error(e);
});
});
});
res.end();
};