Automate more stuff

This commit is contained in:
ekzyis 2025-04-02 18:57:01 +02:00
parent 93608019cd
commit 71e06f09e3

View File

@ -25,7 +25,7 @@ if (!SN_API_KEY) {
process.exit(1) process.exit(1)
} }
const LIMIT = 21 const LIMIT = 50
async function gql (query, variables = {}) { async function gql (query, variables = {}) {
const response = await fetch(`${SN_API_URL}/api/graphql`, { const response = await fetch(`${SN_API_URL}/api/graphql`, {
@ -71,6 +71,30 @@ async function assertSettings () {
} }
} }
async function fetchData () {
return await Promise.all([
fetchLatestWelcomePost(),
fetchRecentBios()
])
}
async function fetchLatestWelcomePost () {
const { items: { items } } = await gql(
`query LatestWelcomePost {
items(name: "ek", sort: "user", type: "posts", limit: 50) {
items {
id
title
text
}
}
}`)
const regex = /Baby Stacker Corner/i
for (const item of items) if (regex.test(item.title)) return item
throw new Error('latest welcome post not found')
}
async function fetchRecentBios () { async function fetchRecentBios () {
// fetch all recent bios. we assume here there won't be more than 21 // fetch all recent bios. we assume here there won't be more than 21
// since the last bio we already included in a post as defined by FETCH_AFTER. // since the last bio we already included in a post as defined by FETCH_AFTER.
@ -102,8 +126,11 @@ async function fetchRecentBios () {
return newBios return newBios
} }
async function populate (bios) { async function populate (data) {
return await Promise.all( const [welcomePost, bios] = data
return await Promise.all([
welcomePost,
Promise.all(
bios.map( bios.map(
async bio => { async bio => {
bio.user.since = await util.fetchItem(bio.user.since) bio.user.since = await util.fetchItem(bio.user.since)
@ -119,9 +146,36 @@ async function populate (bios) {
} }
) )
) )
])
} }
async function printTable (bios) { function printIntro (data) {
const [welcomePost, bios] = data
console.log(`> latest welcome post: ${welcomePost.title}`)
const nr = Number(welcomePost.title.match(/\d+/)[0])
console.log(`\n# Baby Stacker Corner #${nr + 1}\n`)
let series = welcomePost.text.split('\n').filter(line => line.startsWith('whole series:'))[0]
series += `, [#${nr}](${util.itemLink(welcomePost.id)})\n`
console.log(series)
console.log(`${bios.length} new stackers have found their way to Stacker News this week!\n`)
console.log('Questions for the new stackers:')
console.log('- How did you find out about SN?')
console.log('- How difficult was it to get started? Any feedback?')
console.log('- How much experience do you have with lightning?')
console.log('- Have you read the [FAQ](https://stacker.news/faq) already?')
console.log('- Have you realized that you need to attach a wallet to receive sats?')
console.log('- How was your first week on SN?\n')
return data
}
async function printTable (data) {
const [, bios] = data
console.log('| nym | bio (stacking since) | items | sats/ccs stacked | sat standard |') console.log('| nym | bio (stacking since) | items | sats/ccs stacked | sat standard |')
console.log('| --- | -------------------- | ----- | ---------------- | ------------ |') console.log('| --- | -------------------- | ----- | ---------------- | ------------ |')
@ -141,11 +195,21 @@ async function printTable (bios) {
console.log(`| @${user.name} | ${col2} | ${user.nitems} | ${user.sats}/${user.credits} | ${user.satstandard.toFixed(2)} |`) console.log(`| @${user.name} | ${col2} | ${user.nitems} | ${user.sats}/${user.credits} | ${user.satstandard.toFixed(2)} |`)
} }
console.log(`${bios.length} rows`) return data
return bios
} }
function printOutro (data) {
console.log('\n_sat standard = ratio of received sats vs credits (`sats/(sats+credits)`)_\n')
console.log('Questions for the old stackers:')
console.log('- Anyone in there who you want to point out?')
console.log('- Do you know better questions I could ask the new stackers or you?\n')
console.log('<<< INSERT MEME HERE >>>')
console.log('inspiration: https://imgflip.com/memetemplates?sort=top-new')
return data
}
const util = { const util = {
formatDate (date) { formatDate (date) {
return new Date(date).toLocaleDateString('en-US', { month: 'short', day: 'numeric' }) return new Date(date).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })
@ -188,7 +252,9 @@ const util = {
} }
assertSettings() assertSettings()
.then(fetchRecentBios) .then(fetchData)
.then(printIntro)
.then(populate) .then(populate)
.then(printTable) .then(printTable)
.then(printOutro)
.catch(console.error) .catch(console.error)