-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
66 lines (56 loc) · 1.47 KB
/
Copy pathserver.js
File metadata and controls
66 lines (56 loc) · 1.47 KB
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
var fs = require('fs'),
path = require('path'),
Twit = require('twit'),
config = require(path.join(__dirname, 'config.js'))
var T = new Twit(config)
function random_from_array(images){
return images[Math.floor(Math.random() * images.length)]
}
function upload_random_image(images){
console.log('Opening an image...')
var image_path = path.join(__dirname, '/frames/' + random_from_array(images)),
b64content = fs.readFileSync(image_path, { encoding: 'base64' })
console.log('Uploading an image...')
T.post('media/upload', { media_data: b64content }, function (err, data, response) {
if (err){
console.log('ERROR:')
console.log(err)
}
else{
console.log('Image uploaded!')
console.log('Now tweeting it...')
T.post('statuses/update', {
media_ids: new Array(data.media_id_string)
},
function(err, data, response) {
if (err){
console.log('ERROR:')
console.log(err)
}
else{
console.log('Posted an image!')
}
}
)
}
})
}
const debug = false
fs.readdir(__dirname + '/frames', function(err, files) {
if (err){
console.log(err)
}
else{
var images = []
files.forEach(function(f) {
images.push(f)
})
// Do it once now
upload_random_image(images)
setInterval(function(){
upload_random_image(images)
// debug mode tweets every twenty seconds (20s * 1000ms = 20,000ms)
// if off, it tweets every nine hours (9h * 60m * 60s * 1000ms = 32,400,000)
}, debug ? 20000 : 32400000)
}
})