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", "version": "0.0.0",
"dependencies": { "dependencies": {
"autoprefixer": "^10.4.17", "autoprefixer": "^10.4.17",
"cjs-loader": "^0.1.0",
"postcss": "^8.4.41", "postcss": "^8.4.41",
"postcss-cli": "^11.0.0" "postcss-cli": "^11.0.0"
}, },
@@ -238,6 +239,12 @@
"fsevents": "~2.3.2" "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": { "node_modules/cliui": {
"version": "8.0.1", "version": "8.0.1",
"resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz", "resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz",

View File

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

View File

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