hs2-last-updated: woohoo! it works i think!
This commit is contained in:
parent
03a1d1f5e4
commit
14f902c1d7
3 changed files with 147 additions and 1 deletions
31
deno.lock
31
deno.lock
|
@ -159,7 +159,8 @@
|
|||
},
|
||||
"redirects": {
|
||||
"https://deno.land/x/canvas/mod.ts": "https://deno.land/x/canvas@v1.4.1/mod.ts",
|
||||
"https://deno.land/x/emit/mod.ts": "https://deno.land/x/emit@0.28.0/mod.ts"
|
||||
"https://deno.land/x/emit/mod.ts": "https://deno.land/x/emit@0.28.0/mod.ts",
|
||||
"https://deno.land/x/font_dir/mod.ts": "https://deno.land/x/font_dir@v0.1.0/mod.ts"
|
||||
},
|
||||
"remote": {
|
||||
"https://deno.land/std@0.106.0/encoding/base64.ts": "eecae390f1f1d1cae6f6c6d732ede5276bf4b9cd29b1d281678c054dc5cc009e",
|
||||
|
@ -449,6 +450,34 @@
|
|||
"https://deno.land/x/emit@0.28.0/_utils.ts": "98412edc7aa29e77d592b54fbad00bdec1b05d0c25eb772a5f8edc9813e08d88",
|
||||
"https://deno.land/x/emit@0.28.0/emit.generated.js": "6ca765a2a7a1c2dab97e747981e6a9fb98a5acd7a0aa78a003e596fdd0632480",
|
||||
"https://deno.land/x/emit@0.28.0/mod.ts": "817cb45fcd94d15a43de826161049eee89e3be4bcf06b3a2f60e8cf459e559f1",
|
||||
"https://deno.land/x/font_dir@v0.1.0/mod.ts": "2b26db64f0ffb29d9d446796fdac3e5c964921a1367950541d1ad83ef283cd7f",
|
||||
"https://deno.land/x/imagescript@1.2.15/ImageScript.js": "9ce10c37a0e4fe43699689088c24f386fd401874057932bd57079ac729205038",
|
||||
"https://deno.land/x/imagescript@1.2.15/png/src/crc.mjs": "5cf50de181d61dd00e66a240d811018ba5070afa8bba302f393604404604de84",
|
||||
"https://deno.land/x/imagescript@1.2.15/png/src/mem.mjs": "4968d400dae069b4bf0ef4767c1802fd2cc7d15d90eda4cfadf5b4cd19b96c6d",
|
||||
"https://deno.land/x/imagescript@1.2.15/png/src/png.mjs": "96ef0ceff1b5a6cd9304749e5f187b4ab238509fb5f9a8be8ee934240271ed8d",
|
||||
"https://deno.land/x/imagescript@1.2.15/png/src/zlib.mjs": "9867dc3fab1d31b664f9344b0d7e977f493d9c912a76c760d012ed2b89f7061c",
|
||||
"https://deno.land/x/imagescript@1.2.15/utils/buffer.js": "952cb1beb8827e50a493a5d1f29a4845e8c648789406d389dd51f51205ba02d8",
|
||||
"https://deno.land/x/imagescript@1.2.15/utils/crc32.js": "573d6222b3605890714ebc374e687ec2aa3e9a949223ea199483e47ca4864f7d",
|
||||
"https://deno.land/x/imagescript@1.2.15/utils/png.js": "fbed9117e0a70602645d70df9c103ff6e79c03e987bd5c1685dcb4200729b6de",
|
||||
"https://deno.land/x/imagescript@1.2.15/utils/wasm/font.js": "9e75d842608c057045698d6a7cdf5ffd27241b5cdea0391c89a1917b31294524",
|
||||
"https://deno.land/x/imagescript@1.2.15/utils/wasm/gif.js": "8b86f7b96486bb8ff50fbc7c7487f86cb5cef85e6acd71e1def78a1aa2f12e4f",
|
||||
"https://deno.land/x/imagescript@1.2.15/utils/wasm/jpeg.js": "75295e2fcf96b4f7bb894b3844fdaa8140d63169d28b466b5d5be89d59a7b6e6",
|
||||
"https://deno.land/x/imagescript@1.2.15/utils/wasm/png.js": "0659536a8dd8f892c8346e268b2754b4414fad0ec1e9794dfcde1ba1c804ee02",
|
||||
"https://deno.land/x/imagescript@1.2.15/utils/wasm/svg.js": "f5c8a9d1977b51a7c07549ceb6bbbaca9497321a193f28b3dc229a42d91bcf14",
|
||||
"https://deno.land/x/imagescript@1.2.15/utils/wasm/tiff.js": "c2d7bdaef094df25aae1752e75167f485e89275d76a1379e39d8949580b7af4f",
|
||||
"https://deno.land/x/imagescript@1.2.15/utils/wasm/zlib.js": "749875f83abffe24d3b977475a0cbd5f9b52bee1fbdbef61ec183cbfc17805f6",
|
||||
"https://deno.land/x/imagescript@1.2.15/v2/framebuffer.mjs": "add44ff184636659714b3c6d4b896f628545451abffbc30b5bcc2e8d9a73d012",
|
||||
"https://deno.land/x/imagescript@1.2.15/v2/ops/blur.mjs": "80716f1ffab8a2aeb54a036f583bf51a2b9dd37e005adc000add803df8e8a12f",
|
||||
"https://deno.land/x/imagescript@1.2.15/v2/ops/color.mjs": "5e72cdcbf97dc939a2795223f01e3cb0544c0c56b03ea2aa026050df58348814",
|
||||
"https://deno.land/x/imagescript@1.2.15/v2/ops/crop.mjs": "69431fa6f687fd9f0c31eff0ec27d7ac925275005e53a37f0c3fab4cc4d9a9ea",
|
||||
"https://deno.land/x/imagescript@1.2.15/v2/ops/fill.mjs": "cf1b9488314753fbc9ebf03410ac74c2a34ea5a69fb6892cd6e8366cd1930d93",
|
||||
"https://deno.land/x/imagescript@1.2.15/v2/ops/flip.mjs": "825a34a66567dcf15e76a719f1bf2f66fb106503cd69942292b1b0ae05c5718e",
|
||||
"https://deno.land/x/imagescript@1.2.15/v2/ops/index.mjs": "423ba687119be2bba8cec72890577d3afa3621b6b8108912242fe937a183f2aa",
|
||||
"https://deno.land/x/imagescript@1.2.15/v2/ops/iterator.mjs": "c2adf3d90ce00719a02c48c97634574176a3501ff026676259bd71aa8f5d69b9",
|
||||
"https://deno.land/x/imagescript@1.2.15/v2/ops/overlay.mjs": "7e6e2c2ffd25006d52597ab8babc5f8f503d388a3fdf2fbc0eaea02799a020c9",
|
||||
"https://deno.land/x/imagescript@1.2.15/v2/ops/resize.mjs": "814e78ebce8eaf8f1f918688db7b52a141405e06a36ed4b25d04413d69e7d17b",
|
||||
"https://deno.land/x/imagescript@1.2.15/v2/ops/rotate.mjs": "a1b65616717bd2eed8db406affea3263b4674dada46b56441ef38167a187455d",
|
||||
"https://deno.land/x/imagescript@1.2.15/v2/util/mem.mjs": "4968d400dae069b4bf0ef4767c1802fd2cc7d15d90eda4cfadf5b4cd19b96c6d",
|
||||
"https://deno.land/x/media_types@v2.9.0/db.ts": "ba39cddbcefce47d577c0529066787a3a7b39d27750a6d32b5ad53ed487e7b7b",
|
||||
"https://deno.land/x/media_types@v2.9.0/deps.ts": "364b24c35845cfd5c6903ab22b8ba9873bf1022bbbf6bf3d001695332d4bbb4f",
|
||||
"https://deno.land/x/media_types@v2.9.0/mod.ts": "d63583b978d32eff8b76e1ae5d83cba2fb27baa90cc1bcb0ad15a06122ea8c19",
|
||||
|
|
117
serverside/hs2-last-updated.ts
Normal file
117
serverside/hs2-last-updated.ts
Normal file
|
@ -0,0 +1,117 @@
|
|||
// once a minute (what's the point if it's out of date!)
|
||||
// do days since last update (0 will be obvious then), should (please please please let this be true) only need three digits MAX
|
||||
// until hs2 ends of course but let's not think about that now
|
||||
// okay aaaa hs2 will end that will be sad and good i guess but
|
||||
// not important right now
|
||||
|
||||
import { contentType } from "https://deno.land/std@0.202.0/media_types/mod.ts";
|
||||
import {
|
||||
DOMParser,
|
||||
NodeType,
|
||||
} from "https://deno.land/x/deno_dom@v0.1.38/deno-dom-wasm.ts";
|
||||
import {
|
||||
Image,
|
||||
TextLayout,
|
||||
} from "https://deno.land/x/imagescript@1.2.15/ImageScript.js";
|
||||
|
||||
// not sure how worthwhile this optimization is but
|
||||
const dom_parser = new DOMParser();
|
||||
|
||||
async function get_last_update_date(): Promise<Date | string> {
|
||||
try {
|
||||
const res = await fetch("https://homestuck2.com/log");
|
||||
const body = await res.text();
|
||||
if (!body) return "couldn't get a string body";
|
||||
|
||||
// could just regex for the date lol
|
||||
const doc = dom_parser.parseFromString(body, "text/html");
|
||||
if (!doc) return "couldn't parse the body into the DOM";
|
||||
|
||||
const p = doc.querySelector("p");
|
||||
if (!p) return "couldn't get a p from the doc";
|
||||
|
||||
// should really enable strict indexing
|
||||
const us_date_node = p.childNodes[0];
|
||||
if (!us_date_node || us_date_node.nodeType != NodeType.TEXT_NODE) {
|
||||
return "couldn't get a date node from the log entry";
|
||||
}
|
||||
|
||||
const us_date = us_date_node.textContent.replace(" - ", "");
|
||||
// apparently doing new Date("10/8/2023") is implementation defined which is icky but it works with deno
|
||||
const date = new Date(us_date);
|
||||
if (Number.isNaN(date.valueOf())) {
|
||||
return `got an invalid date :(. the text_content was '${us_date}'`;
|
||||
}
|
||||
return date;
|
||||
} catch (e) {
|
||||
return `caught error: ${e}`;
|
||||
}
|
||||
}
|
||||
|
||||
async function check_again() {
|
||||
last_checked = new Date();
|
||||
|
||||
const new_date = await get_last_update_date();
|
||||
if (typeof new_date == "string") {
|
||||
console.log(`failed to get a date when checking 😬: '${new_date}'`);
|
||||
console.log(
|
||||
"^ ironically enough, this is more likely when there *is* an update",
|
||||
);
|
||||
console.log(
|
||||
"^ unironically, this is *most* likely the result of a bug",
|
||||
);
|
||||
return;
|
||||
}
|
||||
last_updated = new_date;
|
||||
}
|
||||
|
||||
function days_since_update(): number {
|
||||
const millis = new Date().getTime() - last_updated.getTime();
|
||||
return millis / (24 * 60 * 60 * 1000);
|
||||
}
|
||||
|
||||
// silly, i know
|
||||
let last_updated: Date = new Date(0);
|
||||
let last_checked: Date = new Date();
|
||||
await check_again();
|
||||
|
||||
// *definitely* a worthwhile optimization
|
||||
// (still inefficient to go over http...)
|
||||
const font = await fetch("https://static.pyrope.net/courier-std-bold.otf")
|
||||
.then((r) => r.arrayBuffer())
|
||||
.then((b) => new Uint8Array(b));
|
||||
|
||||
function make_image(): Image {
|
||||
const days = Math.floor(days_since_update()).toString().padStart(3, "0");
|
||||
console.log(days_since_update());
|
||||
const image = new Image(106, 44);
|
||||
const text = Image.renderText(
|
||||
font,
|
||||
64,
|
||||
days,
|
||||
0x00_00_00_ff,
|
||||
new TextLayout({ verticalAlign: "center", horizontalAlign: "middle" }),
|
||||
);
|
||||
|
||||
return image.composite(text, -5);
|
||||
}
|
||||
|
||||
async function update_image() {
|
||||
image = await make_image().encode();
|
||||
}
|
||||
|
||||
let image: Uint8Array = new Uint8Array();
|
||||
await update_image();
|
||||
|
||||
Deno.serve({ port: 61264 }, async () => {
|
||||
if (new Date().getTime() - last_checked.getTime() >= 60 * 1000) {
|
||||
await check_again();
|
||||
await update_image();
|
||||
}
|
||||
|
||||
return new Response(image, {
|
||||
headers: { "Content-Type": contentType("png") },
|
||||
});
|
||||
});
|
||||
|
||||
await Deno.writeFile("/tmp/poop.png", image);
|
BIN
static/courier-std-bold.otf
Normal file
BIN
static/courier-std-bold.otf
Normal file
Binary file not shown.
Loading…
Reference in a new issue