⚡ Now cli is able to update the last drop articles
All checks were successful
Update RSS / update-rss (push) Successful in 5m20s
All checks were successful
Update RSS / update-rss (push) Successful in 5m20s
This commit is contained in:
parent
ce93c7241f
commit
5d174d2f19
26
cli.mjs
26
cli.mjs
@ -96,28 +96,48 @@ async function sitemap(){
|
|||||||
const json= s.test("-f", path) ? s.cat(path).xargs(JSON.parse) : { drops: [], articles: [] };
|
const json= s.test("-f", path) ? s.cat(path).xargs(JSON.parse) : { drops: [], articles: [] };
|
||||||
await syncDrops(json);
|
await syncDrops(json);
|
||||||
const [ { drop: drop_last } ]= json.drops;
|
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);
|
const res= await fetch(url_drops+drop_last);
|
||||||
if(res.status !== 200) return { json, changed: [] };
|
if(res.status !== 200) return { json, changed: [] };
|
||||||
|
|
||||||
|
const drop_articles= dropArticles(drop_last, json);
|
||||||
const dom= new JSDOM(await res.text());
|
const dom= new JSDOM(await res.text());
|
||||||
const diff= [];
|
const diff= [];
|
||||||
for(const article of dom.window.document.querySelectorAll("article")){
|
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({
|
diff.push({
|
||||||
title: article.querySelector("h2").textContent.trim(),
|
title: article.querySelector("h2").textContent.trim(),
|
||||||
perex: article.querySelector("[class^=ArticleTile_perex]").textContent.trim(),
|
perex: article.querySelector("[class^=ArticleTile_perex]").textContent.trim(),
|
||||||
author: article.querySelector("[class^=ArticleTile_author]").textContent.trim(),
|
author: article.querySelector("[class^=ArticleTile_author]").textContent.trim(),
|
||||||
loc: article.querySelector("a").href,
|
loc,
|
||||||
drop: drop_last,
|
drop: drop_last,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if(!diff.length) return { json, changed: [] };
|
||||||
|
|
||||||
json.articles.unshift(...diff);
|
json.articles.unshift(...diff);
|
||||||
s.echo(JSON.stringify(json, null, "\t")).to(path);
|
s.echo(JSON.stringify(json, null, "\t")).to(path);
|
||||||
return { json, changed: [ 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 */
|
/** @param {Sitemap} json */
|
||||||
async function syncDrops(json){
|
async function syncDrops(json){
|
||||||
const [ { drop: drop_last } ]= json.drops;
|
const [ { drop: drop_last } ]= json.drops;
|
||||||
|
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "pagenotfound-cli",
|
"name": "pagenotfound-cli",
|
||||||
"version": "1.1.0",
|
"version": "1.2.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "pagenotfound-cli",
|
"name": "pagenotfound-cli",
|
||||||
"version": "1.1.0",
|
"version": "1.2.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"jsdom": "~24.1"
|
"jsdom": "~24.1"
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "pagenotfound-cli",
|
"name": "pagenotfound-cli",
|
||||||
"version": "1.1.1",
|
"version": "1.2.0",
|
||||||
"description": "Utility primary for generating RSS feed for Pagenotfound",
|
"description": "Utility primary for generating RSS feed for Pagenotfound",
|
||||||
"bin": "cli.js",
|
"bin": "cli.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
Loading…
Reference in New Issue
Block a user