]> git.r.bdr.sh - rbdr/pinboard-linkblog-updater/blobdiff - index.js
Make some of the values more generic
[rbdr/pinboard-linkblog-updater] / index.js
index b6c3e10d10f0ca09be10d20f843b353215c9ef2c..518cc79b83c44610a609e824d7c7da1d9d48a30a 100644 (file)
--- a/index.js
+++ b/index.js
@@ -10,19 +10,55 @@ const internals = {
   apiToken: process.env.PINBOARD_TOKEN,
   blogUrl: process.env.BLOG_URL,
   archiveUrl: process.env.ARCHIVE_URL,
+  blogPublicUrl: process.env.BLOG_PUBLIC_URL,
+  archivePublicUrl: process.env.ARCHIVE_PUBLIC_URL,
+  tootToken: process.env.TOOT_TOKEN,
+  mastodonDomain: process.env.MASTODON_DOMAIN,
 
   date: (new Date()).toISOString().split('T')[0],
 
-  generateGemtext(posts) {
+  generateGemtext(title, text) {
 
-    const title = posts.length === 1 ? 'A link' : `${posts.length} links`;
+    return `# ${title}\n\n${text}`;
+  },
 
-    const linkText = posts.map((pin) => {
-      return `=> ${pin.href} ${pin.description}\n${pin.extended}`;
+  getText(posts) {
+
+    return posts.map((post) => {
+
+      return `=> ${post.href} ${post.description}\n${post.extended}`;
     }).join('\n\n');
+  },
+
+  getTitle(posts) {
+
+    if (posts.length === 1) {
+      return `Link: ${posts[0].description}`;
+    }
+    return `${posts.length} links for ${internals.date}`;
+  },
+
+  slugify(text) {
+
+    return text.toLowerCase().replace(/[^a-z0-9 ]/g, '').replace(/ +/g, '-')
+  },
 
-    return `# ${title} for ${internals.date}\n\n${linkText}`;
+  async toot(title) {
+
+    const body = new FormData();
+    body.set(
+      'status',
+      `New post: ${title}\n\nAvailable on:\n\nā™Šļø the gemini archive ${internals.archivePublicUrl}\n\n or, the ephemeral blog šŸŒ: ${internals.blogPublicUrl}`
+    );
+    return fetch(`https://${internals.mastodonDomain}/api/v1/statuses`, {
+      method: 'post',
+      headers: {
+        Authorization: `Bearer ${internals.tootToken}`,
+      },
+      body
+    });
   }
+
 };
 
 
@@ -33,20 +69,24 @@ async function run() {
 
   const pins = await getPins({ tag: 'linkblog' });
 
-  if (pins.posts.length === 0) {
+  if (pins.length === 0) {
     console.error('No links to post');
     return;
   }
 
-  const gemtext = internals.generateGemtext(pins.posts);
-  const gemfile = resolve(join(__dirname, `linkblog-${internals.date}.gmi`));
+  const title = internals.getTitle(pins);
+  const text = internals.getText(pins);
+  const gemtext = internals.generateGemtext(title, text);
+  const filename = internals.slugify(title);
+
+  const gemfile = resolve(join(__dirname, `${filename}.gmi`));
   await writeFile(gemfile, gemtext);
   await internals.exec(`blog --add ${gemfile}`);
   await internals.exec(`blog --publish ${internals.blogUrl}`);
   await internals.exec(`blog --publish-archive ${internals.archiveUrl}`);
   await rm(gemfile);
 
-  for (const pin of pins.posts) {
+  for (const pin of pins) {
     await addPin({
       url: pin.href,
       description: pin.description,
@@ -55,6 +95,10 @@ async function run() {
       tags: pin.tags.replace('linkblog', 'linkblog-posted')
     });
   }
+
+  if (internals.tootToken) {
+    await internals.toot(title);
+  }
 }
 
 run()