From 40a01789ec43ac42dde49cb0de03f95138f44982 Mon Sep 17 00:00:00 2001 From: keyan Date: Sat, 2 Sep 2023 12:21:00 -0500 Subject: [PATCH] newsletter script --- scripts/newsletter.js | 90 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 scripts/newsletter.js diff --git a/scripts/newsletter.js b/scripts/newsletter.js new file mode 100644 index 00000000..8d537ca4 --- /dev/null +++ b/scripts/newsletter.js @@ -0,0 +1,90 @@ +const { ApolloClient, InMemoryCache, HttpLink, gql } = require('@apollo/client') + +const ITEMS = gql` + query items ($sort: String, $when: String, $sub: String) { + items (sort: $sort, when: $when, sub: $sub) { + cursor + items { + id + title + url + ncomments + sats + company + maxBid + status + location + remote + boost + user { + id + name + } + } + } + } +` + +const client = new ApolloClient({ + link: new HttpLink({ uri: 'https://stacker.news/api/graphql' }), + cache: new InMemoryCache() +}) + +const abbrNum = n => { + if (n < 1e4) return n + if (n >= 1e4 && n < 1e6) return +(n / 1e3).toFixed(1) + 'k' + if (n >= 1e6 && n < 1e9) return +(n / 1e6).toFixed(1) + 'm' + if (n >= 1e9 && n < 1e12) return +(n / 1e9).toFixed(1) + 'b' + if (n >= 1e12) return +(n / 1e12).toFixed(1) + 't' +} + +async function main () { + const top = await client.query({ + query: ITEMS, + variables: { sort: 'top', when: 'week' } + }) + + const meta = await client.query({ + query: ITEMS, + variables: { sort: 'top', when: 'week', sub: 'meta' } + }) + + const jobs = await client.query({ + query: ITEMS, + variables: { sub: 'jobs' } + }) + + process.stdout.write( +`Happy Sat-urday Stackers, + +Have a great weekend! + +##### Top Posts +${top.data.items.items.slice(0, 10).map((item, i) => + `${i + 1}. [${item.title}](https://stacker.news/items/${item.id})${item.url ? `\n - ${item.url}` : ''} + - ${abbrNum(item.sats)} sats \\ ${item.sats} boost \\ ${item.ncomments} comments\n`).join('')} + +##### Don't miss +${top.data.items.items.slice(0, 15).map((item, i) => + `- [${item.title}](https://stacker.news/items/${item.id})\n`).join('')} + +##### Top meta +${meta.data.items.items.slice(0, 10).map((item, i) => + `- [${item.title}](https://stacker.news/items/${item.id})\n`).join('')} + +[**all of this week's top posts**](https://stacker.news/top/posts/week) + +##### Promoted jobs +${jobs.data.items.items.filter(i => i.maxBid > 0 && i.status === 'ACTIVE').slice(0, 5).map((item, i) => + `${i + 1}. [${item.title.trim()} \\ ${item.company} \\ ${item.location}${item.remote ? ' or Remote' : ''}](https://stacker.news/items/${item.id})\n`).join('')} + +[**all jobs**](https://stacker.news/~jobs) + +Yeehaw, +Keyan +A guy who works on Stacker News + +[Watch](https://www.youtube.com/@stackernews/live) or [Listen](https://www.fountain.fm/show/Mg1AWuvkeZSFhsJZ3BW2) to SN's top stories every week.`) +} + +main()