Scripting webmention

This commit is contained in:
fundor333
2024-08-10 11:54:13 +02:00
parent dffde502f3
commit 5cb1f12ca7
4 changed files with 69 additions and 6 deletions

7
package-lock.json generated
View File

@@ -9,6 +9,7 @@
"version": "0.0.0",
"dependencies": {
"autoprefixer": "^10.4.17",
"cjs-loader": "^0.1.0",
"postcss": "^8.4.41",
"postcss-cli": "^11.0.0"
},
@@ -238,6 +239,12 @@
"fsevents": "~2.3.2"
}
},
"node_modules/cjs-loader": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/cjs-loader/-/cjs-loader-0.1.0.tgz",
"integrity": "sha512-WgdnnrdNJT+ffiyU2S1zsrhbDjkDH4qgTbGYHPSN6XBVMSg1LbnSnUrVFMic4pWYypcTwkhoNfBB+GFP/73MHQ==",
"license": "Apache-2"
},
"node_modules/cliui": {
"version": "8.0.1",
"resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz",

View File

@@ -14,7 +14,8 @@
},
"dependencies": {
"autoprefixer": "^10.4.17",
"cjs-loader": "^0.1.0",
"postcss": "^8.4.41",
"postcss-cli": "^11.0.0"
}
}
}

View File

@@ -1,17 +1,22 @@
const fs = require("fs");
const https = require("https");
const path = require('path');
function writeFileSyncRecursive(filename, content = '') {
fs.mkdirSync(path.dirname(filename), { recursive: true })
fs.writeFileSync(filename, content)
}
fetchWebmentions().then(webmentions => {
webmentions.forEach(webmention => {
const slug = webmention["wm-target"]
.replace("https://fundor333.com/", "")
.replace(/\/$/, "")
.replace("/", "--");
.replace(/\/$/, "").split('?')[0];
const filename = `${__dirname}/data/webmentions/${slug}.json`;
if (!fs.existsSync(filename)) {
fs.writeFileSync(filename, JSON.stringify([webmention], null, 2));
writeFileSyncRecursive(filename, JSON.stringify([webmention], null, 2));
return;
}
@@ -26,11 +31,13 @@ fetchWebmentions().then(webmentions => {
});
});
function fetchWebmentions() {
const token = process.env.WEBMENTIONS_TOKEN;
const token = "-g5vlz9y3p5llrdS7TmnCg";
const since = new Date();
since.setDate(since.getDate() - 3);
since.setDate(since.getDate() - 30);
const url =
"https://webmention.io/api/mentions.jf2" +
@@ -61,6 +68,7 @@ function fetchWebmentions() {
});
}).then(response => {
if (!("children" in response)) {
console.log(response);
throw new Error("Invalid webmention.io response.");
}

47
webmention.py Normal file
View File

@@ -0,0 +1,47 @@
from pathlib import Path
from datetime import datetime, timedelta
import requests
from pprint import pprint
import json
import os
TOKEN = "-g5vlz9y3p5llrdS7TmnCg"
DOMAIN = "fundor333.com"
PER_PAGE = 999
SINCE_DAY = 999
def elaborate_webmention(webmention:list, site:str, remove_paramas: bool):
for e in webmention["children"]:
slug = e["wm-target"]
if remove_paramas is True:
slug = slug.split('?')[0]
print(slug)
slug = slug.replace(site, "").replace("\\", "")
ids= e['wm-id']
filename = os.path.join(f'data/webmentions/{slug}/{ids}.json')
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, "w") as f:
json.dump(e, f)
def get_webmention(remove_paramas: bool=True):
r = requests.get(
"https://webmention.io/api/mentions.jf2",
params={'domain': DOMAIN,
'token':TOKEN,
'since': datetime.today() - timedelta(days=SINCE_DAY),
'per-page':PER_PAGE
}
)
elaborate_webmention(webmention=r.json(),site=f"https://{DOMAIN}", remove_paramas=remove_paramas)
get_webmention()