]>
git.r.bdr.sh - rbdr/linkding-linkblog-updater/blob - index.js
2059534c08c2bfadfd7e97e87fa10d2e728e678d
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 ;
10 apiToken : process
. env
. PINBOARD_TOKEN
,
11 blogUrl : process
. env
. BLOG_URL
,
12 archiveUrl : process
. env
. ARCHIVE_URL
,
13 tootToken : process
. env
. TOOT_TOKEN
,
15 date : ( new Date ()). toISOString (). split ( 'T' )[ 0 ],
17 generateGemtext ( title
, text
) {
19 return `# ${title} \n\n ${text} ` ;
24 return posts
. map (( post
) => {
26 return `=> ${post.href} ${post.description} \n ${post.extended} ` ;
32 if ( posts
. length
=== 1 ) {
33 return `Link: ${posts[0].description} ` ;
35 return ` ${posts.length} links for ${internals.date} ` ;
40 return text
. toLowerCase (). replace (/[^ a
- z0
- 9 ]/ g
, '' ). replace (/ +/ g
, '-' )
45 const body
= new FormData ();
48 `New post: ${title} \n\n Available on: \n\n ♊️ the gemini archive gemini://gemini.unlimited.pizza/gemlog/ \n\n or, the ephemeral blog 🌐: https://blog.unlimited.pizza`
50 return fetch ( 'https://mastodon.social/api/v1/statuses' , {
53 Authorization : `Bearer ${internals.tootToken} ` ,
62 async
function run () {
63 const pinboard
= new Pinboard ( internals
. apiToken
);
64 const getPins
= promisify ( pinboard
. all
);
65 const addPin
= promisify ( pinboard
. add
);
67 const pins
= await
getPins ({ tag : 'linkblog' });
69 if ( pins
. length
=== 0 ) {
70 console
. error ( 'No links to post' );
74 const title
= internals
. getTitle ( pins
);
75 const text
= internals
. getText ( pins
);
76 const gemtext
= internals
. generateGemtext ( title
, text
);
77 const filename
= internals
. slugify ( title
);
79 const gemfile
= resolve ( join ( __dirname
, ` ${filename} .gmi` ));
80 await
writeFile ( gemfile
, gemtext
);
81 await internals
. exec ( `blog --add ${gemfile} ` );
82 await internals
. exec ( `blog --publish ${internals.blogUrl} ` );
83 await internals
. exec ( `blog --publish-archive ${internals.archiveUrl} ` );
86 for ( const pin
of pins
) {
89 description : pin
. description
,
90 extended : pin
. extended
,
92 tags : pin
. tags
. replace ( 'linkblog' , 'linkblog-posted' )
96 if ( internals
. tootToken
) {
97 await internals
. toot ( title
);
102 . then (() => process
. exit ( 0 ))