Webmention action

This commit is contained in:
Fundor333
2025-01-16 19:01:51 +01:00
parent b61ac4c0f3
commit 72dcd9fe9d
2 changed files with 105 additions and 0 deletions

36
.github/workflows/webmentions.yml vendored Normal file
View File

@@ -0,0 +1,36 @@
name: Webmentions
on:
schedule:
- cron: "0 */6 * * *"
jobs:
webmentions:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@master
- name: Set up Node.js
uses: actions/setup-node@master
with:
node-version: 12.x
- name: Fetch webmentions
env:
WEBMENTIONS_TOKEN: ${{ secrets.WEBMENTIONS_TOKEN }}
run: node ./webmentions.js
- name: Commit to repository
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMIT_MSG: |
Fetch webmentions
skip-checks: true
run: |
git config user.email "git@fundor333.com"
git config user.name "fundor333"
git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/fundor333/fundor333.github.io.git
git checkout master
git add .
git diff --quiet && git diff --staged --quiet || (git commit -m "${COMMIT_MSG}"; git push origin master)

69
webmentions.js Normal file
View File

@@ -0,0 +1,69 @@
const fs = require("fs");
const https = require("https");
fetchWebmentions().then(webmentions => {
webmentions.forEach(webmention => {
const slug = webmention["wm-target"]
.replace("https://fundor333.com/", "")
.replace(/\/$/, "")
.replace("/", "--");
const filename = `${__dirname}/data/webmentions/${slug}.json`;
if (!fs.existsSync(filename)) {
fs.writeFileSync(filename, JSON.stringify([webmention], null, 2));
return;
}
const entries = JSON.parse(fs.readFileSync(filename))
.filter(wm => wm["wm-id"] !== webmention["wm-id"])
.concat([webmention]);
entries.sort((a, b) => a["wm-id"] - b["wm-id"]);
fs.writeFileSync(filename, JSON.stringify(entries, null, 2));
});
});
function fetchWebmentions() {
const token = process.env.WEBMENTIONS_TOKEN;
const since = new Date();
since.setDate(since.getDate() - 3);
const url =
"https://webmention.io/api/mentions.jf2" +
"?domain=fundor333.com" +
`&token=${token}` +
`&since=${since.toISOString()}` +
"&per-page=999";
return new Promise((resolve, reject) => {
https
.get(url, res => {
let body = "";
res.on("data", chunk => {
body += chunk;
});
res.on("end", () => {
try {
resolve(JSON.parse(body));
} catch (error) {
reject(error);
}
});
})
.on("error", error => {
reject(error);
});
}).then(response => {
if (!("children" in response)) {
throw new Error("Invalid webmention.io response.");
}
return response.children;
});
}