Repository navigation
Expand file tree
/
Copy pathapi.js
More file actions
980 lines (893 loc) · 36.8 KB
/
Copy pathapi.js
File metadata and controls
980 lines (893 loc) · 36.8 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
const fetch = require("node-fetch");
const uap = require("ua-parser-js");
const HTMLParser = require("node-html-parser");
const path = require("path");
import { put, head } from "@vercel/blob";
import { time } from "console";
const fs = require("fs");
const navbarCode = fs.readFileSync(path.join(__dirname, "api", "navbar.txt"), "utf8");
const flagCodes = JSON.parse(fs.readFileSync(path.join(__dirname, "api", "flagcodes.json"), "utf8"));
const locations = JSON.parse(fs.readFileSync(path.join(__dirname, "api", "locations.json"), "utf8"));
// import { createClient } from "@supabase/supabase-js";
// const supabase = createClient(process.env.SUPA_URL, process.env.SUPA_SERVICEROLEKEY);
// const Database = require("easy-json-database");
// const projectDB = new Database("./user-project-database.json", {
// snapshots: {
// enabled: false,
// },
// });
// const projectInfoDB = new Database("./project-database.json", {
// snapshots: {
// enabled: false,
// },
// });
let api = {};
async function fetch2(url) {
let proxyURL = "https://cors.ogk.workers.dev/?";
let proxyURL2 = "https://renderapi.quuq.dev/proxy";
// if (url.startsWith("https://api.scratch.mit.edu")) {
let thing = await fetch(proxyURL + encodeURIComponent(encodeURI(url)));
if ((!thing.ok && thing.status != 404) || thing.redirected) {
console.log("scratch is 429, trying to use proxy");
thing = await fetch(proxyURL2, {
headers: {
"X-URL": url,
},
method: "POST",
});
}
return thing;
// } else {
// return await fetch(url);
// }
}
api.followering = async function (request, reply) {
let data = {
followers: "?",
following: "?",
};
let user = request.params.username;
if (user.endsWith("*")) {
user = user.slice(0, user.length - 1);
}
try {
// Get followers/following
let followingHTML = await (await fetch2(`https://scratch.mit.edu/users/${user}/following`)).text();
let followersHTML = await (await fetch2(`https://scratch.mit.edu/users/${user}/followers`)).text();
let root, text;
root = HTMLParser.parse(followingHTML);
text = root.querySelector(".box-head h2").childNodes[1].textContent;
// let dom1 = new JSDOM(followingHTML);
// let text = dom1.window.document.querySelector(".box-head h2").textContent;
data.following = parseInt(text.slice(text.indexOf("(") + 1, text.indexOf(")")));
root = HTMLParser.parse(followersHTML);
text = root.querySelector(".box-head h2").childNodes[1].textContent;
// let dom2 = new JSDOM(followersHTML);
// let text2 = dom2.window.document.querySelector(".box-head h2").textContent;
data.followers = parseInt(text.slice(text.indexOf("(") + 1, text.indexOf(")")));
} catch {}
reply.headers({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
"Cache-Control": "max-age=3600",
});
reply.code(200).header("Content-Type", "application/json; charset=utf-8").send(JSON.stringify(data, null, 4));
};
async function getStats(username) {
let user = username;
let data = {
totalViews: "?",
totalLoves: "?",
totalFaves: "?",
loveToViewRatio: "?",
faveToViewRatio: "?",
faveToLoveRatio: "?",
averageStats: {
averageViews: "?",
averageLoves: "?",
averageFaves: "?",
},
};
let page = 0;
let projects = [];
while (true) {
let url = `https://api.scratch.mit.edu/users/${user}/projects?limit=40&offset=${page * 40}`;
console.log(url);
let r = await (await fetch2(url)).json();
projects.push(r);
if (Object.keys(r).length < 40 || page > 24) {
break;
}
page++;
}
projects = projects.flat(1);
let projectCount = projects.length;
let totalLoves = 0,
totalFaves = 0,
totalViews = 0;
for (let project of projects) {
totalViews += project.stats.views;
totalLoves += project.stats.loves;
totalFaves += project.stats.favorites;
}
console.log("total views, loves, faves", totalViews, totalLoves, totalFaves);
data.totalViews = totalViews;
data.totalLoves = totalLoves;
data.totalFaves = totalFaves;
data.loveToViewRatio = (totalLoves / totalViews) * 100 || 0;
data.faveToViewRatio = (totalFaves / totalViews) * 100 || 0;
data.faveToLoveRatio = (totalFaves / totalLoves) * 100 || 0;
data.averageStats.averageViews = Math.round(totalViews / projectCount) || 0;
data.averageStats.averageLoves = Math.round(totalLoves / projectCount) || 0;
data.averageStats.averageFaves = Math.round(totalFaves / projectCount) || 0;
let best = projects.map((a) => ({
loves: a.stats.loves,
faves: a.stats.favorites,
views: a.stats.views,
loveToViewRatio: (a.stats.loves / a.stats.views) * 100,
faveToViewRatio: (a.stats.favorites / a.stats.views) * 100,
faveToLoveRatio: (a.stats.favorites / a.stats.loves) * 100 || 0,
// remixCount: a.stats.remixes,
id: a.id,
title: a.title,
// img: a.image,
img: `https://uploads.scratch.mit.edu/get_image/project/${a.id}_230x500.png`,
time: new Date(a.history.shared).getTime(),
}));
best.sort((a, b) => b.loves + b.faves - (a.loves + a.faves));
data.projects = best;
return data;
}
api.projectStats = async function (request, reply) {
let user = request.params.username;
reply.headers({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
"Cache-Control": "max-age=3600",
});
reply
.code(200)
.header("Content-Type", "application/json; charset=utf-8")
.send(JSON.stringify(await getStats(user), null, 4));
};
async function getUserData(username, mode) {
let params = {
username: username,
scratchteam: "?",
id: "?",
browser: null,
os: null,
browserOStimeAgo: null,
joinDate: "?",
country: "?",
profilePicture: "",
projectsShared: "?",
followers: "?",
following: "?",
deleted: "?",
};
params.nav = navbarCode;
let user = params.username;
// console.log("extra", extra)
if (mode == "regular" || mode == "all") {
let userInfo = await (await fetch2(`https://api.scratch.mit.edu/users/${user}`)).json();
if (userInfo.code) {
if (userInfo.code.includes("NotFound")) {
return 404;
}
}
params.id = userInfo.id;
params.joinDate = new Date(userInfo.history.joined).toLocaleString();
params.profilePicture = userInfo.profile.images["60x60"];
params.country = userInfo.profile.country;
params.username = userInfo.username;
params.scratchteam = userInfo.scratchteam;
}
if (mode == "extra" || mode == "all") {
let projects = await (await fetch2(`https://api.scratch.mit.edu/users/${user}/projects`)).json();
uaIf: if (Object.keys(projects).length == 0 || projects.code == "ResourceNotFound" || projects.code == "NotFound") {
break uaIf;
} else {
let firstProject = Object.values(projects)[0].id;
let projectData = await (await fetch2(`https://api.scratch.mit.edu/projects/${firstProject}`)).json();
let projectDate = new Date(projectData.history.modified).getTime();
let difference = Math.round((Date.now() - projectDate) / (1000 * 3600 * 24));
params.browserOStimeAgo = difference;
try {
let agent = (await (await fetch2(`https://projects.scratch.mit.edu/${firstProject}?token=${projectData.project_token}`)).json()).meta.agent;
let parsed = uap(agent);
params.browser = `${parsed.browser.name} ${parsed.browser.version || ""}`.trim();
if (parsed.os.name == "Windows" && parsed.os.version == "10") {
if (parsed.browser.name.includes("Chrome") && parsed.browser.version.replaceAll(".0", "") < 110) {
parsed.os.version = "7-11";
} else {
parsed.os.version = "10/11";
}
}
params.os = `${parsed.os.name} ${parsed.os.version || ""}`.trim();
} catch {}
}
try {
let root, text;
let response2 = await fetch2(`https://scratch.mit.edu/users/${user}/projects`);
if (response2.status == 404) {
params.deleted = true;
} else {
params.deleted = false;
}
root = HTMLParser.parse(await response2.text());
text = root.querySelector(".box-head h2").childNodes[1].textContent;
params.projectsShared = parseInt(text.slice(text.indexOf("(") + 1, text.indexOf(")")));
} catch (e) {
console.log("failed to get users projects");
}
}
if (mode == "followering" || mode == "all") {
try {
// Get followers/following
let followingHTML = await (await fetch2(`https://scratch.mit.edu/users/${user}/following`)).text();
let followersHTML = await (await fetch2(`https://scratch.mit.edu/users/${user}/followers`)).text();
let root, text;
root = HTMLParser.parse(followingHTML);
text = root.querySelector(".box-head h2").childNodes[1].textContent;
// let dom1 = new JSDOM(followingHTML);
// let text = dom1.window.document.querySelector(".box-head h2").textContent;
params.following = parseInt(text.slice(text.indexOf("(") + 1, text.indexOf(")")));
root = HTMLParser.parse(followersHTML);
text = root.querySelector(".box-head h2").childNodes[1].textContent;
// let dom2 = new JSDOM(followersHTML);
// let text2 = dom2.window.document.querySelector(".box-head h2").textContent;
params.followers = parseInt(text.slice(text.indexOf("(") + 1, text.indexOf(")")));
} catch {}
}
let newParams = {};
for (let param of Object.keys(params)) {
if (params[param] != "?" && params[param] != "" && params[param] != undefined) {
newParams[param] = params[param];
}
}
return newParams;
}
api.getUser = async function (request, reply) {
let params = {
username: request.params.username,
usernameAsterisk: request.params.username,
browser: "Loading...",
os: "Loading...",
browserOStimeAgo: "Loading...",
id: "Loading...",
joinDate: "?",
country: "?",
followers: "Loading...",
following: "Loading...",
profilePicture: "",
stats: {},
projectsShared: "Loading...",
};
params.nav = navbarCode;
let user = request.params.username;
let data = await getUserData(user, "regular");
if (data == 404) {
return reply.view("/userNotFound.hbs", params);
}
params.username = data.username;
params.browser = data.browser;
params.os = data.os || "?";
params.id = data.id || "?";
params.browserOStimeAgo = data.browserOStimeAgo > 1 ? `(as of ${data.browserOStimeAgo} days ago)` : "";
params.joinDate = data.joinDate;
params.country = data.country;
params.projectsShared = data.projectsShared;
params.profilePicture = data.profilePicture;
params.usernameAsterisk = data.username + (data.scratchteam ? "*" : "");
params.smallLoading = `<span class="img-load-wrapper" style="width: 70px;height: 20px;display: inline-block;margin-bottom: -4px;"><span class="activity"></span></span>`;
params.mediumLoading = `<span class="img-load-wrapper" style="width: 75px;height: 20px;display: inline-block;margin-bottom: -4px;"><span class="activity"></span></span>`;
// let user = params.username;
// let userInfo = await (await fetch2(`https://api.scratch.mit.edu/users/${user}`)).json();
// if (userInfo.code) {
// if (userInfo.code.includes("NotFound")) {
// return reply.view("/src/pages/UserNotFound.hbs", { username: user });
// }
// }
// params.id = userInfo.id;
// params.joinDate = new Date(userInfo.history.joined).toLocaleString();
// params.profilePicture = userInfo.profile.images["60x60"];
// params.country = userInfo.profile.country;
// params.username = userInfo.username + (userInfo.scratchteam ? "*" : "");
// let projects = await (await fetch2(`https://api.scratch.mit.edu/users/${user}/projects`)).json();
// uaIf: if (Object.keys(projects).length == 0) {
// break uaIf;
// } else {
// let firstProject = Object.values(projects)[0].id;
// let projectData = await (await fetch2(`https://api.scratch.mit.edu/projects/${firstProject}`)).json();
// let projectDate = new Date(projectData.history.modified).getTime();
// let difference = Math.round((Date.now() - projectDate) / (1000 * 3600 * 24));
// if (difference > 1) {
// params.browserOStimeAgo = `(as of ${difference} days ago)`;
// }
// try {
// let agent = (await (await fetch2(`https://projects.scratch.mit.edu/${firstProject}?token=${projectData.project_token}`)).json()).meta.agent;
// let parsed = uap(agent);
// params.browser = `${parsed.browser.name} ${parsed.browser.version}`;
// if (parsed.os.name == "Windows" && parsed.os.version == "10") {
// parsed.os.version = "10/11";
// }
// params.os = `${parsed.os.name} ${parsed.os.version}`;
// } catch {}
// }
// // project stats
// // let stats = await getStats(user);
// // stats.loveToViewRatio = `${Math.round(stats.loveToViewRatio * 100) / 100}%`;
// // params.stats = stats;
// // projects shared
// try {
// let root, text;
// root = HTMLParser.parse(await (await fetch2(`https://scratch.mit.edu/users/${user}/projects`)).text());
// text = root.querySelector(".box-head h2").childNodes[1].textContent;
// params.projectsShared = text.slice(text.indexOf("(") + 1, text.indexOf(")"));
// } catch {}
return reply.view("/user.hbs", params);
};
api.getUserInfo = async function (request, reply) {
reply.headers({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
"Cache-Control": "max-age=3600",
});
let code = 200;
let data;
if (request.query.mode) {
// console.log("extra")
data = await getUserData(request.params.username, request.query.mode);
} else {
// console.log("regular")
// data = await getUserData(request.params.username, false)
data = { error: "must specify 'mode' parameter" };
code = 400;
}
delete data.nav;
reply.headers({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
"Cache-Control": "max-age=3600",
});
reply.code(code).header("Content-Type", "application/json; charset=utf-8").send(JSON.stringify(data, null, 4));
};
api.browserHistoryPage = async function (request, reply) {
// console.log(request.params);
let params = {
username: request.params.username,
// page: request.query.page || 1,
// nextPage: parseInt(request.query.page || 1) + 1,
};
params.nav = navbarCode;
// params.prevPage = params.page > 1 ? params.page - 1 : 1;
return reply.view("/browserHistory.hbs", params);
};
api.browserHistory = async function (request, reply) {
let user = request.params.username.toString();
let data = [];
let projects;
let blobDetails = await head("projectData.json");
let projectInfoDB = await (await fetch(blobDetails.url + `?nocache=${Math.random()}&download=1`)).json();
let blobDetails2 = await head("userProjectList.json");
let userDB = await (await fetch(blobDetails2.url + `?nocache=${Math.random()}&download=1`)).json();
if (userDB[user]) {
// if (false) {
// let offset = request.query.page ? (request.query.page - 1) * 40 : 0;
// projects = projectDB.get(user).slice(offset, offset + 40);
projects = userDB[user];
console.log("using stored projects for user", user, projects);
} else {
let page = 0;
projects = [];
while (true) {
let url = `https://api.scratch.mit.edu/users/${user}/projects?limit=40&offset=${page * 40}`;
console.log(url);
let r = await (await fetch2(url)).json();
projects.push(r);
if (Object.keys(r).length < 40 || page > 15) {
break;
}
page++;
}
projects = projects.flat(1);
//console.log(projects[0], projects[1])
projects.sort((a, b) => new Date(b.history.modified).getTime() - new Date(a.history.modified).getTime());
let ids = projects.map((a) => a.id);
// projectDB.set(user, ids);
userDB[user] = ids;
console.log("created project list for user:", user);
// let offset = request.query.page ? (request.query.page - 1) * 40 : 0;
// projects = projectDB.get(user).slice(offset, offset + 40);
}
// console.log("using project list", projects)
let promises = [];
let projectInfo = [];
for (let i = 0; i < projects.length; i++) {
promises.push(
new Promise(async function (resolve, reject) {
let id = projects[i].id || projects[i];
let time, ua;
if (projectInfoDB[id.toString()]) {
// if (false) {
time = projectInfoDB[id.toString()].time;
ua = projectInfoDB[id.toString()].ua;
console.log("using stored data");
} else {
let info = await (await fetch2("https://api.scratch.mit.edu/projects/" + id)).json();
// console.log(info)
time = new Date(info.history.modified).getTime();
let token;
token = info.project_token;
try {
ua = (await (await fetch("https://projects.scratch.mit.edu/" + id + "?token=" + token)).json()).meta.agent;
} catch {
ua = "";
}
// console.log("setting id", id, time, ua)
projectInfoDB[id.toString()] = {
time: time,
ua: ua,
};
console.log("fetching new data");
}
projectInfo.push({ time: time, agent: ua, id: id });
resolve();
})
);
}
function compare(a, b) {
if (parseFloat(a.browser.split(" ")[1]) < parseFloat(b.browser.split(" ")[1])) {
return -1;
}
if (parseFloat(a.browser.split(" ")[1]) > parseFloat(b.browser.split(" ")[1])) {
return 1;
}
return 0;
}
await Promise.all(promises);
projectInfo.sort((a, b) => a.time - b.time);
for (let project of projectInfo) {
let parsed = uap(project.agent);
let browser = `${parsed.browser.name} ${parsed.browser.version}`;
if (parsed.os.name == "Windows" && parsed.os.version == "10") {
parsed.os.version = "10/11";
}
let os = `${parsed.os.name} ${parsed.os.version}`;
if (!browser.includes("undefined") && !os.includes("undefined")) {
data.push({ browser: browser, os: os, time: project.time, id: project.id });
}
}
console.log("done");
// console.log(projectInfoDB)
const blob = await put("projectData.json", JSON.stringify(projectInfoDB), {
access: "public",
allowOverwrite: true,
});
const blob2 = await put("userProjectList.json", JSON.stringify(userDB), {
access: "public",
allowOverwrite: true,
});
console.log("blob", blob.url, blob2.url);
//data.sort(compare)
reply.headers({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
"Cache-Control": "max-age=3600",
});
reply.code(200).header("Content-Type", "application/json; charset=utf-8").send(JSON.stringify(data, null, 4));
};
api.projectPage = async function getProjectPage(request, reply) {
let params = {};
let id = request.params.project;
params = await getProjectData(id);
if (params == 404) {
return reply.view("/projectNotFound.hbs", { id: id, nav: navbarCode });
} else {
params.loveToViewRatio = params.loveToViewRatio || 0;
params.faveToViewRatio = params.faveToViewRatio || 0;
params.faveToLoveRatio = params.faveToLoveRatio || 0;
return reply.view("/project.hbs", params);
}
};
api.apiProjectData = async function apiProjectData(request, reply) {
reply.headers({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
"Cache-Control": "max-age=3600",
});
let data = await getProjectData(request.params.id);
delete data.nav;
delete data.smallLoading;
delete data.mediumLoading;
reply.code(200).header("Content-Type", "application/json; charset=utf-8").send(JSON.stringify(data, null, 4));
};
async function getProjectData(id) {
if (!id) {
return {};
}
let params = {};
params.nav = navbarCode;
let basicInfo = await (await fetch2(`https://api.scratch.mit.edu/projects/${id}`)).json();
if (basicInfo.code == "NotFound" || basicInfo.code == "ResourceNotFound") {
return 404;
}
try {
params.id = parseInt(id);
} catch {
params.id = id;
}
params.title = basicInfo.title;
params.author = {};
params.author.username = basicInfo.author.username;
params.author.id = basicInfo.author.id;
params.author.image = basicInfo.author.profile.images["60x60"];
params.history = basicInfo.history;
params.stats = basicInfo.stats;
params.image = basicInfo.image;
params.loveToViewRatio = (params.stats.loves / params.stats.views) * 100;
params.faveToViewRatio = (params.stats.favorites / params.stats.views) * 100;
params.faveToLoveRatio = (params.stats.favorites / params.stats.loves) * 100;
params.smallLoading = `<span class="img-load-wrapper" style="width: 70px;height: 20px;display: inline-block;margin-bottom: -4px;"><span class="activity"></span></span>`;
params.mediumLoading = `<span class="img-load-wrapper" style="width: 75px;height: 20px;display: inline-block;margin-bottom: -4px;"><span class="activity"></span></span>`;
// let response = await fetch2("https://scratch.mit.edu/projects/" + id.toString() + "/remixtree/");
// let t = await response.text();
// let root = HTMLParser.parse(t);
// let els = root.getElementsByTagName("script");
// for (var i = 0; i < els.length; i++) {
// if (els[i].textContent.includes("var projectData = {")) {
// let el = els[i];
// if (el.textContent.includes("notsafe")) {
// params.reviewStatus = "notsafe";
// break;
// }
// if (el.textContent.includes("safe")) {
// params.reviewStatus = "safe";
// break;
// }
// if (el.innerHTML.includes("notreviewed")) {
// params.reviewStatus = "notreviewed";
// break;
// }
// break;
// }
// }
return params;
}
// module.exports = {
// followering: api.followering,
// projectStats: api.projectStats,
// getUser: api.getUser,
// browserHistoryPage: api.browserHistoryPage,
// browserHistory: api.browserHistory
// };
api.griffpatchFollowerCount = async function (request, reply) {
let blobDetails = await head("data.json");
let data = await (await fetch(blobDetails.url + `?nocache=${Math.random()}`)).json();
// return reply.send(data)
let min = data.lastCount;
let hours = Math.max(Math.abs(Date.now() - data.lastTime) / 36e5, 0.8);
console.log("Difference in hours: ", hours);
let estimatedNewFollowers = Math.ceil(data.perHour * hours);
console.log("Estimated new followers:", estimatedNewFollowers + 10);
let max = Math.ceil(min + estimatedNewFollowers);
console.log("Min", min, "Max", max);
let testId = min;
let result = [];
console.time("count");
// Do a binary search to find the follower count
let attempts = 0;
while (max - min > 1) {
if (attempts > 20) {
return reply.code(500).send("Error, attempt counter exceeded 20");
break;
}
let startTime = Date.now();
testId = Math.ceil((min + max) / 2);
result = await (await fetch2(`https://api.scratch.mit.edu/users/griffpatch/followers/?offset=${testId}&limit=1`)).json();
// console.log(result)
if (result.response == "Too many requests") {
console.log("oops", result);
return "sorry, the scratch api isn't working right now";
}
if (result == undefined) {
result = 0;
}
if (result.length == 0) {
max = testId;
} else {
min = testId;
}
let sleepTime = Math.max(400 - (Date.now() - startTime), 50);
console.log("test", testId, "max", max, "min", min, "sleeping", sleepTime);
await sleep(sleepTime);
attempts += 1;
}
console.timeEnd("count");
if (result.length == 0) {
testId -= 1;
}
console.log("final count: ", testId, "in", attempts, "attempts");
data.lastCount = testId;
data.lastTime = Date.now();
let griffyFollowerCount = testId;
const blob = await put("data.json", JSON.stringify(data, null, 4), {
access: "public",
allowOverwrite: true,
});
reply.headers({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
"Cache-Control": "max-age=3600",
});
reply.code(200).send(griffyFollowerCount);
};
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
api.page = function (page) {
return function (request, reply) {
let params = {};
params.nav = navbarCode;
return reply.view(`/${page}.hbs`, params);
};
};
api.rateLimit = function (max, timeWindow) {
return {
config: {
rateLimit: {
max: max,
timeWindow: timeWindow,
},
},
};
};
function randInt(min, max) {
// min and max included
return Math.floor(Math.random() * (max - min + 1) + min);
}
api.randomProject = async function (request, reply) {
reply.headers({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
});
while (true) {
let testId = randInt(104, 1200000000);
console.log("trying id", testId);
let response = await (await fetch2("https://api.scratch.mit.edu/projects/" + testId)).json();
if (!(response.code == "NotFound" || response.code == "ResourceNotFound")) {
console.log("found", testId);
reply.code(200).send(testId);
break;
}
}
};
api.randomUser = async function (request, reply) {
reply.headers({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
});
let blobDetails = await head("usernames.txt");
let max = 120031047;
let min = 100;
let num2 = randInt(min, max);
let num1 = num2 - min;
let response = (
await (
await fetch(blobDetails.url, {
method: "GET",
headers: {
Range: `bytes=${num1}-${num2}`,
},
})
).text()
).trim();
response = response.replaceAll("\r", "");
let trimmed = response.slice(response.indexOf("\n"), response.lastIndexOf("\n", response.length - 1)).trim();
let userList = trimmed.split("\n");
let randomUser = userList[randInt(0, userList.length - 1)];
console.log("from userlist", userList, "picking", randomUser);
reply.code(200).send(randomUser);
};
api.posts = async function (request, reply) {
return reply.view("/posts.hbs", { username: request.params.username, nav: navbarCode });
};
async function makeQuery(page, query) {
const url = `https://api.scratch.mit.edu/search/projects?limit=40&offset=${page * 40}&language=en&mode=popular&q=${encodeURIComponent(query.toLowerCase())}`;
// console.log(url)
return (await fetch2(url)).json();
}
async function getSearchStatus(id, name, username) {
const query = name;
const queryID = id;
let output = {};
output.status = null;
let page = 0;
while (page <= 60) {
console.log("Getting page", page);
let response = (await Promise.all([makeQuery(page, query), makeQuery(page + 1, query), makeQuery(page + 2, query), makeQuery(page + 3, query), makeQuery(page + 4, query)])).flat();
for (const project of response) {
if (project.id == queryID) {
console.log(project.stats);
console.log("project", id, name, "indexed");
output.status = "indexed";
output.stats = project.stats;
return output;
}
}
page += 5;
}
console.log("project", id, name, "not indexed with name, trying username");
page = 0;
while (page <= 25) {
console.log("Getting page", page);
// console.log("name", username)
let response = (await Promise.all([makeQuery(page, username), makeQuery(page + 1, username), makeQuery(page + 2, username), makeQuery(page + 3, username), makeQuery(page + 4, username)])).flat();
for (const project of response) {
if (project.id == queryID) {
console.log(project.stats);
console.log("project", id, name, "indexed");
output.status = "indexed";
output.stats = project.stats;
return output;
}
}
page += 5;
}
console.log("project", id, name, "not indexed");
output.status = "notindexed";
return output;
}
api.searchStatus = async function (request, reply) {
reply.headers({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
"Cache-Control": "max-age=3600",
});
let id = request.params.id;
if (!id) {
return reply.code(400).send("missing id");
}
let basicInfo = await (await fetch2(`https://api.scratch.mit.edu/projects/${id}`)).json();
if (basicInfo.code == "NotFound" || basicInfo.code == "ResourceNotFound") {
return reply.code(404).send("project not found");
}
try {
id = parseInt(id);
} catch {
id = id;
}
const title = basicInfo.title;
const username = basicInfo.author.username;
reply
.code(200)
.header("Content-Type", "application/json; charset=utf-8")
.send(JSON.stringify(await getSearchStatus(id, title, username), null, 4));
};
// async function getRankings(country, page) {
// if (country == "all") country = null;
// // console.log(await supabase.from("rankings").select("*").limit(5))
// const offset = page * 100;
// console.log("fetching rankings country:", country, "offset", offset);
// if (country) {
// console.time("fetch");
// const { data, error } = await supabase.rpc("get_ranking_location", { offset2: offset, location: country });
// console.log(data, error);
// // const { data, error } = await supabase
// // .from("rankings")
// // .select("*")
// // .eq("country", country)
// // .order("followers", { ascending: false })
// // .range(page * 100, page * 100 + 99);
// console.timeEnd("fetch");
// // console.time("count");
// // const { data2, count } = await supabase.from("rankings").select("*", { count: "planned" }).eq("country", country);
// // console.timeEnd("count");
// // console.log(data2, count);
// return data;
// } else {
// console.time("fetch");
// const { data, error } = await supabase.rpc("get_ranking", { offset2: offset });
// console.log(data, error);
// // const { data, error } = await supabase
// // .from("rankings")
// // .select("*")
// // .order("followers", { ascending: false })
// // .range(page * 100, page * 100 + 99);
// console.timeEnd("fetch");
// // console.time("count");
// // const { data2, count } = await supabase.from("rankings").select("*", { count: "planned" });
// // console.timeEnd("count");
// // console.log(data2, count);
// return data;
// }
// }
// async function getUserRankings(username, country) {
// console.log("ranking requested for", username);
// const included = await supabase.from("rankings").select("username,country").eq("username", username);
// if (!included.data.length) {
// return "ERROR: user not ranked";
// }
// const userCountry = included.data[0].country;
// // if (userCountry != country && country) {
// // return "ERROR: country mismatch";
// // }
// let data, error;
// if (country) {
// ({ data, error } = await supabase.rpc("get_user_ranking_country", { say: username, location: userCountry }));
// } else {
// ({ data, error } = await supabase.rpc("get_user_ranking", { say: username }));
// }
// console.log(data, error);
// data[0].ranking = data[0].row_number;
// if (userCountry) data[0].country = userCountry;
// delete data[0].row_number;
// console.log(data[0]);
// return data[0];
// }
// api.rankings = async function (request, reply) {
// reply.headers({
// "Access-Control-Allow-Origin": "*",
// "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
// "Cache-Control": "max-age=3600",
// });
// reply.code(200).send(await getRankings(request.query.country, request.query.page));
// };
// api.userRanking = async function (request, reply) {
// reply.headers({
// "Access-Control-Allow-Origin": "*",
// "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
// "Cache-Control": "max-age=3600",
// });
// reply.code(200).send(await getUserRankings(request.params.username, request.query.country));
// };
// api.rankingsPage = async function (request, reply) {
// let params = {};
// params.nav = navbarCode;
// const page = parseInt(request.query.page ?? 1) - 1;
// let country = request.query.location ?? null;
// let data = await getRankings(country, page);
// data = data || [];
// console.log(data);
// let i = page * 100 + 1;
// for (let user of data) {
// user.followers = user.followers.toLocaleString();
// user.index = i;
// if (user.country && flagCodes[user.country]) {
// user.countryName = user.country;
// user.country = flagCodes[user.country].toUpperCase();
// } else {
// user.country = "none";
// }
// i++;
// }
// params.users = data;
// params.page = page + 1;
// params.location = country || "none";
// params.locations = locations;
// // params.count = count
// return reply.view("/rankings.hbs", params);
// };
// api.rankingsCount = async function (request, reply) {
// // if (!request.query.country) {
// // return reply.code(400).send("Missing country")
// // }
// reply.headers({
// "Access-Control-Allow-Origin": "*",
// "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
// "Cache-Control": "max-age=604800",
// });
// let data, error;
// let country = request.query.country
// if (country == "all") country = null;
// if (!country) {
// ({ data, error } = await supabase.rpc("get_amount_nocountry"));
// } else {
// ({ data, error } = await supabase.rpc("get_amount", { location: country }));
// }
// const amount = data[0].count;
// console.log(amount, "in", country || "all");
// reply.code(200).send(amount);
// };
module.exports = api;