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
85
86
87
88
89
90
91
92
93
94
95
import { set } from "https://esm.town/v/std/set?v=15";
import { altelierHarfanfLastItems } from "https://esm.town/v/vogelino/altelierHarfanfLastItems";
import buildRSSFeedString from "https://esm.town/v/vogelino/buildRSSFeedString";
import { DOMParser } from "npm:linkedom@0.16.10";
function response(message: string, contentType = "text/markdown"): Response {
const headers = new Headers();
headers.set("Access-Control-Allow-Origin", "*");
headers.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
headers.set("Access-Control-Allow-Headers", "Content-Type, Authorization");
headers.set("Access-Control-Max-Age", "86400");
headers.set("Content-Type", contentType);
return new Response(message, {
status: 200,
headers: headers,
});
}
export default async function(req: Request): Promise<Response> {
const url = new URL(`https://atelierharfang.ch`);
const html = await fetch(url.toString(), {
method: req.method,
headers: new Headers({
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Sec-Fetch-Site": "cross-site",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-User": "?1",
"Sec-Fetch-Dest": "document",
"Referer": "https://www.google.com/",
"sec-ch-ua": `"Not A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"`,
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": `"macOS"`,
"Upgrade-Insecure-Requests": "1",
// Add any other headers you need here
}),
}).then(r => r.text());
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
console.log(html);
const htmlTitle = doc.head.querySelector("title")?.textContent;
const title = htmlTitle || `RSS feed de l'Atelier Harfang`;
const htmlDescription = doc.head.querySelector("meta[name=description]")?.getAttribute("content");
const description = htmlDescription
|| `L'activité de l'Atelier Harfang s’articule principalement autour de la réalisation d’identité visuelle. Disposant des deux entités en notre sein, design graphique et développement web, nous couvrons l’intégralité des phases de réalisation.`;
const imagePath = doc.head.querySelector("link[rel=icon]")?.getAttribute("href");
let image = imagePath?.startsWith("http") ? imagePath : `${url.origin}/${imagePath}`.replace(/\/\//g, "/");
image = imagePath?.startsWith("data:") ? imagePath : image;
const postsLinks = Array.from(doc.body.querySelectorAll(`main > a`));
const items = [];
for (const postIdx in postsLinks) {
const post = postsLinks[postIdx];
const title = post.querySelector(".col.s10.m6.l6")?.textContent.trim();
const link = post.getAttribute("href");
const date = altelierHarfanfLastItems[postIdx + 1]?.date || new Date();
const linkFormatted = `${url.origin}/${link}`.replace(/\/\//g, "/");
const paragraphs = Array.from(post.querySelectorAll("p"));
const description = post.querySelector(".col.s10.m4.l4")?.textContent;
if (link && date && title && description && linkFormatted) {
items.push({
title,
guid: linkFormatted,
link: linkFormatted,
author: `Atelier Harfang`,
description,
date,
});
}
}
const rssAsString = buildRSSFeedString({
title: title.trim(),
description: description.trim(),
link: url.toString(),
image: imagePath && {
url: image.trim(),
title: `Icon for "${title.trim()}"`,
link: url.toString(),
},
items,
});
set("altelierHarfanfLastItems", items);
return response(rssAsString, "application/rss+xml");
}