Now cli is able to update the last drop articles
All checks were successful
Update RSS / update-rss (push) Successful in 5m20s

This commit is contained in:
Jan Andrle 2024-07-08 10:33:33 +02:00
parent ce93c7241f
commit 5d174d2f19
Signed by: jaandrle
GPG Key ID: B3A25AED155AFFAB
3 changed files with 26 additions and 6 deletions

26
cli.mjs
View File

@ -96,28 +96,48 @@ async function sitemap(){
const json= s.test("-f", path) ? s.cat(path).xargs(JSON.parse) : { drops: [], articles: [] };
await syncDrops(json);
const [ { drop: drop_last } ]= json.drops;
const [ article_last= { drop: "" } ]= json.articles;
if(drop_last === article_last.drop) return { json, changed: [] };
const res= await fetch(url_drops+drop_last);
if(res.status !== 200) return { json, changed: [] };
const drop_articles= dropArticles(drop_last, json);
const dom= new JSDOM(await res.text());
const diff= [];
for(const article of dom.window.document.querySelectorAll("article")){
const loc= article.querySelector("a")?.href;
if(!loc){
echo("Article without link:", article.textContent);
continue;
}
if(drop_articles.has(loc)) continue;
diff.push({
title: article.querySelector("h2").textContent.trim(),
perex: article.querySelector("[class^=ArticleTile_perex]").textContent.trim(),
author: article.querySelector("[class^=ArticleTile_author]").textContent.trim(),
loc: article.querySelector("a").href,
loc,
drop: drop_last,
});
}
if(!diff.length) return { json, changed: [] };
json.articles.unshift(...diff);
s.echo(JSON.stringify(json, null, "\t")).to(path);
return { json, changed: [ path ] };
}
/**
* Assumes that articles are sorted from newest to oldest
* @param {Drop.drop} drop
* @param {Sitemap} json
* @returns {Set<Article.loc>}
* */
function dropArticles(drop, { articles }){
const out= new Set();
for(const article of articles){
if(article.drop !== drop) break;
out.add(article.loc);
}
return out;
}
/** @param {Sitemap} json */
async function syncDrops(json){
const [ { drop: drop_last } ]= json.drops;

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "pagenotfound-cli",
"version": "1.1.0",
"version": "1.2.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "pagenotfound-cli",
"version": "1.1.0",
"version": "1.2.0",
"license": "MIT",
"dependencies": {
"jsdom": "~24.1"

View File

@ -1,6 +1,6 @@
{
"name": "pagenotfound-cli",
"version": "1.1.1",
"version": "1.2.0",
"description": "Utility primary for generating RSS feed for Pagenotfound",
"bin": "cli.js",
"scripts": {