-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
155 lines (118 loc) · 3.64 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var gm = require('gm'),
fs = require('fs'),
snapchat = require('snapchat'),
client = new snapchat.Client(),
config = require('./config.json'),
sendSnap = require('./sendSnap.js'),
takePicture = require('./takePicture.js'),
cleanSnaps = require('./cleanSnaps.js'),
moment = require('moment'),
dir = '.',
imgPath = dir+'/img',
inPath = imgPath+'/cam.jpeg',
outPath = imgPath+'/out.jpeg'; // jpeg is important because the 'streamer' package depends on it
console.log("Logging in as '" + config.snapchat.username + "'...");
client.login(config.snapchat.username, config.snapchat.password).then(function(){
console.log("Logged in.");
// sync initially and then syc every 'timeInterval'
syncSnaps(client);
console.log('Start loop for new snaps.');
(function loop(){
// random number between min and max
var timeMin = 20, timeMax = 45,
timeInterval = (Math.floor(Math.random() * (timeMax - timeMin + 1)) + timeMin) * 1000;
console.log("Timeout in: "+ timeInterval/1000 +"s");
setTimeout(function(){
console.log('\n\n=========\nLoop!');
syncSnaps(client);
loop();
}, timeInterval);
}());
},
function(err){
console.error("Failed to login");
console.error(err)
});
function syncSnaps(client){
console.log('Syncing snaps...');
client.sync(function(err, data){
if(err){
console.error("Failed to sync");
console.error(err);
return;
}
console.log('Synced snaps!');
// if theres snaps clear the feed as soon as we sync
if(data.snaps.length != 0){
console.log("Clearing feed...");
client.clear(function(){
console.log("Feed cleared");
});
}
// clean up snapchat data
cleanSnaps(data, function(users){
if(users.length > 0){
console.log("Users:", users);
takeAndSend(users);
}else{
console.log("No users");
}
});
});
}
function takeAndSend(usernames){
var snapLength = 10; // length of the snap in secs
takePicture(inPath, function(){
console.log('Picture taken and saved.');
gm(inPath).size(function(err, img){
fixImage(inPath, img, function(){
console.log('Sending out snapchats!');
sendSnap(client, snapLength, outPath, usernames);
});
});
});
}
function fixImage(inPath, img, callback){
var x = img.height,
y = img.width,
data = client.lastSync.data,
mode = require('./modes.json')[config.mode],
font = { "type": mode.font.type || 'Arial', "colour": mode.font.colour || '#ffffff' },
colour = mode.colour || [0, 0, 0],
url = mode.url || '',
watermark = '';
if(mode.watermark){
watermark = '--'+mode.name;
}
if(mode.colour == "random"){
colour = [Math.random()*100, Math.random()*100, Math.random()*100]
}
gm(inPath)
.rotate('black', 90)
.crop(x - 120, y, 0, 0)
// .colorize(colour[0], colour[1], colour[2])
.fontSize(20)
.font(font.type)
.fill(font.colour)
.drawText(20, 35, data.username, 'NorthWest')
.drawText(20, 65, moment().format('h:mm A'), 'NorthWest')
.drawText(20, 90, moment().format('D/M/YY'), 'NorthWest')
.drawText(20, 115, data.score+' / '+ data.sent, 'NorthWest')
// .drawText(0, 100, url, 'South')
.write(outPath, function(err){
if (err) return console.dir(arguments)
console.log('Rotated, cropped, coloured and added text to picture.');
// console.log(this.outname + " created :: " + arguments[3]);
gm().subCommand('composite')
.gravity('center')
.in('-compose', 'Over', imgPath+'/watermark'+watermark+'.png', outPath)
.write(outPath, function(err){
if (err) return console.dir(arguments)
console.log('Applied watermark to picture.');
// console.log(this.outname + " created :: " + arguments[3]);
callback();
}
);
}
);
}