]>
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 | ||
16 | generateGemtext(posts) { | |
17 | ||
18 | const title = posts.length === 1 ? 'A link' : `${posts.length} links`; | |
19 | ||
20 | const linkText = posts.map((pin) => { | |
21 | return `=> ${pin.href} ${pin.description}\n${pin.extended}`; | |
22 | }).join('\n\n'); | |
23 | ||
24 | return `# ${title} for ${internals.date}\n\n${linkText}`; | |
25 | } | |
26 | }; | |
27 | ||
28 | ||
29 | async function run() { | |
30 | const pinboard = new Pinboard(internals.apiToken); | |
096cf8b1 | 31 | const getPins = promisify(pinboard.all); |
89326417 RBR |
32 | const addPin = promisify(pinboard.add); |
33 | ||
89326417 RBR |
34 | const pins = await getPins({ tag: 'linkblog' }); |
35 | ||
28414efe | 36 | if (pins.length === 0) { |
89326417 RBR |
37 | console.error('No links to post'); |
38 | return; | |
39 | } | |
40 | ||
28414efe | 41 | const gemtext = internals.generateGemtext(pins); |
89326417 | 42 | const gemfile = resolve(join(__dirname, `linkblog-${internals.date}.gmi`)); |
89326417 | 43 | await writeFile(gemfile, gemtext); |
2390d230 RBR |
44 | await internals.exec(`blog --add ${gemfile}`); |
45 | await internals.exec(`blog --publish ${internals.blogUrl}`); | |
46 | await internals.exec(`blog --publish-archive ${internals.archiveUrl}`); | |
89326417 RBR |
47 | await rm(gemfile); |
48 | ||
28414efe | 49 | for (const pin of pins) { |
89326417 RBR |
50 | await addPin({ |
51 | url: pin.href, | |
52 | description: pin.description, | |
53 | extended: pin.extended, | |
54 | dt: pin.time, | |
55 | tags: pin.tags.replace('linkblog', 'linkblog-posted') | |
56 | }); | |
57 | } | |
58 | } | |
59 | ||
60 | run() | |
61 | .then(() => process.exit(0)) | |
62 | .catch((err) => { | |
63 | console.error(err); | |
64 | process.exit(1); | |
65 | }) |