-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.js
146 lines (125 loc) · 4.19 KB
/
scraper.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
const http = require('https'); // or 'http' for http:// URLs
const Matrix = require('./matrix.js');
const EnumColor = require('./colors.js');
const events = require("events");
const EventSource = require("eventsource");
const { threadId } = require('worker_threads');
class Scraper {
constructor(fingerprint, coords) {
this.colors = new EnumColor();
this.canvas = null;
this.coords = coords;
this.x = coords.x;
this.y = coords.y;
this.w = coords.w;
this.h = coords.h;
this.eventEmitter = new events.EventEmitter();
this.eventUrl = "https://pixelcanvas.io/";
this.fingerprint = fingerprint;
return this;
}
downloadCanvas(x, y) {
const ORIGIN = 'https://pixelcanvas.io';
const headers = {
'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',
'Origin': ORIGIN,
'Referer': ORIGIN
};
return new Promise((res, rej) => {
const request = http.get({ hostname: "pixelcanvas.io", path: `/api/bigchunk/${x}.${y}.bmp`, headers: headers }, function (response) {
let data = [];
var headerDate;
if (response.headers) {
if (response.headers.date) {
headerDate = response.headers.date;
} else {
headerDate = "No response date given";
}
}
/*console.log('Status Code:', response.statusCode);
console.log('Date in Response header:', headerDate);*/
response.on('data', chunk => {
data.push(chunk);
});
response.on('end', () => {
const result = Buffer.concat(data);
res(result);
});
}).on("error", err => {
rej(err);
});
});
}
get() {
return new Promise(async (res, rej) => {
const x = this.x; // start coords
const y = this.y;
const h = this.h;
const w = this.w;
const canvas = new Matrix(x, y, w, h);
let wc = Math.floor((x + w + 448) / 960);
let hc = Math.floor((y + h + 448) / 960);
let xc = Math.floor((x + 448) / 960);
let yc = Math.floor((y + 448) / 960);
for (let iy = yc; iy < (hc + 1); iy++) {
for (let ix = xc; ix < (wc + 1); ix++) {
try {
const data = await this.downloadCanvas(ix * 15, iy * 15);
let i = 0;
let offX = ix * 960 - 448;
let offY = iy * 960 - 448;
data.forEach((b) => {
let tx = offX + (Math.floor(i / (64 * 64)) % 15) * 64 + (i % (64 * 64)) % 64;
let ty = offY + Math.floor(i / (64 * 64 * 15)) * 64 + Math.floor((i % (64 * 64)) / 64);
let c = b >> 4;
canvas.update(tx, ty, this.colors.index(c));
i += 1;
tx = offX + (Math.floor(i / (64 * 64)) % 15) * 64 + (i % (64 * 64)) % 64;
ty = offY + Math.floor(i / (64 * 64 * 15)) * 64 + Math.floor((i % (64 * 64)) / 64);
c = b & 0xF;
canvas.update(tx, ty, this.colors.index(c));
i += 1;
});
} catch(e) {
rej(e);
}
}
}
this.eventEmitter.emit("ready", canvas);
this.canvas = canvas;
res(canvas);
});
}
getColor(x, y) {
return this.canvas.getColor(x, y);
}
emit(event, data) {
this.eventEmitter.emit(event, data);
}
on(event, callback) {
this.eventEmitter.on(event, callback);
}
once(event, callback) {
this.eventEmitter.once(event, callback);
}
connectEventSource() {
this.source = new EventSource(this.eventUrl + "events?fingerprint=" + this.fingerprint);
this.source.onmessage = (e) => {
const data = JSON.parse(e.data);
if (data.x >= this.x && data.x <= this.x + this.w) {
if (data.y >= this.y && data.y <= this.y + this.h) {
this.canvas.update(data.x, data.y, this.colors.index(data.color));
data.color = this.colors.index(data.color);
this.emit("update", data);
}
}
}
this.source.onopen = (e) => {
this.eventEmitter.emit("connectionReady", e);
}
this.source.onerror = (e) => {
this.eventEmitter.emit("connectionError", e);
};
}
}
module.exports = Scraper;