-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient.js
executable file
·255 lines (214 loc) · 8.58 KB
/
client.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"use strict";
require('dotenv').config();
const PORT = process.env.PORT || 6502;
const HOST = process.env.SERVER || 'localhost'
var POLL_DELAY = process.env.POLL_DELAY || 5000
const TEST = (process.argv.indexOf("test") > -1)
const TRY = (process.argv.indexOf("try") > -1)
POLL_DELAY = TEST ? 0 : POLL_DELAY ;
const emulationDuration = 33; // seconds
const startFrame = 1587; // frames (50fps when vsync active)
const fs = require('fs');
const requirejs = require('requirejs'); // for jsbeeb compatibility
const https = require('https');
const cert_path = "./certs/";
const parser = require('./parser');
const gifsicle = require('gifsicle');
const beebjit = require('./beebjit');
const cache = require('./cache');
let beebState = null;
var mastodon = TRY ? null : require(TEST ? './test' : './mastodon');
var tweetServer = {
hostname: HOST,
port: PORT,
method: 'GET',
key: fs.readFileSync(cert_path+'client_key.pem'),
cert: fs.readFileSync(cert_path+'client_cert.pem'),
ca: fs.readFileSync(cert_path+'server_cert.pem')
};
function exec(cmd) {
const exec = require('child_process').exec;
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.warn(error);
}
resolve(stdout? stdout.trim() : stderr);
});
});
}
function log(l){console.log(l);}
var clientID = "Cli0";
require( 'console-stamp' )( console, { pattern: 'dd/mm/yyyy HH:MM:ss '},clientID+":" );
requirejs.config({
baseUrl: "./node_modules/jsbeeb/",
paths: {
'jsunzip': 'lib/jsunzip',
'promise': 'lib/promise-6.0.0',
'underscore': 'lib/underscore-min',
'emulator':'../../jsbeeb'
}
});
requirejs(['emulator'],
function (jsbeeb) {
"use strict";
async function run(tweet){
// Tweet ID will be used in tmp filenames passed into shell exec, so check it's safe. For a real tweet it should be numeric while for a testcase it can contain alphanumerics.
if (/\W/.test(tweet.id)) {
console.error("id contained unexpected character");
process.exit(1);
}
console.log("");
console.log("Running "+tweet.id+" from "+tweet.account.url);
var c = parser.parseTweet(tweet);
console.log("Parser output",c);
// If rude, block the user and do not run the program
if (c.rude) {
console.warn("BLOCKED @"+tweet.user.screen_name)
await mastodon.block(tweet);
setTimeout(requestTweet, POLL_DELAY);
return;
}
let start = new Date()
let media_path = "./tmp/"+tweet.id;
// Emulate on Beebjit
if (c.emulator == "beebjit") {
var frame_path = "./tmp/beebjit_frame_";
var audio_file = null;
var pixel_format = "bgra";
var emu_name = "beebjit";
beebState = await beebjit(c, jsbeeb);
if (beebState === null) setTimeout(requestTweet, POLL_DELAY);
} else // JSbeeb
{
var frame_path = media_path + "frame";
audio_file = media_path + "audiotrack.raw";
var pixel_format = "rgba";
var emu_name = "jsbeeb";
var frames = await jsbeeb.emulate(c.input,frame_path,audio_file,emulationDuration,startFrame);
if (!fs.existsSync(audio_file)) audio_file = null;
}
var end = new Date() - start
console.log(emu_name+" DONE in %ds ",end/1000);
// Count unique video frames
var shasum_check = (await exec("sha1sum client.js | awk '{print $1}' | wc -l")); // should equal 1
var shasum = (shasum_check > 0) ? "sha1sum" : "shasum";
var frames = (await exec(shasum+" "+frame_path+"*."+pixel_format+" | awk '{print $1}' | wc -l"));
var uniqueFrames = (await exec(shasum+" "+frame_path+"*."+pixel_format+" | awk '{print $1}' | sort | uniq | wc -l"));
console.log("Captured "+frames+" frames ("+uniqueFrames+" unique) "+frame_path);
start = new Date();
if (frames == 0) {
// NO VIDEO -> NOTHING
var ffmpegCmd = "";
} else if (uniqueFrames==1 && audio_file === null) {
// STATIC IMAGE WITHOUT SOUND -> PNG SCREENSHOT
var mediaFilename = media_path+'.png';
var mediaType = 'image/png';
var ffmpegCmd = './ffmpeg -hide_banner -y -f rawvideo -pixel_format '+pixel_format+' -video_size 640x512 -i '+frame_path+(frames-1)+'.'+pixel_format+' -vf "scale=1280:1024" '+mediaFilename
} else {
// ANIMATION OR STATIC IMAGE WITH SOUND -> GIF
var mediaFilename = media_path+'.gif';
var mediaType = 'image/gif';
var ffmpegCmd = './ffmpeg -hide_banner -loglevel panic ';
if (audio_file !== null) {
ffmpegCmd = ffmpegCmd + '-f f32le -ar 44100 -ac 1 -i '+audio_file;
}
ffmpegCmd = ffmpegCmd + '-y -f image2 -r 50 -s 640x512 -pix_fmt '+pixel_format+' -vcodec rawvideo -i '+frame_path+'%d.'+pixel_format+' -i images/palette.png -filter_complex "[0:v][1:v] paletteuse" -af "highpass=f=50, lowpass=f=15000,volume=0.5" -b:v 8M -b:a 128k -strict -2 -shortest '+mediaFilename
}
if (frames > 0) {
await exec(ffmpegCmd);
var checksum = await exec(shasum+" "+frame_path+(frames-1)+'.'+pixel_format+" | awk '{print $1}'");
} else {
var checksum = '';
}
exec('rm -f '+frame_path+'*.'+pixel_format);
if (audio_file !== null) {
fs.unlinkSync(audio_file);
}
if (frames > 1) {
var output = await exec ("gifsicle "+mediaFilename+" --optimize=3 --colors=16 --output "+mediaFilename);
console.log("Gifsicle:"+output);
}
var end = new Date() - start
console.log("Ffmpeg DONE in %ds ",end/1000);
if (frames == 0) {
mastodon.noOutput(tweet);
} else {
var hasAudio = (audio_file !== null);
let title = tweet.spoiler_text || "Untitled";
// Summarize toot data
let tootData = {
"prog":c.input,
"mode":c.mode,
"src": tweet.url,
"title": title
};
// Save state to cache
let tag = (TEST || TRY) ? "test" : await cache(tootData, beebState);
// Post a video toot
mastodon.videoReply(mediaFilename,mediaType,tweet.id,"@"+tweet.account.acct,tweet,checksum,hasAudio,tag);
}
setTimeout(requestTweet, POLL_DELAY);
};
function requestTweet() {
tweetServer.path="/pop";
https.get(tweetServer, (resp) => {
let data = '';
resp.setEncoding('utf8');
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
var tweet = JSON.parse(data);
if (typeof tweet.text === 'undefined') {
setTimeout(requestTweet, 5000);
return;
} else {
if (TEST && tweet.text == null) {process.exit()};
run(tweet).catch((err) => {
console.error(err);
if (TEST) {
tweetServer.path="/quit";
https.get(tweetServer);
process.exit(1);
}
});
}
});
}).on("error", (err) => {
log("Error: " + err.message);
log('Retry in 5 seconds');
setTimeout(requestTweet, 5000);
});
}
var try_arg = process.argv.indexOf("try") + 1;
if (try_arg > 0) {
var try_file = (try_arg == process.argv.length) ? "/dev/stdin" : process.argv[try_arg];
var toot = {
text: fs.readFileSync(try_file, 'utf8'),
id: 'try',
user: { screen_name: 'try' },
account: { username: 'try', url: 'https://localhost/@try' }
};
// Set up mastodon object to mock the methods that we use.
mastodon = {};
mastodon.videoReply = function(filename,mediaType,replyTo,text,toot,checksum,hasAudio) {
console.log("Generated " + mediaType);
exec("xdg-open "+filename);
process.exit();
};
mastodon.block = function(toot) {
console.log("Failed: Toot blocked because of badwords");
process.exit(1);
};
mastodon.noOutput = function(toot) {
console.log("Failed: No output captured");
process.exit(1);
};
run(toot);
} else {
requestTweet();
}
}
);