-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
151 lines (122 loc) · 4.82 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var express = require('express'),
app = express(),
rest = require('./app/js/rest.js');
// configure Express
app.configure(function() {
app.use(express.favicon());
app.use(express.static(__dirname + '/app'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
// routes
/* __ __ __
____ ____ / /_ ____ ___ ___ ____/ /__ ____/ /
/ __ \/ __ \/ __/ / __ \/ _ \/ _ \/ __ / _ \/ __ /
/ / / / /_/ / /_ / / / / __/ __/ /_/ / __/ /_/ /
/_/ /_/\____/\__/ /_/ /_/\___/\___/\__,_/\___/\__,_/
*/
/*app.all('*', function(req, res, next) {
// console.log(req);
res.header('Access-Control-Allow-Origin', '*.jopho.com');
res.header('Access-Control-Allow-Headers', 'Origin, Content-Type, X-Requested-With');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
return next();
});*/
/* __ _
____ ____ ____ ___ ____ ________ / /_(_)___ ___ ___
/ __ \/ __ \ / __ `__ \/ __ \/ ___/ _ \ / __/ / __ `__ \/ _ \
/ / / / /_/ / / / / / / / /_/ / / / __/ / /_/ / / / / / / __/
/_/ /_/\____/ /_/ /_/ /_/\____/_/ \___/ \__/_/_/ /_/ /_/\___/
*/
app.get('/twitter', function(req, res) {
var username = req.query.user;
var enddate = req.query.ey + '-' + req.query.em + '-' + req.query.ed;
//never would have worked… twitter requires per-search oauth tokens
//which also require https. ran out of time.
var options = {
host: 'api.twitter.com',
path: '/1.1/search/tweets.json?q=%23' + username + '&result_type=popular&count=100&until=' + enddate,
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
rest.getJSON(options, function(statusCode, results) {
res.statusCode = statusCode;
res.send(results);
});
});
app.get('/flickr', function(req, res) {
var username = req.query.user;
var startdate = new Date(req.query.sy, req.query.sm - 1, req.query.sd, 0, 0, 0);
var enddate = new Date(req.query.ey, req.query.em - 1, req.query.ed, 23, 59, 59);
var useroptions = {
host: 'api.flickr.com',
path: '/services/rest/?method=flickr.people.findByUsername' + '&api_key=6ccf3ac4e38fcdc496798883300e8b6b' + '&username=' + username + '&format=json&nojsoncallback=1',
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
rest.getJSON(useroptions, function(statusCode, result) {
var usercode = result.user.nsid;
var options = {
host: 'api.flickr.com',
path: '/services/rest/?method=flickr.photos.search' + '&api_key=6ccf3ac4e38fcdc496798883300e8b6b' + '&user_id=' + usercode + '&min_taken_date=' + Math.round(startdate.getTime() / 1000) + '&max_taken_date=' + Math.round(enddate.getTime() / 1000) + '&extras=date_taken&format=json&nojsoncallback=1',
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
rest.getJSON(options, function(statusCode, results) {
res.statusCode = statusCode;
res.send(results);
});
});
});
/*
/ /__ __(_) /_____ (_)____ / /_ _________ / /_____ ____
/ __/ | /| / / / __/ __ \/ / ___/ / __ \/ ___/ __ \/ //_/ _ \/ __ \
/ /_ | |/ |/ / / /_/ /_/ / / /__ / /_/ / / / /_/ / ,< / __/ / / /
\__/ |__/|__/_/\__/ .___/_/\___/ /_.___/_/ \____/_/|_|\___/_/ /_/
/_/ */
//I fail at recursive.
app.get('/twitpic', function(req, res) {
var user = req.query.user;
var startdate = new Date(req.query.sy, req.query.sm - 1, req.query.sd, 0, 0, 0);
var enddate = new Date(req.query.ey, req.query.em - 1, req.query.ed, 23, 59, 59);
var results = [];
function getoptions(n) {
var options = {
host: 'api.twitpic.com',
path: '/2/users/show.json?username=' + user + '?page=' + (n == 0 ? 0 : n),
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
return options;
};
rest.getJSON(getoptions(0), function(statusCode, result) {
// number of pages to request in batches of 20 rounded up
var pages = Math.ceil(result.photo_only_count / 20);
console.log('number of pages: ' + pages);
function repeater(p) {
if (p <= pages) {
console.log('start of if loop: ' + p);
rest.getJSON(getoptions(p), function(statusCode, nextresult) {
console.log('rest request ' + p + ' push to array');
results.push(nextresult.images);
console.log('current array ' + results);
repeater(p + 1);
});
}
};
repeater(1);
});
res.statusCode = statusCode;
res.send(results);
});
app.listen(9999);
console.log(new Date() + 'Node+Express server listening on port 9999');