Skip to content

Commit

Permalink
added total downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Niceygy committed Jan 18, 2024
1 parent 2f0ebe5 commit dabd522
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"configurations": [
{
"name": "Miniflare",
"type": "node",
"request": "attach",
"port": 9229,
"cwd": "/",
"resolveSourceMapLocations": null,
"attachExistingChildren": false,
"autoAttachChildProcesses": false,
}
]
}
23 changes: 16 additions & 7 deletions src/worker.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
async function cron(env) {
async function cron(env, sendMsg) {
console.log("Cron func is running...");
const KV = env.KV;
const webhookURL = env.url;
var repos = [];

const list = await KV.list();
console.log("Key list aquired");
var total = 0;
for (const { name, expiration } of list.keys) {
console.log("Getting value for " + name);
const value = await KV.get(name);
if (name == "total" | name == undefined | name == "undefined") {
console.log("Invalid Repo, skipping");
} else {
repos.push({ name: name, count: parseInt(value) });
total += parseInt(value);
}
}
repos.sort((a, b) => b.count - a.count); // Sort repos by count in descending order
let currentDate = new Date();

var message = "# Downloads as of " + currentDate.toUTCString() + "\n\n" + repos.map(repo => `Repo: ${repo.name}, Count: ${repo.count}\n`).join("");
var message = message + "\n\nTotal downloads: " + total;
console.log("Message generated. Sending to Discord...");
console.log(message);

if (sendMsg == false) {
await fetch(webhookURL, {
method: "POST",
headers: {
Expand All @@ -33,7 +38,11 @@ async function cron(env) {
.then((response) => console.log(response))
.catch((error) => console.error(error));
console.log("Message sent to Discord");
return "complete\n";
return "Complete! Message sent.";
} else {
console.log("Returning mesage");
return message;
}
}


Expand All @@ -42,13 +51,13 @@ export default {
console.log("Worker is running...");
console.log("Processing request for " + request.url)
const url = request.url;
if (url.includes("testCron")) { console.log("testing cron func"); return new Response(await cron(env));}
if (url.includes("testCron")) { console.log("testing cron func"); return new Response(await cron(env, true));}
if (url.includes("favicon.ico")) {
return new Response(null, { status: 404 });
}
if (url.length < 1 | url == undefined | url == "http://127.0.0.1:8787/") {
if (url.length < 1 | url == undefined | url == "http://127.0.0.1:8787/") { //for local testing
console.log("Invalid request, redirecting to 404")
return await response.redirect(
return await Response.redirect(
"https://thealiendoctor.com/404", 301
);
}
Expand All @@ -59,7 +68,7 @@ export default {
var packName = packNameRaw.split("?file=")[0]; //e.g niceygylive/example
if (packName == undefined | fileName == undefined) {
console.log("Invalid request, redirecting to 404")
return await response.redirect(
return await Response.redirect(
"https://thealiendoctor.com/404", 301
);

Expand Down Expand Up @@ -94,6 +103,6 @@ export default {
);
},
async scheduled(event, env, ctx) {
ctx.waitUntil(cron(env));
ctx.waitUntil(cron(env, false));
},
};

0 comments on commit dabd522

Please sign in to comment.