-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
159 lines (135 loc) · 4.12 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
152
153
154
155
156
157
158
159
var bodyParser = require('body-parser'),
express = require('express'),
http = require('http'),
exphbs = require('express-handlebars'),
Twitter = require('twitter'),
app = express(),
// urlExpander = require('expand-url'), /// Experimental
sensitive = require('./sensitive.js'),
crypto = require('crypto'),
async = require('async'),
server = require('http').createServer(app);
var client = new Twitter({
consumer_key: sensitive.Consumer_Key,
consumer_secret: sensitive.Consumer_Secret,
access_token_key: sensitive.Access_Token,
access_token_secret: sensitive.Access_Token_Secret
});
var BBC_API_KEY = "YB0MY3VMHyllzPqEf5alVj5bUvGpvDVi"; // http://docs.bbcnewslabs.co.uk/NewsHack-Wales.html
/* OH GOD WHY */
var articles = [];
var response;
/* Express settings */
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
/* Handlebar settings */
app.engine('handlebars', exphbs({
defaultLayout: 'layout',
helpers: {
foo: function () { return 'FOO!'; }
},
}));
app.set('view engine', 'handlebars');
app.get('/', function(req, res){
//getJuicerArticle({"articleId":"6e825b2e5becd6c489ad9bef124b22b8d0450dcb"}, function(data){
renderPage("home", null, res); // THIS NEEDS TO BE A LIST, BUT NOT HERE
//});
});
app.route('/run').post(function(req, res) {
articles = [];
response = res;
post = req.body;
var twitterQuery = "bbc.co.uk OR news.sky.com";
//var twitterQuery = "Xander_barnes";
// post.lat = 53.1436732;
// post.lng = -4.2727924;
var locationData = post.lat + "," + post.lng + "," + post.radius + "mi";
client.get('search/tweets', {q: twitterQuery,geocode:locationData, count:24, result_type: "recent"}, function(error, tweets, response) {
getArticlesFromTweets(tweets);
});
});
/* 404 Route */
app.get('*', function(req, res){
res.status(404).send("404, these are not the droids you are looking for.");
});
/* Start server */
server.listen(8000);
console.log("Server listening on http://localhost:8000");
/* ACTUAL FUNCTIONS THAT DO STUFF GO BELOW HERE */
function getArticlesFromTweets(tweets) {
var results = [];
tweets.statuses.map(function(item) {
item.entities.urls.map(function(url) {
if(url.expanded_url.length > 25) {
results.push(sha1URL(url.expanded_url));
}
});
});
var reduced = [];
var usedIds = {};
results.forEach(function(result){
if (!usedIds[result]) {
reduced.push(result)
usedIds[result] = 0;
}
usedIds[result] += 1;
})
reduced = [];
for(o in usedIds){
reduced.push({el:o, freq: usedIds[o]});
}
reduced.sort(function (a,b) {
if (a.freq < b.freq)
return 1;
if (a.freq > b.freq)
return -1;
return 0;
});
console.log("Articles: " + JSON.stringify(reduced, null, 2));
createArticleList(reduced);
}
function createArticleList(hashes) {
async.map(hashes, getJuicerArticle, function(e, data) {
response.render('home', {layout: false, data:articles});
//renderPage('home', articles, response);
});
}
var getJuicerArticle = function(hash, callback){
url = "http://data.test.bbc.co.uk/bbcrd-juicer/articles/";
url += hash.el;
url += "?apikey=" + BBC_API_KEY;
//console.log("Generated URI: " + url + " FREQ: " + hash.freq);
http.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var data = JSON.parse(body);
if(data) {
if(data.id) {
data.freq = hash.freq;
articles.push(data);
}
}
callback();
});
}).on('error', function(e) {
console.log("Got error: ", e);
});
};
/* Has a news article URL to use on juicer */
function sha1URL(url) {
var hash = crypto.createHash('sha1').update(url);
url = hash.digest('hex');
return url;
}
/* Return a page with data included */
function renderPage(page, data, res) {
res.render(page, {'data':data});
}
/* URL Expander */
///urlExpander.expand('http://bbc.in/1MOoEuU', function(err, longUrl){
//console.log("LONG: " + longUrl);
//});