forked from zileeva/instagram-hashtag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
137 lines (127 loc) · 3.13 KB
/
app.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
var mysql = require('./config/mysql.js');
var post = require('./modules/Post.js');
var hashtag = require('./modules/Hashtag.js');
var user = require('./modules/User.js');
var async = require('async');
// to run node app.js
/**
* Gets the user either from Instagram or our database depending on
* whether we have previously made an insert
* @param instagramUserID instagram user id
* @param callback callback
*/
function getUser(instagramUserID, callback) {
user.getUser(instagramUserID, function(err, res) {
if (res) {
callback(null, res);
} else {
user.getUserFromIG(instagramUserID, function(err, res) {
if (err) {
callback(err, null);
} else {
user.insertUser(res, function(err, res) {
if (err) {
console.log(err);
callback(err, null);
} else {
user.getUser(instagramUserID, function(err, res) {
if (err) {
console.log(err);
callback(err, null);
} else {
callback(null, res);
}
})
}
})
}
})
}
})
};
/**
* Main function to get user, insert/score the post and insert/score hashtag
* @param instagramPost Instagram post
* @param callback callback
*/
function control(instagramPost, callback) {
async.waterfall([
function(callback) {
getUser(instagramPost.user.id, function(err, user) {
if (err) {
callback(err, null);
console.log(err);
} else {
callback(null, user);
}
})
},
function(user, callback) {
post.insertPost(instagramPost, user, function(err, res) {
if (err) {
callback(err, null);
console.log(err);
} else {
callback(null);
}
})
},
function(callback) {
hashtag.hashtagInit(instagramPost, function(err, hashtag) {
if (err) {
callback(err, null);
console.log(err);
} else {
callback(null, hashtag);
}
})
}
], function (err, result) {
callback(err, result);
});
}
/**
* main function to call to get new posts as well as display the top hashtags
* (Asynchronously)
*/
function postsInit() {
post.getPostsByLocations(function (err, res) {
if (err) {
console.log("ERROR");
console.log(err);
} else {
for (var i = 0; i < res.length; i++) {
if (res[i].tags.length > 0) {
control(res[i], function (err, res) {
if (err) {
console.log(err)
throw err
}
})
}
else {
console.log('no tags')
}
}
console.log('top hashtags')
hashtag.topHashtags(function(err, results) {
if (err) {
throw err
}
else {
console.log(results)
}
})
console.log('top hashtags used at least 2 times (to remove outliers');
hashtag.topHashtagsMinUsedTimes(2, function(err, results) {
if (err) {
throw err
}
else {
console.log(results)
}
})
}
})
}
postsInit();