-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
153 lines (137 loc) · 4.62 KB
/
index.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
var pg = require('pg');
var express = require('express');
var Twitter = require('twitter');
var Clarifai = require('clarifai');
var schedule = require('node-schedule');
var app = express();
// Load credentials from environment variables
var dbUrl = process.env.DATABASE_URL;
var clarifaiKey = process.env.clarifaiKey;
var clarifaiSecret = process.env.clarifaiSecret;
var twitterConsumerKey = process.env.twitterConsumerKey;
var twitterConsumerSecret = process.env.twitterConsumerSecret;
var twitterAccessTokenKey = process.env.twitterAccessTokenKey;
var twitterAccessTokenSecret = process.env.twitterAccessTokenSecret;
// Twitter Client for getting latest tweets.
var twitterClient = new Twitter({
consumer_key: twitterConsumerKey,
consumer_secret: twitterConsumerSecret,
access_token_key: twitterAccessTokenKey,
access_token_secret: twitterAccessTokenSecret
});
// Clarifai client for extracting concepts from images.
var clarifaiApp = new Clarifai.App(
clarifaiKey,
clarifaiSecret
);
// Submit the image to Clarifai, for learning about the concepts that are
// present inside the image.
function predict(imageUrl) {
clarifaiApp.models.predict(Clarifai.GENERAL_MODEL, imageUrl).then(
function(response) {
console.log("Image: " + imageUrl);
// If the response in not Ok, just return.
if (response.status.description !== "Ok") {
console.log("Invalid response");
console.log(response);
return;
}
for (var i = 0; i < response.outputs.length; i++) {
var output = response.outputs[i];
var concepts = output.data.concepts;
if (concepts === undefined) {
console.log("Invalid output");
console.log(output);
continue;
}
// Loop over all the concepts present in the image and
// store them in the DB.
for (var ci = 0; ci < concepts.length; ci++) {
var concept = concepts[ci];
storeInDB(concept);
}
}
},
function(err) {
console.error(err);
}
);
}
// Increments the counter of concepts that are related to happniess.
var storeInDB = function(concept) {
console.log(concept);
var name = concept.name;
var value = concept.value;
// If the concept "name" is not present in the DB, then inserts a new record with
// "count" intialized to 1.
// Else if the concept "name" is already present in the DB, then increment the "count" column by 1.
var sqlQuery = "INSERT INTO TAGS (name, count) values ('" + name + "', 1) ON CONFLICT (name) DO UPDATE SET count = TAGS.count + 1;"
console.log("Running query: " + sqlQuery);
pg.connect(dbUrl, function(err, pgClient, done) {
pgClient.query(sqlQuery, function(err, result) {
done();
if (err)
{ console.error(err); }
else
{ console.log(result.rows); }
});
});
}
// Driver function, which queries for tweets containing keyword "happy" and
// filter the ones which contains images.
var work = function() {
twitterClient.get('search/tweets', {q: 'happy filter:images'}, function(error, tweets, response) {
var statuses = tweets.statuses;
for (var i = 0; i < statuses.length; i++) {
var tweet = statuses[i];
var entities = tweet.entities;
var medias = entities.media;
// If the tweet contains an image, then pass it onto Clarifai
// to learn more about the concepts it contains.
if (medias !== undefined) {
for (var j = 0; j < medias.length; j++) {
var media = medias[j];
console.log(media.media_url);
predict(media.media_url);
}
}
}
});
}
// Local debug
// work();
var rule = new schedule.RecurrenceRule();
rule.second = 2;
var job = schedule.scheduleJob(rule, function(){
work();
});
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/', function(request, response) {
// Whenever the dashboard is loaded, query the DB for the things that
// internet is happy about.
var sqlQuery = "SELECT * FROM TAGS ORDER BY count DESC LIMIT 25"
pg.connect(dbUrl, function(err, pgClient, done) {
pgClient.query(sqlQuery, function(err, result) {
done();
if (err) {
console.error(err);
} else {
response.render('pages/index', {rows: result.rows});
}
});
});
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
/**
TABLE SCHEMA:
CREATE TABLE TAGS(
NAME TEXT PRIMARY KEY NOT NULL,
COUNT INT NOT NULL
);
*/