-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetbot.js
102 lines (88 loc) · 2.35 KB
/
tweetbot.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
var oauth = require('oauth-client');
var http = require('http');
var Canvas = require('canvas');
var colors = require('./colors');
var MIN_SLEEP = 0.5 * 60 * 60 * 1000;
var MAX_SLEEP = 1.5 * 60 * 60 * 1000;
var lastColor;
loop();
function loop() {
getImage(function(src) {
var img = new Canvas.Image();
img.src = src;
var canvas = new Canvas();
var color = colors.getSkyColor(img, canvas);
var hex = colors.hex(color);
var name = colors.findNearest(color);
if(lastColor != name) {
lastColor = name;
tweet(name + ' #' + hex);
updateColors(hex);
} else {
console.log('not tweeting ' + name + ' again');
}
});
var sleep = Math.round(MIN_SLEEP + Math.random() * (MAX_SLEEP - MIN_SLEEP));
console.log('sleeping ' + (sleep / 60 / 1000) + ' minutes, see you at ' +
new Date(sleep + new Date().valueOf()).toString());
setTimeout(loop, sleep);
}
function getImage(callback) {
var url = 'http://www.met.fu-berlin.de/wetter/webcam/Cam00_prev.jpg';
var req = http.get(url, function(res) {
if(res.statusCode == 200) {
var chunks = [];
res.on('data', function(chunk) {
chunks.push(chunk);
});
res.on('end', function() {
var src = Buffer.concat(chunks);
callback(src);
});
} else {
console.error('http error fetching image: ' + res.statusCode);
}
});
req.on('error', function(e) {
console.error('error fetching image: ' + e.message);
});
}
function updateColors(color) {
twitter('account/update_profile_colors.json', {
profile_background_color: color
});
}
function tweet(text) {
twitter('statuses/update.json', {status: text});
}
function twitter(path, body) {
var signer = oauth.createHmac(
oauth.createConsumer(process.env.CONSUMER_KEY, process.env.CONSUMER_SECRET),
oauth.createToken(process.env.TOKEN, process.env.TOKEN_SECRET)
);
var request = {
port: 443,
host: 'api.twitter.com',
https: true,
path: '/1.1/' + path,
oauth_signature: signer,
method: 'POST',
body: body
};
var req = oauth.request(request, function(res) {
if(res.statusCode != 200) {
console.log('http error on ' + path, body, res.statusCode);
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log(chunk);
});
} else {
console.log('success ' + path, body);
}
});
req.write(body);
req.end();
req.on('error', function(e) {
console.error('request error: ' + e.message);
});
}