Compare commits
8 Commits
5700e740f8
...
v1.1.0
Author | SHA1 | Date | |
---|---|---|---|
df82405ed8
|
|||
39bb5ef62d
|
|||
4bbd88a563
|
|||
0054ceb6f2
|
|||
add7993460
|
|||
db2af48953
|
|||
597e21895a
|
|||
837cea046d
|
@ -2,10 +2,10 @@ name: Update RSS
|
|||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
schedule:
|
schedule:
|
||||||
- cron: '0 3 * * *' # daily at 3am
|
- cron: '39 19 * * *' # daily at 19:39 Prague
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
Explore-Gitea-Actions:
|
update-rss:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: https://gitea.com/actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
|
- uses: https://gitea.com/actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
|
||||||
@ -14,4 +14,4 @@ jobs:
|
|||||||
node-version: lts/*
|
node-version: lts/*
|
||||||
cache: 'npm'
|
cache: 'npm'
|
||||||
- run: npm ci
|
- run: npm ci
|
||||||
- run: npx nodejsscript cli.js pull
|
- run: npx nodejsscript cli.mjs pull --git
|
||||||
|
48
cli.mjs
48
cli.mjs
@ -20,20 +20,44 @@ const { version, description }= s.cat("package.json").xargs(JSON.parse);
|
|||||||
* @property {Article[]} articles
|
* @property {Article[]} articles
|
||||||
* @property {Drop[]} drops
|
* @property {Drop[]} drops
|
||||||
* */
|
* */
|
||||||
|
/**
|
||||||
|
* @typedef {Object} State
|
||||||
|
* @property {Sitemap} json
|
||||||
|
* @property {string[]} changed Changed files
|
||||||
|
* */
|
||||||
|
|
||||||
$.api()
|
$.api()
|
||||||
.version(version)
|
.version(version)
|
||||||
.describe(description)
|
.describe(description)
|
||||||
.command("pull", "Update article list")
|
.command("pull", "Update article list")
|
||||||
.action(async function pull(){
|
.option("--git", "Update git repository")
|
||||||
const json= await sitemap();
|
.action(async function pull({ git: is_git= false }){
|
||||||
toRSS(json);
|
const { changed }= await sitemap().then(toRSS);
|
||||||
|
echo("Changed files:", changed.length ? changed.join(", ") : "—");
|
||||||
|
if(is_git) gitCommit(changed, "pull");
|
||||||
$.exit(0);
|
$.exit(0);
|
||||||
})
|
})
|
||||||
.parse();
|
.parse();
|
||||||
|
|
||||||
/** @param {Sitemap} json */
|
function gitCommit(files, des= "not specified"){
|
||||||
async function toRSS(json){
|
if(!files.length || !s.run`git diff --numstat`.trim())
|
||||||
|
return echo("Nothig todo");
|
||||||
|
|
||||||
|
echo("Diff to save");
|
||||||
|
s.run`git config user.name "Bot"`;
|
||||||
|
s.run`git config user.email "${"zc.murtnec@naj.elrdna".split("").reverse().join("")}"`;
|
||||||
|
s.run`git add ${files}`;
|
||||||
|
s.run`git commit -m "Updated by bot – ${des}"`;
|
||||||
|
s.run`git push`;
|
||||||
|
s.run`git config --remove-section user`;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param {State} state
|
||||||
|
* @returns {State} state
|
||||||
|
* */
|
||||||
|
async function toRSS({ json, changed }){
|
||||||
|
if(!changed.length) return { json, changed };
|
||||||
|
|
||||||
const path= "rss.xml";
|
const path= "rss.xml";
|
||||||
const host= "https://pagenotfound.cz";
|
const host= "https://pagenotfound.cz";
|
||||||
const articles= json.articles.map(function({ title, perex, author, loc, drop }){
|
const articles= json.articles.map(function({ title, perex, author, loc, drop }){
|
||||||
@ -61,20 +85,22 @@ async function toRSS(json){
|
|||||||
"</channel>",
|
"</channel>",
|
||||||
"</rss>"
|
"</rss>"
|
||||||
].join("\n")).to(path);
|
].join("\n")).to(path);
|
||||||
|
return { json, changed: [...changed, path] };
|
||||||
}
|
}
|
||||||
import { JSDOM } from "jsdom";
|
import { JSDOM } from "jsdom";
|
||||||
|
/** @returns {Promise<State>} */
|
||||||
async function sitemap(){
|
async function sitemap(){
|
||||||
const path= "sitemap.json";
|
const path= "sitemap.json";
|
||||||
|
|
||||||
/** @type {Sitemap} */
|
/** @type {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: [] };
|
||||||
const [ article_last= { drop: "" } ]= json.articles;
|
|
||||||
await syncDrops(json);
|
await syncDrops(json);
|
||||||
const [ { drop: drop_last } ]= json.drops;
|
const [ { drop: drop_last } ]= json.drops;
|
||||||
if(drop_last === article_last.drop) return json;
|
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;
|
if(res.status !== 200) return { json, changed: [] };
|
||||||
|
|
||||||
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")){
|
||||||
@ -89,7 +115,7 @@ async function sitemap(){
|
|||||||
|
|
||||||
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;
|
return { json, changed: [ path ] };
|
||||||
}
|
}
|
||||||
/** @param {Sitemap} json */
|
/** @param {Sitemap} json */
|
||||||
async function syncDrops(json){
|
async function syncDrops(json){
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "pagenotfound-cli",
|
"name": "pagenotfound-cli",
|
||||||
"version": "1.0.0",
|
"version": "1.1.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": {
|
||||||
|
Reference in New Issue
Block a user