]> git.r.bdr.sh - rbdr/linkding-linkblog-updater/blob - index.js
549e223b4d19aaef488ce24dfd5cbe43175facd8
[rbdr/linkding-linkblog-updater] / index.js
1 const { exec } = require('child_process');
2 const { resolve, join} = require('path');
3 const { rm, writeFile } = require('fs/promises');
4 const { promisify } = require('util');
5 const Pinboard = require('node-pinboard').default;
6
7 const internals = {
8 exec: promisify(exec),
9
10 apiToken: process.env.PINBOARD_TOKEN,
11 blogUrl: process.env.BLOG_URL,
12 archiveUrl: process.env.ARCHIVE_URL,
13
14 date: (new Date()).toISOString().split('T')[0],
15
16 generateGemtext(title, text) {
17
18 return `# ${title}\n\n${text}`;
19 },
20
21 getText(posts) {
22
23 const linkText = posts.map((post) => {
24
25 return `=> ${post.href} ${post.description}\n${post.extended}`;
26 }).join('\n\n');
27 },
28
29 getTitle(posts) {
30
31 if (posts.length === 1) {
32 return posts[0].description;
33 }
34 return `${posts.length} links for ${internals.date}`;
35 },
36
37 slugify(text) {
38
39 return text.toLowerCase().replace(/[^a-z0-9 ]/g, '').replace(/ +/g, '-')
40 }
41 };
42
43
44 async function run() {
45 const pinboard = new Pinboard(internals.apiToken);
46 const getPins = promisify(pinboard.all);
47 const addPin = promisify(pinboard.add);
48
49 const pins = await getPins({ tag: 'linkblog' });
50
51 if (pins.length === 0) {
52 console.error('No links to post');
53 return;
54 }
55
56 const title = internals.getTitle(posts);
57 const text = internals.getText(posts);
58 const gemtext = internals.generateGemtext(title, text);
59 const filename = internals.slugify(title);
60
61 const gemfile = resolve(join(__dirname, `${filename}.gmi`));
62 await writeFile(gemfile, gemtext);
63 await internals.exec(`blog --add ${gemfile}`);
64 await internals.exec(`blog --publish ${internals.blogUrl}`);
65 await internals.exec(`blog --publish-archive ${internals.archiveUrl}`);
66 await rm(gemfile);
67
68 for (const pin of pins) {
69 await addPin({
70 url: pin.href,
71 description: pin.description,
72 extended: pin.extended,
73 dt: pin.time,
74 tags: pin.tags.replace('linkblog', 'linkblog-posted')
75 });
76 }
77 }
78
79 run()
80 .then(() => process.exit(0))
81 .catch((err) => {
82 console.error(err);
83 process.exit(1);
84 })