-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot-u-sc.js
229 lines (210 loc) · 8.38 KB
/
bot-u-sc.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
require('dotenv').config();
console.log("Mastodon bot starting...");
const Tusk = require('tusk-mastodon');
const fs = require('fs'),
es = require('event-stream'),
path1 = 'songNumberTEMP.txt',
path2 = 'scpLikesAndRepostsTEMP.txt',
path3 = 'totalSongsNumberTEMP.txt';
var songToPost = "",
songNumber = 0,
songNumData = [],
currentSongNumStr = "",
//in case we fail to post, store the old num
oldSongNumStr = ""
i_as_string = "",
totalSongNum = 0,
totalSongNumData = [],
totalSongStr = "";
try {
songNumData = fs.readFileSync(path1);
totalSongNumData = fs.readFileSync(path3);
} catch (error) {
console.log("a READ error occurred, errno=" + error.errno + "\n")
console.log(error)
process.exit(error.errno);
}
if (songNumData.length != 0) {
songNumData.forEach(i => {
currentSongNumStr += String.fromCharCode(i);
oldSongNumStr = currentSongNumStr
});
} else {
console.log('songNumData was empty');
console.log('make sure bot-daemon.js has read access')
process.exit(1);
}
console.log("song number is: " + songNumData);
// the actual song read will be songNumber + 1
songNumber = parseInt(currentSongNumStr);
if (totalSongNumData.length != 0) {
totalSongNumData.forEach(i => {
totalSongStr += String.fromCharCode(i);
});
} else {
console.log('totalSongNumData was empty');
console.log('make sure bot-daemon.js has read access');
process.exit(1);
}
console.log("total songs number is: " + totalSongNumData);
totalSongNum = parseInt(totalSongStr);
if (songNumber == totalSongNum) {
songNumber = 0;
console.log('songNumber reset to zero since reached EOF')
}
const TUSK = new Tusk({
client_key: process.env.M_CLIENT_KEY,
client_secret: process.env.M_CLIENT_SECRET,
access_token: process.env.M_AUTH_TOKEN,
timeout_ms: 60*1000, // optional HTTP request timeout to apply to all requests.
api_url: process.env.M_INSTANCE_URL + '/api/v1/', // defaults to https://mastodon.social/api/v1/
})
var i = 0;
var s = fs.createReadStream(path2)
.pipe(es.split())
.pipe(es.mapSync(function(song) {
// pause the readstream
s.pause();
// the stream reads past the end of the file, causing a blank song to be read
// so to keep the total to not be counted one above the actual total,
// only increment if not blank
if (song != '') {
i++;
}
if (i > songNumber && i_as_string == "") {
console.log('songToPost = ' + song);
songToPost = song;
i_as_string = i.toString();
// "MEME_FOUND" custom error to catch, but unnecessary b/c destroy() calls .on(close)
s.destroy("MEME_FOUND")
return
}
s.resume();
})
.on('error', function(err) {
// error "MEME_FOUND" never called
if (err === "MEME_FOUND") {
console.log('Finished Reading .on(error === "MEME_FOUND")');
console.log(err)
totalSongStr = i.toString();
currentSongNumStr = i_as_string;
// finally, toot the new song
// toot(songToPost);
//DEBUG to circumvent toot(), remove updateSongNum() in production
updateSongNum(currentSongNumStr)
} else {
console.log("error 4324324312")
reject('Error occurred, errno=:' + err.errno + '\n', err);
process.exit(1)
}
})
// on(end) doesn't get called
.on('end', function(){
console.log('Finished Reading .on(end)');
totalSongStr = i.toString();
currentSongNumStr = i_as_string;
// finally, toot the new song
// toot(songToPost);
//DEBUG to circumvent toot(), remove updateSongNum() in production
updateSongNum(currentSongNumStr)
})
// on(close) gets called by mapstream.destroy() when song is found
.on('close', function(){
console.log('Finished Reading .on(close)');
totalSongStr = i.toString();
currentSongNumStr = i_as_string;
// finally, toot the new song
// toot(songToPost);
//DEBUG to circumvent toot(), remove updateSongNum() in production
updateSongNum(currentSongNumStr)
})
);
function toot(newSong) {
post_frequency = process.env.POST_VISIBILITY_FREQUENCY
_visibility = songNumber % post_frequency == 0 ? 'public' : 'unlisted'
const params = {
status: "this song came from my feed on SoundCloud: ⬇️\n\n"
+ newSong + "\n\n" +
"for more cool electronic music go here: ⬇️ \n\n"
+ "https://soundcloud.com/sour_cream_pringles" +
"\n\n" + "#EDM #acid #electro #IDM" + "\n\n\n\n" +
"♬♫♪ ヽ(⌐■_■)ノ ♪♫♬",
visibility: "direct",
file: "true" // forces tusk to send a multipart form request instead of a url query see tusk-mastodon/lib/mastodon.js:_buildReqOpts()
}
TUSK.post('statuses', params)
.then( function (promiseObject) {
data = promiseObject.data
resp = promiseObject.resp
rspCode = resp.status
instanceURL = TUSK.apiUrl
switch (true) {
case (rspCode > 199 && rspCode < 300):
//SUCCESS
console.log('success! :)')
console.log(`here is the toot on ${instanceURL}:`)
console.log(`ID: ${data.id} and timestamp: ${data.created_at}`)
// update songNum after posting to Mastodon
console.log("incrementing songNumber")
updateSongNum(currentSongNumStr)
break
default:
console.log(`request failed, response.statusCode= ${rspCode} statusText ${resp.statusText}`)
console.log("songNumber not changed:" + oldSongNumStr)
process.exit(1)
}
})
.catch( function (err) {
console.log("TUSK.post('statuses') failed, error = " )
if (err.statusCode || err.code) {
console.log(`statusCode= ${err.statusCode} err.code ${err.code}`)
}
console.log(err.message + "\n=======================")
console.log(err.stack)
console.log("songNumber not changed:" + oldSongNumStr)
process.exit(1)
})
}
function mastodonCallback(post_err, data, response, instanceURL) {
if (post_err) {
console.log("an error when tooting, errno=" + post_err.errno)
console.log("post_err is\n" + post_err)
console.log("data.error is\n" + data.error)
console.log("songNumber not changed:" + oldSongNumStr)
process.exit(1)
} else if (data.length < 1) {
console.log("no data")
console.log("songNumber not changed:" + oldSongNumStr)
process.exit(1)
} else {
rspCode = response.statusCode
switch (true) {
case (rspCode >= 200 && rspCode < 300):
//SUCCESS
console.log(`here is the toot on ${instanceURL}:`)
console.log(`ID: ${data.id} and timestamp: ${data.created_at}`)
// update songNum after successful post
console.log("incrementing songNumber")
updateSongNum(currentSongNumStr)
break
default:
console.log("request failed, response.statusCode= " + rspCode)
console.log("post_err is\n " + post_err)
console.log("data.error is\n" + data.error)
console.log("songNumber not changed:" + oldSongNumStr)
process.exit(1)
}
}
}
function updateSongNum(currentSongNumStr) {
try {
fs.writeFileSync(path1, currentSongNumStr);
console.log('songNumber changed to ' + currentSongNumStr);
fs.writeFileSync(path3, totalSongStr);
console.log('total songs = ' + totalSongStr);
} catch(error) {
console.log("a WRITE error occurred, errno=" + error.errno + "\n")
console.log(error)
process.exit(1)
}
}