-
Notifications
You must be signed in to change notification settings - Fork 1
/
send-webmentions.js
84 lines (76 loc) · 2.64 KB
/
send-webmentions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const { atom } = require("feed-read-parser");
const fs = require('fs');
const util = require('util');
const yaml = require('js-yaml');
const { Octokit } = require("@octokit/rest");
const { Base64 } = require("js-base64");
const { sendAllWebmentions } = require('send-all-webmentions');
const WM_RESULTS_PATH = "src/_data/wmresults.yaml";
const octokit = new Octokit({
auth: process.env.GH_ACCESS_TOKEN,
});
async function getWmResultsSHA() {
const result = await octokit.repos.getContent({
owner: "drivet",
repo: "website-11ty",
path: WM_RESULTS_PATH,
});
return result?.data?.sha;
}
async function commitWmResults(wmresults) {
try {
const yamlStr = yaml.dump(wmresults);
const encoded = Base64.encode(yamlStr);
const sha = await getWmResultsSHA();
const response = await octokit.repos.createOrUpdateFileContents({
owner: "drivet",
repo: "website-11ty",
path: WM_RESULTS_PATH,
message: "save wmresults",
committer: {
name: "Desmond Rivet",
email: "[email protected]",
},
author: {
name: "Desmond Rivet",
email: "[email protected]",
},
content: encoded,
sha,
});
console.log(`Successfully comitted wmresults: ${JSON.stringify(response)}`);
} catch(err) {
console.error(`Error committing wmresults: ${JSON.stringify(err)}`);
}
}
async function loadUrls() {
const feedData = fs.readFileSync('_site/posts/feed_all.xml', 'utf8');
const patom = util.promisify(atom);
const feed = await patom(feedData);
return feed.map(a => a.link.replace('https://desmondrivet.com/', ''));
}
function loadWebmentionResults() {
const wmresultsData = fs.readFileSync('./src/_data/wmresults.yaml', 'utf8');
return yaml.load(wmresultsData);
}
async function onSuccess() {
const urls = await loadUrls();
const wmresults = loadWebmentionResults();
const siteUrl = wmresults.site_url;
const sources = urls.filter(u => wmresults.results[u] === undefined || wmresults.results[u] === null);
if (sources.length === 0) {
console.log('No webmentions to process');
return;
}
console.log(`processing these source URLs for possible webmentions: ${JSON.stringify(sources)}`);
const report = await sendAllWebmentions(sources.map(s => `${siteUrl}/${s}`));
console.log(`webmentions (possibly) sent, showing results for ${siteUrl}`);
for (const source of sources) {
console.log(`Source URL: ${source}`);
const resultForSource = report[`${siteUrl}/${source}`] || {};
console.log(`Result: ${JSON.stringify(resultForSource)}`);
wmresults.results[source] = resultForSource;
}
await commitWmResults(wmresults);
}
onSuccess();