]>
Commit | Line | Data |
---|---|---|
89326417 RBR |
1 | const { exec } = require('child_process'); |
2 | const { resolve, join} = require('path'); | |
3 | const { rm, writeFile } = require('fs/promises'); | |
2390d230 | 4 | const { promisify } = require('util'); |
89326417 RBR |
5 | const Pinboard = require('node-pinboard').default; |
6 | ||
7 | const internals = { | |
8 | exec: promisify(exec), | |
2390d230 | 9 | |
89326417 | 10 | apiToken: process.env.PINBOARD_TOKEN, |
2390d230 RBR |
11 | blogUrl: process.env.BLOG_URL, |
12 | archiveUrl: process.env.ARCHIVE_URL, | |
13 | ||
89326417 RBR |
14 | date: (new Date()).toISOString().split('T')[0], |
15 | ||
f0b2faff | 16 | generateGemtext(title, text) { |
89326417 | 17 | |
f0b2faff RBR |
18 | return `# ${title}\n\n${text}`; |
19 | }, | |
89326417 | 20 | |
f0b2faff RBR |
21 | getText(posts) { |
22 | ||
23 | const linkText = posts.map((post) => { | |
24 | ||
25 | return `=> ${post.href} ${post.description}\n${post.extended}`; | |
89326417 | 26 | }).join('\n\n'); |
f0b2faff RBR |
27 | }, |
28 | ||
29 | getTitle(posts) { | |
89326417 | 30 | |
f0b2faff RBR |
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, '-') | |
89326417 RBR |
40 | } |
41 | }; | |
42 | ||
43 | ||
44 | async function run() { | |
45 | const pinboard = new Pinboard(internals.apiToken); | |
096cf8b1 | 46 | const getPins = promisify(pinboard.all); |
89326417 RBR |
47 | const addPin = promisify(pinboard.add); |
48 | ||
89326417 RBR |
49 | const pins = await getPins({ tag: 'linkblog' }); |
50 | ||
28414efe | 51 | if (pins.length === 0) { |
89326417 RBR |
52 | console.error('No links to post'); |
53 | return; | |
54 | } | |
55 | ||
f0b2faff RBR |
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`)); | |
89326417 | 62 | await writeFile(gemfile, gemtext); |
2390d230 RBR |
63 | await internals.exec(`blog --add ${gemfile}`); |
64 | await internals.exec(`blog --publish ${internals.blogUrl}`); | |
65 | await internals.exec(`blog --publish-archive ${internals.archiveUrl}`); | |
89326417 RBR |
66 | await rm(gemfile); |
67 | ||
28414efe | 68 | for (const pin of pins) { |
89326417 RBR |
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 | }) |