From: Ruben Beltran del Rio Date: Fri, 9 Dec 2022 17:11:01 +0000 (+0100) Subject: Add the project X-Git-Url: https://git.r.bdr.sh/rbdr/linkding-linkblog-updater/commitdiff_plain/893264177a03bc7dd3e306753024fe1394fc3e77?ds=sidebyside Add the project --- 893264177a03bc7dd3e306753024fe1394fc3e77 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..4b1e893 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# Pinboard Linkblog Updater + +This only works with [blog][blog]. + + +[blog]: https://git.sr.ht/~rbdr/blog diff --git a/index.js b/index.js new file mode 100644 index 0000000..78c3d26 --- /dev/null +++ b/index.js @@ -0,0 +1,68 @@ +const { exec } = require('child_process'); +const { resolve, join} = require('path'); +const { rm, writeFile } = require('fs/promises'); +const { debuglog, promisify } = require('util'); +const Pinboard = require('node-pinboard').default; + +const internals = { + exec: promisify(exec), + debuglog: debuglog('blog'), + apiToken: process.env.PINBOARD_TOKEN, + date: (new Date()).toISOString().split('T')[0], + + generateGemtext(posts) { + + const title = posts.length === 1 ? 'A link' : `${posts.length} links`; + + const linkText = posts.map((pin) => { + return `=> ${pin.href} ${pin.description}\n${pin.extended}`; + }).join('\n\n'); + + return `# ${title} for ${internals.date}\n\n${linkText}`; + } +}; + + +async function run() { + const pinboard = new Pinboard(internals.apiToken); + const getPins = promisify(pinboard.get); + const addPin = promisify(pinboard.add); + + internals.debuglog('Fetching Pins'); + const pins = await getPins({ tag: 'linkblog' }); + + internals.debuglog(`Found ${pins.posts.length} pins`); + if (pins.posts.length === 0) { + console.error('No links to post'); + return; + } + + internals.debuglog('Generating gemtext'); + const gemtext = internals.generateGemtext(pins.posts); + const gemfile = resolve(join(__dirname, `linkblog-${internals.date}.gmi`)); + internals.debuglog(`Writing to ${gemfile}`); + await writeFile(gemfile, gemtext); + internals.debuglog('Adding gemfile to log'); + const output = await internals.exec(`blog --add ${gemfile}`); + internals.debuglog(output.stderr); + internals.debuglog(`Removing ${gemfile}`); + await rm(gemfile); + + for (const pin of pins.posts) { + internals.debuglog(`Updating ${pin.href}`); + await addPin({ + url: pin.href, + description: pin.description, + extended: pin.extended, + dt: pin.time, + tags: pin.tags.replace('linkblog', 'linkblog-posted') + }); + } +} + +run() + .then(() => process.exit(0)) + .catch((err) => { + console.error(err); + process.exit(1); + }) diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..bd3a4f9 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,102 @@ +{ + "name": "pinboard-linkblog-updater", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "pinboard-linkblog-updater", + "version": "1.0.0", + "license": "Apache-2.0", + "dependencies": { + "node-pinboard": "^2.0.1" + } + }, + "node_modules/node-fetch": { + "version": "2.6.7", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", + "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, + "node_modules/node-pinboard": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/node-pinboard/-/node-pinboard-2.0.1.tgz", + "integrity": "sha512-TvFjnLQRW/mEJmBDbBaL3APOxx1bCxCt1iw37YqgtLzWNUnu9zez2JTARd4qjcXp6uvJ8oaRhFNRJsLBhR7epQ==", + "dependencies": { + "node-fetch": "^2.6.0" + }, + "engines": { + "node": ">= 8.0" + } + }, + "node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, + "node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + } + }, + "dependencies": { + "node-fetch": { + "version": "2.6.7", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", + "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", + "requires": { + "whatwg-url": "^5.0.0" + } + }, + "node-pinboard": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/node-pinboard/-/node-pinboard-2.0.1.tgz", + "integrity": "sha512-TvFjnLQRW/mEJmBDbBaL3APOxx1bCxCt1iw37YqgtLzWNUnu9zez2JTARd4qjcXp6uvJ8oaRhFNRJsLBhR7epQ==", + "requires": { + "node-fetch": "^2.6.0" + } + }, + "tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, + "webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "requires": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..689672c --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "pinboard-linkblog-updater", + "version": "1.0.0", + "description": "A linkblog automation for blog, using pinboard as a source, intended to run with cron", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+ssh://git@git.sr.ht/~rbdr/pinboard-linkblog-updater.git" + }, + "author": "", + "license": "Apache-2.0", + "bugs": { + "url": "https://todo.sr.ht/~rbdr/pinboard-linkblog-updater" + }, + "homepage": "https://git.sr.ht/~rbdr/pinboard-linkblog-updater#readme", + "dependencies": { + "node-pinboard": "^2.0.1" + } +}