-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
128 lines (106 loc) · 2.8 KB
/
database.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
var MongoClient = require('mongodb').MongoClient;
var format = require('util').format;
var EventEmitter = require('events').EventEmitter;
var dbEmitter = new EventEmitter();
var col = {};
var db;
var ARTIST_ARRAY = 'artist_property';
var ALL_TWEET = 'all_tweet';
exports.init = function() {
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db_){
if (err) {
throw err;
}
db_.collection(ALL_TWEET, function(err, col_){
db = db_;
col.tweet = col_;
dbEmitter.emit("db-ready-tweet");
});
db_.collection(ARTIST_ARRAY, function(err, col_){
db = db_;
col.artist = col_;
dbEmitter.emit("db-ready");
});
});
}
exports.close = function() {
if (db) {
db.close();
}
}
exports.update = function(arg) {
console.log("in database.js");
console.log(arg);
if (col.artist === undefined) {
dbEmitter.on("db-ready", function(stream){
realUpdate(arg);
});
} else {
realUpdate(arg);
}
}
var realUpdate = function(arg) {
var len = arg.length;
for(var i = 0; i < len; i++) {
var p = arg[i];
console.log(p);
col.artist.findOne({artist: p.artist}, function(err, doc){
if (doc) {
col.artist.update({artist: p.artist}, {$inc: {score: p.score, count: p.count}}, function(err, docs){
if (err) console.warn(err.message);
});
} else {
col.artist.insert(p, function(err, docs){
if (err) console.warn(err.message);
});
}
});
}
}
exports.queryall = function(callback) {
col.artist.find({}).toArray(function(err, docs){
docs.forEach(function(elem, index, array){
docs[index].rating = (elem.score / elem.count) / 2 + 0.5;
});
callback(docs);
});
}
exports.draw = function(artist, callback) {
if (col.tweet == undefined) {
dbEmitter.on("db-ready-tweet", function(stream){
realDraw(artist, callback);
});
} else {
realDraw(artist, callback);
}
}
var realDraw = function(artist, callback) {
col.tweet.find({artist: artist}).toArray(function(err, docs){
var n = docs.length;
if (n > 0){
var rand = Math.floor(Math.random()*n);
console.log("Lottery Luck guy:");
console.log(docs[rand]);
var ch = docs[rand];
var obj = {text: ch.text, name: ch.user.name, screen_name: ch.user.screen_name,
phrase: ch.phrase, created_at: ch.created_at, thumbnail: ch.user.profile_image_url};
callback(obj);
} else {
callback({text: "", name:"", screen_name:""});
}
});
}
exports.log = function(doc) {
if (col.tweet == undefined) {
dbEmitter.on("db-ready-tweet", function(stream){
realLog(doc);
});
} else {
realLog(doc);
}
}
var realLog = function(doc) {
col.tweet.insert(doc, function(err, docs){
if (err) console.warn(err.message);
});
}