-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (77 loc) · 2.02 KB
/
index.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
const request = require('request');
const {
SimplyWS
} = require('simplyws');
const WebSocket = require('ws');
const pod = process.env.pod;
const access_token = process.env.access_token;
const timeout = 6000;
const reconnectInterval = 600000;
const favouriteStatus = (statusId) => {
request({
url: `https://${pod}/api/v1/statuses/${statusId}/favourite`,
method: 'POST',
headers: {
'Authorization': `Bearer ${access_token}`
},
}, (error, response, body) => {
console.log(`${statusId} favourite response: ${body} \n`);
if (error || response.statusCode != 200) {
setTimeout(() => {
favouriteStatus(statusId);
}, timeout);
}
});
};
const favouriteLatestToots = () => {
request({
url: `https://${pod}/api/v1/timelines/public?local=true&access_token=${access_token}`,
method: 'GET',
}, (error, response, body) => {
if (error || response.statusCode != 200) {
setTimeout(() => {
favouriteLatestToots();
}, timeout);
} else {
let responseData = JSON.parse(body);
responseData.forEach((status) => {
if (!status.favourited && !status.application.name.toLowerCase().includes('crossposter')) {
favouriteStatus(status.id);
}
});
}
});
}
var ws, simplyWS;
const connect = () => {
favouriteLatestToots();
ws = new WebSocket(`wss://${pod}/api/v1/streaming/?stream=public:local&access_token=${access_token}`);
simplyWS = new SimplyWS({
socket: ws,
autoConnects: true
});
simplyWS.on('open', () => {
console.log('connected');
});
simplyWS.on('close', () => {
console.log('closed');
});
simplyWS.on('error', (error) => {
console.log('error:', error);
});
simplyWS.on('message', (msg) => {
let message = JSON.parse(msg);
let status = JSON.parse(message.payload);
if (message.event == 'update') {
if (!status.application.name.toLowerCase().includes('crossposter')) {
console.log(`new message: ${message.payload} \n${'-'.repeat(50)}`);
favouriteStatus(status.id);
}
}
});
};
connect();
setInterval(() => {
simplyWS.close();
connect();
}, reconnectInterval);