-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
128 lines (101 loc) · 3.72 KB
/
server.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, request = require('request')
, Twit = require('twit')
, fs = require('fs');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/users', user.list);
app.post('/image', function(req, res) {
fs.readFile(req.files.file.path, function (err, data){
var dirname = "/public/imgs";
var newPath = dirname + "/uploads/" + "image";
fs.writeFile(newPath, data, function (err) {
if(err){
res.json({'response':"Error"});
} else {
res.json({'response':"Saved"});
}
});
});
});
app.get('/image/:file', function (req, res){
file = req.params.file;
var dirname = "/public/imgs";
var img = fs.readFileSync(dirname + "/uploads/" + file);
res.writeHead(200, {'Content-Type': 'image/png' });
res.end(img, 'binary');
});
app.get('/intruder', function(req, res){
// auth keys
var T = new Twit({
consumer_key: 'i16nHoJ8SFsgbVJEPxle24jgS'
, consumer_secret: 'Ke18Hs8BaC4N1zMBhLlcRnxGYXWpGQ6SsbJ0A7WFfHs12ehN5y'
, access_token: '4188246705-BIpbm5o0tCe5uejZeSkcwoeBew86Teoy2Op4bpH'
, access_token_secret: 'NdVwoXm5o50wvCbI5Tkvf6ItqR6VAtwX7s5wIdQiuuCJL'
});
// image encoding
var b64content = fs.readFileSync('public/imgs/image.png', { encoding: 'base64' })
// first we must post the media to Twitter
T.post('media/upload', { media_data: b64content }, function (err, data, response) {
// now we can reference the media and post a tweet (media will attach to the tweet)
var mediaIdStr = data.media_id_string
var params = { status: 'Intruder alert. Chasing now...', media_ids: [mediaIdStr] }
T.post('statuses/update', params, function (err, data, response) {
if (!err && response.statusCode == 200) {
res.render('intruder', { title: 'Intruder' });
}
else {
response.send(err);
}
})
})
});
app.get('/captured', function(req, res){
// auth keys
var T = new Twit({
consumer_key: 'i16nHoJ8SFsgbVJEPxle24jgS'
, consumer_secret: 'Ke18Hs8BaC4N1zMBhLlcRnxGYXWpGQ6SsbJ0A7WFfHs12ehN5y'
, access_token: '4188246705-BIpbm5o0tCe5uejZeSkcwoeBew86Teoy2Op4bpH'
, access_token_secret: 'NdVwoXm5o50wvCbI5Tkvf6ItqR6VAtwX7s5wIdQiuuCJL'
});
// image encoding
var b64content = fs.readFileSync('public/imgs/image.png', { encoding: 'base64' })
// first we must post the media to Twitter
T.post('media/upload', { media_data: b64content }, function (err, data, response) {
// now we can reference the media and post a tweet (media will attach to the tweet)
var mediaIdStr = data.media_id_string
var params = { status: 'Intruder Captured. It\'s all safe now', media_ids: [mediaIdStr] }
T.post('statuses/update', params, function (err, data, response) {
if (!err && response.statusCode == 200) {
res.render('captured', { title: 'Captured' });
}
else {
response.send(err);
}
})
})
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});