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:
|
||||
workflow_dispatch:
|
||||
schedule:
|
||||
- cron: '0 3 * * *' # daily at 3am
|
||||
- cron: '39 19 * * *' # daily at 19:39 Prague
|
||||
|
||||
jobs:
|
||||
Explore-Gitea-Actions:
|
||||
update-rss:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: https://gitea.com/actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
|
||||
@ -14,4 +14,4 @@ jobs:
|
||||
node-version: lts/*
|
||||
cache: 'npm'
|
||||
- run: npm ci
|
||||
- run: npx nodejsscript cli.js pull
|
||||
- run: npx nodejsscript cli.mjs pull --git
|
||||
|
46
cli.mjs
46
cli.mjs
@ -20,20 +20,44 @@ const { version, description }= s.cat("package.json").xargs(JSON.parse);
|
||||
* @property {Article[]} articles
|
||||
* @property {Drop[]} drops
|
||||
* */
|
||||
/**
|
||||
* @typedef {Object} State
|
||||
* @property {Sitemap} json
|
||||
* @property {string[]} changed Changed files
|
||||
* */
|
||||
|
||||
$.api()
|
||||
.version(version)
|
||||
.describe(description)
|
||||
.command("pull", "Update article list")
|
||||
.action(async function pull(){
|
||||
const json= await sitemap();
|
||||
toRSS(json);
|
||||
.option("--git", "Update git repository")
|
||||
.action(async function pull({ git: is_git= false }){
|
||||
const { changed }= await sitemap().then(toRSS);
|
||||
echo("Changed files:", changed.length ? changed.join(", ") : "—");
|
||||
if(is_git) gitCommit(changed, "pull");
|
||||
$.exit(0);
|
||||
})
|
||||
.parse();
|
||||
|
||||
/** @param {Sitemap} json */
|
||||
async function toRSS(json){
|
||||
function gitCommit(files, des= "not specified"){
|
||||
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 host= "https://pagenotfound.cz";
|
||||
const articles= json.articles.map(function({ title, perex, author, loc, drop }){
|
||||
@ -61,20 +85,22 @@ async function toRSS(json){
|
||||
"</channel>",
|
||||
"</rss>"
|
||||
].join("\n")).to(path);
|
||||
return { json, changed: [...changed, path] };
|
||||
}
|
||||
import { JSDOM } from "jsdom";
|
||||
/** @returns {Promise<State>} */
|
||||
async function sitemap(){
|
||||
const path= "sitemap.json";
|
||||
|
||||
/** @type {Sitemap} */
|
||||
const json= s.test("-f", path) ? s.cat(path).xargs(JSON.parse) : { drops: [], articles: [] };
|
||||
const [ article_last= { drop: "" } ]= json.articles;
|
||||
await syncDrops(json);
|
||||
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);
|
||||
if(res.status !== 200) return;
|
||||
if(res.status !== 200) return { json, changed: [] };
|
||||
|
||||
const dom= new JSDOM(await res.text());
|
||||
const diff= [];
|
||||
for(const article of dom.window.document.querySelectorAll("article")){
|
||||
@ -89,7 +115,7 @@ async function sitemap(){
|
||||
|
||||
json.articles.unshift(...diff);
|
||||
s.echo(JSON.stringify(json, null, "\t")).to(path);
|
||||
return json;
|
||||
return { json, changed: [ path ] };
|
||||
}
|
||||
/** @param {Sitemap} json */
|
||||
async function syncDrops(json){
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "pagenotfound-cli",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"description": "Utility primary for generating RSS feed for Pagenotfound",
|
||||
"bin": "cli.js",
|
||||
"scripts": {
|
||||
|
Reference in New Issue
Block a user