plc-replica/scrape.ts

38 lines
1,006 B
TypeScript

import { TextLineStream } from "jsr:@std/streams@1/text-line-stream";
import { ExportEntry } from "./directory-tailer.ts";
import { tailer } from "./main.ts";
export const catchUp = async () => {
using exports = await Deno.open("./data/exports.jsonl", { read: true });
await exports.seek(-4096, Deno.SeekMode.End); // grab last 4kib
const lineStream = exports.readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream());
// discard one partial line
{
const lineReader = lineStream.getReader();
const _line = await lineReader.read();
lineReader.releaseLock();
}
tailer.lastBatchCIDs.clear();
for await (const line of lineStream.values()) {
try {
const entry = JSON.parse(line) as unknown as ExportEntry;
tailer.latestDate = entry.createdAt;
tailer.lastBatchCIDs.add(entry.cid);
} catch (_err) {
// ignore
}
}
};
try {
await catchUp();
} catch (err) {
console.warn(err);
}
await tailer.fetchExports();