Skip to content

Commit d96a34b

Browse files
committed
shameless first commit
1 parent 0a378b5 commit d96a34b

File tree

787 files changed

+112928
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

787 files changed

+112928
-1
lines changed

.DS_Store

6 KB
Binary file not shown.

Readme.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
1-
hues js
1+
# Fortuna Bot
2+
3+
A bot that links two strangers with a random conversation starter: share
4+
ideas, have fun with random people.
5+
6+
## Usage
7+
8+
### Run with default config (`config/config.json`)
9+
10+
`npm run`
11+
12+
### Run with custom config path
13+
14+
`bin/fortunabot path/to/config.json`
15+
16+
## Config Keys
17+
18+
These are the keys required or available in the config object.
19+
20+
TBD
21+
22+
Need for firebase.
23+
Need also for twitter.
24+
25+
## Documentation
26+
27+
Fortuna Bot is documented with JSDoc. To generate all the files you can
28+
run `npm run document`. HTML files will be generated in `./docs` which
29+
can be viewed by opening the files or serving them with an HTTP server
30+
(an easy way is to run `python -m SimpleHTTPServer`)
31+
32+
## Tests
33+
34+
To run the tests just run `npm test`. Right now we depend on `mocha` for
35+
this, so make sure it's installed.

bin.js

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
require('neon');
2+
3+
// var Hue = require('philips-hue');
4+
// var hue = new Hue();
5+
6+
7+
// // hue.getBridges(function(err, bridges){
8+
// // if(err) return console.error(err);
9+
// // console.log(bridges);
10+
11+
// // var bridge = bridges[0]; // use 1st bridge
12+
13+
// // hue.auth(bridge, function(err, username){
14+
// // if(err) return console.error(err);
15+
// // // console.log("bridge: "+bridge);
16+
// // // console.log("username: "+username);
17+
18+
// hue.bridge = "192.168.100.11"; // from hue.getBridges()
19+
// hue.username = "newdeveloper"; // from hue.auth()
20+
21+
// var state = {bri: 255, sat: 255, hue: 10000, effect:'none'};
22+
23+
// hue.light(1).setState(state);
24+
25+
// // hue.light(1).setState({effect: "colorloop"});
26+
27+
// // hue.light(1).setState({alert: "lselect"});
28+
// console.log('>>>>');
29+
// // });
30+
// // });
31+
32+
33+
34+
35+
// Class('Hues')({
36+
// start : function start () {
37+
// console.log('>>> started');
38+
// }
39+
// });
40+
41+
// Hues.start();
42+
43+
44+
45+
46+
47+
48+
49+
#!/usr/bin/env node
50+
51+
require('neon');
52+
53+
var ref, appData,
54+
fs = require('fs'),
55+
Firebase = require('firebase'),
56+
57+
configData = JSON.parse(fs.readFileSync('config/config.json', 'utf-8')),
58+
59+
TwitterClient = require('../lib/twitter_client'),
60+
Matcher = require('../lib/matcher');
61+
62+
var BOT_COMMAND = 'room';
63+
64+
/**
65+
* Fortunabot a bot that matches people with stuff and tweets that!
66+
*
67+
* #Usage
68+
* Fortunabot.tweetMatches(configData);
69+
*
70+
*/
71+
Class('Hues')({
72+
73+
/**
74+
* Kickstarts the matching process using the `Matcher` and tweet the results
75+
* using the TwitterClient
76+
*
77+
* @function tweetMatches
78+
* @memberof Fortunabot
79+
* @argument {object} [config] config object
80+
*
81+
*/
82+
tweetMatches : function tweetMatches (config){
83+
84+
//connect to firebase
85+
ref = new Firebase(config.firebaseUrl);
86+
87+
//get data
88+
ref.once("value", function (snapshot) {
89+
//save into global variable
90+
appData = snapshot.val();
91+
92+
//init in case on new db
93+
appData.mentions = appData.mentions || [];
94+
appData.unmatchedUsers = appData.unmatchedUsers || [];
95+
appData.topics = appData.topics || ['vampires', 'light'];
96+
97+
//kickstart the process
98+
this._process();
99+
100+
}.bind(this), function (errorObject) {
101+
console.log("The read failed: " + errorObject.code);
102+
});
103+
104+
},
105+
106+
/**
107+
* Verify for valid mentions and users, save already processed mentions and
108+
* keep only current unmatched users (allow multiple matches)
109+
*
110+
* @function _process
111+
* @memberof Fortunabot
112+
*
113+
*/
114+
_process : function _process(){
115+
//get mentions from timeline
116+
TwitterClient.getMentions(function(mentions){
117+
118+
//filter just NEW mentions
119+
mentions = mentions.filter(function (mention) {
120+
return appData.mentions.indexOf(mention.id) < 0;
121+
});
122+
123+
//iterate over them
124+
mentions.forEach(function mentionInterator (mention, index) {
125+
var noCommandPresent = true, alreadyUnmatchedUser;
126+
127+
//filter mentions only with the desired command
128+
if (mention.entities.hashtags.length) {
129+
mention.entities.hashtags.forEach(function(hashtag){
130+
if(hashtag.text === BOT_COMMAND){
131+
noCommandPresent = false;
132+
}
133+
})
134+
}
135+
136+
//no command? skip mention
137+
if(noCommandPresent){
138+
return;
139+
}
140+
141+
//save valid mention
142+
appData.mentions.push(mention.id);
143+
144+
//filter only unmatched users
145+
alreadyUnmatchedUser = appData.unmatchedUsers.some(function(user){
146+
user.name === mention.user.screen_name;
147+
});
148+
149+
//already on the list? skip user
150+
if(alreadyUnmatchedUser){
151+
return;
152+
}
153+
154+
//add user to unmatched list
155+
appData.unmatchedUsers.push({
156+
name: '@'+mention.user.screen_name}
157+
);
158+
159+
}.bind(this));
160+
161+
//well run it
162+
this._runMatcher();
163+
164+
}.bind(this));
165+
},
166+
167+
/**
168+
* Calls the Matcher lib
169+
*
170+
* @function _runMatcher
171+
* @memberof Fortunabot
172+
*
173+
*/
174+
_runMatcher : function _runMatcher () {
175+
176+
var tweets,
177+
matcher = new Matcher({
178+
users : appData.unmatchedUsers,
179+
topics : appData.topics,
180+
minimumSize : 2
181+
});
182+
183+
//called'it
184+
matcher.match(function (err, matchArray) {
185+
if (err) {
186+
console.log('darn!', err);
187+
}
188+
189+
//keep reference in caller variable
190+
tweets = matchArray;
191+
192+
//saeve unmatched users
193+
if (matcher.users.length) {
194+
appData.unmatchedUsers = matcher.users;
195+
}
196+
});
197+
198+
//recursive tweeting
199+
var doTweet = function doTweet(tweet){
200+
TwitterClient.post(tweet, function(){
201+
//check if any tweets letf
202+
if(tweets.length){
203+
doTweet(tweets.pop());
204+
}else{
205+
//finished? well, save the data
206+
ref.set(appData, function(){
207+
// success? exit!
208+
process.exit(0);
209+
});
210+
}
211+
}.bind(this));
212+
}.bind(this);
213+
214+
//only if matches happened
215+
if(tweets.length){
216+
doTweet(tweets.pop());
217+
}else{
218+
console.log('> sorry no tweets');
219+
process.exit(0);
220+
}
221+
222+
}
223+
224+
});
225+
226+
Fortunabot.tweetMatches(configData);
227+

bin/hueWorker.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
require('neon');
2+
3+
require('neon');
4+
5+
var ref, appData,
6+
fs = require('fs'),
7+
Firebase = require('firebase'),
8+
Hue = require('philips-hue'),
9+
hue = new Hue(),
10+
chroma = require('chroma-js'),
11+
config = JSON.parse(fs.readFileSync('config/config.json', 'utf-8')),
12+
colors = {
13+
red : {bri: 255, sat: 255, hue: 10000, effect:'none'},
14+
blue : {bri: 255, sat: 255, hue: 30000, effect:'none'},
15+
green : {bri: 255, sat: 255, hue: 50000, effect:'none'}
16+
},
17+
TwitterClient = require('../lib/twitter_client'),
18+
Matcher = require('../lib/matcher'),
19+
checkTimeout;
20+
21+
var BOT_COMMAND = 'room';
22+
23+
ref = new Firebase(config.firebaseUrl);
24+
25+
Class('HueWorker')({
26+
fetchQueue : function fetchQueue(){
27+
console.log('>', 'fetching');
28+
29+
//get data
30+
ref.once("value", function (snapshot) {
31+
//save into global variable
32+
appData = snapshot.val() || {};
33+
34+
//init in case on new db
35+
appData.commandQueue = appData.commandQueue || [];
36+
37+
//kickstart the process
38+
this._process();
39+
40+
}.bind(this), function (errorObject) {
41+
console.log("The read failed: " + errorObject.code);
42+
});
43+
44+
},
45+
46+
_process : function _process(){
47+
var command = appData.commandQueue.pop();
48+
49+
ref.set(appData, function(){
50+
// success? exit!
51+
if(command && command.entities.hashtags){
52+
command.entities.hashtags.forEach(function(hashtag){
53+
// console.log('>>>>>>', hashtag.text);
54+
// var color = chroma(hashtag.text);
55+
try{
56+
var color = chroma('239,247,255'),
57+
tweet = '@'+command.user.screen_name+' sup thanks for tha color: '+hashtag.text;
58+
59+
console.log('>', color.hsi());
60+
console.log('color.rgb()', color.rgb());
61+
console.log('color.hsl()', color.hsl());
62+
console.log('color.hsv()', color.hsv());
63+
console.log('color.lab()', color.lab());
64+
console.log('color.lch()', color.lch());
65+
console.log('color.gl()', color.gl());
66+
console.log('color.num()', color.num());
67+
68+
69+
// hue.bridge = "192.168.100.11"; // from hue.getBridges()
70+
// hue.username = "newdeveloper"; // from hue.auth()
71+
72+
// var state = {
73+
// bri: 255,
74+
// sat: 255,
75+
// hue: 50000,
76+
// effect:'none'
77+
// };
78+
79+
// hue.light(1).setState(state);
80+
// console.log('>>>>', command);
81+
82+
// TwitterClient.post(tweet, function(){
83+
// console.log('> posted:', tweet);
84+
// }.bind(this));
85+
86+
}catch(err){
87+
// console.log('>', err);
88+
}
89+
// if(color.hex){
90+
// console.log('>>>', color.hsv());
91+
// }
92+
// if(colors[hashtag.text]){
93+
// hue.bridge = "192.168.100.11"; // from hue.getBridges()
94+
// hue.username = "newdeveloper"; // from hue.auth()
95+
// var state = colors[hashtag.text];
96+
// hue.light(1).setState(state);
97+
// console.log('>>>>', command);
98+
// }
99+
});
100+
}
101+
102+
checkTimeout = setTimeout(HueWorker.fetchQueue.bind(HueWorker), 10000);
103+
});
104+
105+
}
106+
});
107+
108+
109+
checkTimeout = setTimeout(HueWorker.fetchQueue.bind(HueWorker), 10000);

0 commit comments

Comments
 (0)