forked from PlaceNL2022/Bot
-
Notifications
You must be signed in to change notification settings - Fork 7
/
saver.js
74 lines (69 loc) · 3.16 KB
/
saver.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
import WebSocket from 'ws';
import fs from 'fs';
import fetch from 'node-fetch';
const accessToken = "64865847-oTqvjJZKu_aO0mDu1aVcbXPMvRpWNg";
const ws = new WebSocket('wss://gql-realtime-2.reddit.com/query', 'graphql-ws', {
headers: {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:98.0) Gecko/20100101 Firefox/98.0",
"Origin": "https://hot-potato.reddit.com"
}
});
ws.onopen = () => {
ws.send(JSON.stringify({
'type': 'connection_init',
'payload': {
'Authorization': `Bearer ${accessToken}`
}
}));
ws.send(JSON.stringify({
"id": "1",
"type": "start",
"payload": {
"variables": {
"input": {
"channel": {
"teamOwner": "AFD2022",
"category": "CONFIG"
}
}
},
"extensions": {},
"operationName": "configuration",
"query": "subscription configuration($input: SubscribeInput!) {\n subscribe(input: $input) {\n id\n ... on BasicMessage {\n data {\n __typename\n ... on ConfigurationMessageData {\n colorPalette {\n colors {\n hex\n index\n __typename\n }\n __typename\n }\n canvasConfigurations {\n index\n dx\n dy\n __typename\n }\n canvasWidth\n canvasHeight\n __typename\n }\n }\n __typename\n }\n __typename\n }\n}\n"
}
}));
ws.send(JSON.stringify({
"id": "3",
"type": "start",
"payload": {
"variables": {
"input": {
"channel": {
"teamOwner": "AFD2022",
"category": "CANVAS",
"tag": "1"
}
}
},
"extensions": {},
"operationName": "replace",
"query": "subscription replace($input: SubscribeInput!) {\n subscribe(input: $input) {\n id\n ... on BasicMessage {\n data {\n __typename\n ... on FullFrameMessageData {\n __typename\n name\n timestamp\n }\n ... on DiffFrameMessageData {\n __typename\n name\n currentTimestamp\n previousTimestamp\n }\n }\n __typename\n }\n __typename\n }\n}\n"
}
}));
};
ws.onmessage = (message) => {
const {
data
} = message;
const parsed = JSON.parse(data);
if (parsed.payload && parsed.payload.data && parsed.payload.data.subscribe.data && parsed.payload.data.subscribe.data.name) {
const url = parsed.payload.data.subscribe.data.name
const fStream = fs.createWriteStream(`E:\\frames\\${parsed.payload.data.subscribe.data.currentTimestamp}.png`);
fetch(url)
.then(res => res.body.pipe(fStream))
.then(() => {
console.log(`${parsed.payload.data.subscribe.data.currentTimestamp} saved`);
}
);
}
}