Skip to content

Commit

Permalink
🔖 v0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jaandrle committed Jun 1, 2023
1 parent eadf27d commit abc8a8b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/scheduled-posts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ jobs:
node-version: lts/*
cache: 'npm'
- run: npm ci
- run: npx nodejsscript cli.js --url 'https://fosstodon.org' --token "${MASTODON_ACCESS_TOKEN}"
- run: npx nodejsscript cli.js --url 'https://fosstodon.org' --token "${MASTODON_ACCESS_TOKEN}" --publish
env:
MASTODON_ACCESS_TOKEN: ${{ secrets.MASTODON_ACCESS_TOKEN }}
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ It is also a npm package exporting method `randomMDN`.
For automatic posting, the [GitHub Action](https://docs.github.com/en/actions) is used,
see [`.github/workflows/scheduled-posts.yml`](./.github/workflows/scheduled-posts.yml).

Inspired by [Random MDN (@randomMDN) / Twitter](https://twitter.com/randomMDN).

## Mastodon
You can find the bot on TBD.

## RSS
You can find the rss feed on TBD[^1].

## Acknowledgments
- [random-mdn/random-mdn-bot: Serverless functions tweeting/sending/... random MDN articles](https://github.com/random-mdn/random-mdn-bot)
- [A Beginner's Guide to the Mastodon API - Post a Status Update with cURL or Python - DEV Community](https://dev.to/bitsrfr/getting-started-with-the-mastodon-api-41jj)
- [Simple Mastodon bot in 100 lines.](https://gist.github.com/NeKzor/e7d8551c4f55fbe4ec16252e0f6fa012)

[1] FYI: [Mastodon and RSS](https://derekkedziora.com/notes/20221112094802)
56 changes: 38 additions & 18 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ if($.isMain(import.meta))
])
.option("--url", "instance url (e.g.: `https://mstdn.social`) – required")
.option("--token", "a token for the mastodon account – required")
.action(async function main({ url, token }){
.option("--publish", "sends post")
.action(async function main({ url, token, publish }){
if(!url) throw new Error("--url is required");
if(!token) throw new Error("--token is required");

const article= await randomMDN();
return echo(article);
const article= await randomMDN().then(compose);
if(!publish) return echo(article);
const res= await post({ url, token, article }).then(res=> res.json());
echo(res);
$.exit(0);
Expand All @@ -46,15 +47,8 @@ export async function randomMDN(){
)();
return candidate;
}
/**
* @param {{
* url: string,
* token: script,
* article: Article_object
* }} def
* */
async function post({ url, token, article }){
const status= compose(article);
/** @param {{ url: string, token: script, status: string }} def */
async function post({ url, token, status }){
return fetch(new URL("api/v1/statuses", url), {
method: "POST",
headers: {
Expand All @@ -66,16 +60,19 @@ async function post({ url, token, article }){
}
/** @param {Article_object} article @returns {string} */
function compose({ title, description, link }){
const limit= 500, reserve= 10;
const limit= 500, reserve= 15;
let { length }= description;
description= description.slice(0, limit - reserve - title.length - link.length);
const hashtags= getHashtags(link).join(" ");
description= description.slice(0,
limit - reserve - title.length - link.length - hashtags.length);
length-= description.length;
if(length) description+= "…";//1
if(length) description+= "…";//….length= 1
return [
title,
`🦖 ${title} 🦖`,//2×" 🦖".length= 6
link,
description,
link
].join("\n\n");//2×2
hashtags
].join("\n\n");//3×"\n\n"= 6
}
async function getWebDocUrls(){
const sitemap= await fetch(url_sitemap, {
Expand Down Expand Up @@ -110,4 +107,27 @@ function extractByRegexp(str, regexp){
if(!candidate) return null;
return candidate[1];
}
/**
* Get appropriate hashtags for the URL
* (probably can be way smarter and better)
*
* @param {String} url
* @returns {Array} fitting hashtags for the URL
*/
function getHashtags(url){
const hashtags= [ "#webdev" ];
const [ , section ]= url.match(/Web\/(.*?)\//);
const hashtagWorthySections = [
"CSS",
"Accessibility",
"JavaScript",
"HTTP",
"HTML",
"SVG",
];
if(hashtagWorthySections.includes(section))
hashtags.push(`#${section}`);

return hashtags;
}
function onlyAllowWebUrls(url){ return url.startsWith(url_web); }
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import "nodejsscript";
import {randomMDN} from "./cli.js";
import { randomMDN } from "./cli.js";
export { randomMDN };
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mastodon-randommdn",
"version": "0.5.0",
"version": "0.6.0",
"description": "Posting a random article from MDN.",
"type": "module",
"main": "index.js",
Expand All @@ -18,7 +18,8 @@
"javascript",
"webdev",
"css",
"html"
"html",
"rss"
],
"author": "Jan Andrle <[email protected]>",
"license": "MIT",
Expand Down

0 comments on commit abc8a8b

Please sign in to comment.