From 5d174d2f194c6145e294e7e9de7fd518e977deac Mon Sep 17 00:00:00 2001 From: Jan Andrle Date: Mon, 8 Jul 2024 10:33:33 +0200 Subject: [PATCH] :zap: Now cli is able to update the last drop articles --- cli.mjs | 26 +++++++++++++++++++++++--- package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/cli.mjs b/cli.mjs index 9db102e..7db3bc2 100755 --- a/cli.mjs +++ b/cli.mjs @@ -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} + * */ +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; diff --git a/package-lock.json b/package-lock.json index 4705ecb..ae4d39b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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" diff --git a/package.json b/package.json index 4cffb0e..dfd1470 100644 --- a/package.json +++ b/package.json @@ -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": {