-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen.js
255 lines (208 loc) · 9.13 KB
/
gen.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
const fs = require('fs');
const simplify = require('@turf/simplify');
const sharp = require('sharp');
const feeds = JSON.parse(fs.readFileSync('./feeds.json', 'utf8')).all;
const extraConfig = JSON.parse(fs.readFileSync('./extraConfig.json', 'utf8'));
const globalConfig = JSON.parse(fs.readFileSync('./globalConfig.json', 'utf8'));
const keyGen = () => "pseudo101_" + 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
})
const average = array => array.reduce((a, b) => a + b) / array.length;
let shapesList = [];
let imagesList = [];
let colorsList = [];
const updateFeed = async (feed) => {
if (!fs.existsSync(`./data/${feed.username}`)) {
fs.mkdirSync(`./data/${feed.username}`);
fs.mkdirSync(`./data/${feed.username}/icons`);
}
const key = keyGen();
console.log('Updating feed', feed.fullname);
/*
const deviceIdReq = await fetch(`https://passiogo.com/goServices.php?register=1&deviceId=0&token=${key}&platform=web&buildNo=undefined&oldToken=`, {
"headers": {
'Host': 'rutgers.passiogo.com',
'User-Agent': 'Mozilla/ 5.0(Windows NT 10.0; Win64; x64; rv: 109.0) Gecko / 20100101 Firefox / 116.0',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'X-Requested-With': 'XMLHttpRequest',
'Connection': 'keep-alive',
'Contact': 'I know I am not meant to be here. If you would like to contact me, i am available at [email protected]',
'Referer': 'https://passiogo.com/',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
},
"method": "GET",
});
const { deviceId } = await deviceIdReq.json();
*/
const deviceId = 64638265; //cheeky bastard moment
const routesForm = `json=%7B%22systemSelected0%22%3A%22${feed.id}%22%2C%22amount%22%3A1%7D`;
const stopsForm = `json=%7B%22s0%22%3A%22${feed.id}%22%2C%22sA%22%3A1%7D`;
const routesReq = await fetch(`https://passiogo.com/mapGetData.php?getRoutes=1&deviceId=${deviceId}&wTransloc=1`, {
"credentials": "omit",
"headers": {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0",
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Language": "en-US,en;q=0.5",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site"
},
"referrer": "https://passiogo.com/",
"body": routesForm,
"method": "POST",
"mode": "cors"
});
const stopsReq = await fetch(`https://passiogo.com/mapGetData.php?getStops=2&deviceId=${deviceId}&withOutdated=1&wBounds=1&showBusInOos=0&lat=undefined&lng=undefined&wTransloc=1`, {
"credentials": "omit",
"headers": {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0",
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Language": "en-US,en;q=0.5",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site"
},
"referrer": "https://passiogo.com/",
"body": stopsForm,
"method": "POST",
"mode": "cors"
});
const routes = await routesReq.json();
const stops = await stopsReq.json();
if (routes.error || stops.error) return; //this agency is invalid
//fs.writeFileSync('./routes.json', JSON.stringify(routes));
//fs.writeFileSync('./stops.json', JSON.stringify(stops));
const busTemplate = fs.readFileSync('./templates/bus.svg', 'utf8');
const circleTemplate = fs.readFileSync('./templates/circle.svg', 'utf8');
const arrowTemplate = fs.readFileSync('./templates/arrow.svg', 'utf8');
let iconsRef = [];
routes.map((route) => {
return [route.color.replace('#', ''), route.myid]
}).forEach((routeColor) => {
let actualColor = routeColor[0].toString().toUpperCase();
if (globalConfig.colorReplacements[actualColor]) actualColor = globalConfig.colorReplacements[actualColor];
//console.log(actualColor, routeColor[1])
const busIcon = busTemplate.replaceAll("FILL", `#${actualColor}`).replaceAll("BORDERS", feed.black && feed.black.includes(routeColor[1]) ? '#000000' : '#FFFFFF');
const busBuffer = Buffer.from(busIcon, 'utf8');
iconsRef.push(`${actualColor.toUpperCase()}_bus.png`);
const arrowIcon = arrowTemplate.replaceAll("FILL", `#${actualColor}`).replaceAll("BORDERS", feed.black && feed.black.includes(routeColor[1]) ? '#000000' : '#FFFFFF');
const arrowBuffer = Buffer.from(arrowIcon, 'utf8');
iconsRef.push(`${actualColor.toUpperCase()}_arrow.png`);
const circleIcon = circleTemplate.replaceAll("FILL", `#${actualColor}`).replaceAll("BORDERS", feed.black && feed.black.includes(routeColor[1]) ? '#000000' : '#FFFFFF');
const circleBuffer = Buffer.from(circleIcon, 'utf8');
iconsRef.push(`${actualColor.toUpperCase()}_circle.png`);
/*
sharp(busBuffer)
.resize(64, 64)
.png()
.toFile(`./data/${feed.username}/icons/${actualColor.toUpperCase()}_bus.png`, (err, info) => {
if (err) throw err;
//console.log(`${routeColor[0]}_bus.png generated for ${feed.fullname}`)
});
*/
sharp(arrowBuffer)
.resize(120, 120)
.png()
.toFile(`./data/${feed.username}/icons/${actualColor.toUpperCase()}_arrow.png`, (err, info) => {
if (err) throw err;
//console.log(`${routeColor[0]}_bus.png generated for ${feed.fullname}`)
});
sharp(circleBuffer)
.resize(64, 64)
.png()
.toFile(`./data/${feed.username}/icons/${actualColor.toUpperCase()}_circle.png`, (err, info) => {
if (err) throw err;
//console.log(`${routeColor[0]}_bus.png generated for ${feed.fullname}`)
});
colorsList.push(`${actualColor}_${feed.black && feed.black.includes(routeColor[1]) ? '000000' : 'FFFFFF'}`);
});
fs.writeFileSync(`./data/${feed.username}/icons.json`, JSON.stringify(iconsRef));
imagesList.push(...iconsRef.map((n) => {
return `${feed.username}/icons/${n}`
}));
const rawLines = Object.keys(stops.routePoints).map((routeKey) => {
const route = routes.find((route) => route.myid === routeKey);
if (!route) return {
type: 'feature',
properties: {
routeColor: '#FFFFFF',
routeType: '3',
},
geometry: {
type: 'MultiLineString',
coordinates: [],
}
}
let routeColor = route.color.replace('#', '');
if (globalConfig.colorReplacements[routeColor]) routeColor = globalConfig.colorReplacements[routeColor];
//console.log(stops.routePoints[routeKey])
const routeLines = stops.routePoints[routeKey].map((routeLine) => {
const routePoints = routeLine.map((routePoint) => {
if (!routePoint) return null;
return [Number(routePoint.lng), Number(routePoint.lat)];
});
const filteredRoutePoints = routePoints.filter((routePoint) => routePoint !== null);
if (filteredRoutePoints.length < 2) return null;
/*
const simplifiedRoutePoints = simplify({
type: 'LineString',
coordinates: filteredRoutePoints.filter((routePoint) => routePoint !== null),
}, {
tolerance: 0.00001,
highQuality: true,
});
*/
return filteredRoutePoints.filter((routePoint) => routePoint !== null);
//console.log(simplifiedRoutePoints);
return simplifiedRoutePoints.coordinates;
});
return {
type: 'Feature',
properties: {
routeID: feed.combineBasedOnName ? (route.shortName.length > 0 ? route.shortName : route.nameOrig) : route.myid,
routeShortName: route.shortName ?? '',
routeLongName: route.nameOrig,
routeColor: `#${routeColor === '000000' ? 'FFFFFF' : (routeColor === 'FFFFFF' ? '000000' : routeColor)}`,
minZoom: 8,
},
geometry: {
type: 'MultiLineString',
coordinates: routeLines.filter((routeLine) => routeLine !== null),
},
};
})
fs.writeFileSync(`./data/${feed.username}/shapes.json`, JSON.stringify({
type: 'FeatureCollection',
features: rawLines.filter((rawLine) => rawLine !== null),
}));
shapesList.push(`${feed.username}/shapes.json`);
};
const updateFeeds = async () => {
const onlyThese = ['rutgers', 'chicago', 'gcsu', 'georgiast', 'gatech', 'GASO', 'mit', 'newyork', 'uncc', 'uncg', 'uncw', 'bamabama', 'ncstateuni'];
for (let i = 0; i < feeds.length; i++) {
let feed = feeds[i];
if (extraConfig[feed.username]) {
feed = {
...feed,
...extraConfig[feed.username],
};
}
//if (!onlyThese.includes(feed.username)) continue;
//if (feed.username !== 'unm') continue;
await updateFeed(feed);
}
fs.writeFileSync('./allIcons.json', JSON.stringify(imagesList));
fs.writeFileSync('./allShapes.json', JSON.stringify(shapesList));
fs.writeFileSync('./colorsList.json', JSON.stringify([...new Set(colorsList.sort())]));
};
if (fs.existsSync('./data')) {
fs.rmSync('./data', { recursive: true });
}
fs.mkdirSync('./data');
updateFeeds();