-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
82 lines (70 loc) · 1.97 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
var express = require("express");
var app = express();
var port = process.env.PORT || 3333;
var io = require('socket.io').listen(app.listen(port));
var siteUrl = 'http://9abd2b82.ngrok.io/';
var Instagram = require('instagram-node-lib');
var http = require('http');
var request = ('request');
var intervalID;
var tag = 'selfie';
var pub = __dirname + '/public',
view = __dirname + '/views';
app.use('/bower_components', express.static(__dirname + '/bower_components'));
// Instagram secret
var clientID = '',
clientSecret = '';
// Instagram config
Instagram.set('client_id', clientID);
Instagram.set('client_secret', clientSecret);
Instagram.set('callback_url', siteUrl + 'callback');
Instagram.set('redirect_uri', siteUrl);
Instagram.set('maxSockets', 10);
Instagram.subscriptions.subscribe({
object: 'tag',
object_id: tag,
aspect: 'media',
callback_url: siteUrl + 'callback',
type: 'subscription',
id: '#'
});
// Socket config
io.configure(function () {
io.set("transports", [
'websocket'
, 'xhr-polling'
, 'flashsocket'
, 'htmlfile'
, 'jsonp-polling'
]);
io.set("polling duration", 10);
});
// Server config
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(pub));
app.use(express.static(view));
app.use(express.errorHandler());
});
app.get("/views", function(req, res){
res.render("index");
});
app.get('/callback', function(req, res){
var handshake = Instagram.subscriptions.handshake(req, res);
});
// Get data for every new image
app.post('/callback', function(req, res) {
var data = req.body;
data.forEach(function(tag) {
var url = 'https://api.instagram.com/v1/tags/' + tag.object_id + '/media/recent?client_id='+clientID;
sendMessage(url);
});
res.end();
});
// Get url so we can make a call
function sendMessage(url) {
io.sockets.emit('show', { show: url });
}
console.log("Being awesome at " + port);