Skip to content

Commit 4262189

Browse files
committed
fix week interval in newsletter
1 parent 211e3c1 commit 4262189

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

scripts/newsletter.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const { ApolloClient, InMemoryCache, HttpLink, gql } = require('@apollo/client')
22
const { quote } = require('../lib/md.js')
3+
const { datePivot } = require('../lib/time.js')
34

45
const ITEMS = gql`
5-
query items ($sort: String, $when: String, $sub: String, $by: String) {
6-
items (sort: $sort, when: $when, sub: $sub, by: $by) {
6+
query items ($sort: String, $when: String, $sub: String, $by: String, $from: String, $to: String) {
7+
items (sort: $sort, when: $when, sub: $sub, by: $by, from: $from, to: $to) {
78
cursor
89
items {
910
id
@@ -70,8 +71,8 @@ const abbrNum = n => {
7071
}
7172

7273
const SEARCH = gql`
73-
query Search($q: String, $sort: String, $what: String, $when: String) {
74-
search(q: $q, sort: $sort, what: $what, when: $when) {
74+
query Search($q: String, $sort: String, $what: String, $when: String, $from: String, $to: String) {
75+
search(q: $q, sort: $sort, what: $what, when: $when, from: $from, to: $to) {
7576
items {
7677
id
7778
title
@@ -80,6 +81,9 @@ query Search($q: String, $sort: String, $what: String, $when: String) {
8081
}
8182
}`
8283

84+
const to = String(new Date(new Date().setHours(0, 0, 0, 0)).getTime())
85+
const from = String(datePivot(new Date(Number(to)), { days: -8 }).getTime())
86+
8387
async function bountyWinner (q) {
8488
const WINNER = gql`
8589
query Item($id: ID!) {
@@ -95,7 +99,7 @@ async function bountyWinner (q) {
9599

96100
const bounty = await client.query({
97101
query: SEARCH,
98-
variables: { q: `${q} @sn`, sort: 'recent', what: 'posts', when: 'week' }
102+
variables: { q: `${q} @sn`, sort: 'recent', what: 'posts', when: 'custom', from, to }
99103
})
100104

101105
const items = bounty.data.search.items.filter(i => i.bountyPaidTo?.length > 0)
@@ -115,7 +119,7 @@ async function bountyWinner (q) {
115119
}
116120
}
117121

118-
async function getTopUsers ({ by, cowboys = false, includeHidden = false, count = 5, when = 'week' } = {}) {
122+
async function getTopUsers ({ by, cowboys = false, includeHidden = false, count = 5, when = 'custom', from, to } = {}) {
119123
const accum = []
120124
let cursor = ''
121125
try {
@@ -127,7 +131,9 @@ async function getTopUsers ({ by, cowboys = false, includeHidden = false, count
127131
variables = {
128132
...variables,
129133
by,
130-
when
134+
when,
135+
from,
136+
to
131137
}
132138
}
133139
const result = await client.query({
@@ -146,17 +152,17 @@ async function getTopUsers ({ by, cowboys = false, includeHidden = false, count
146152
async function main () {
147153
const top = await client.query({
148154
query: ITEMS,
149-
variables: { sort: 'top', when: 'week' }
155+
variables: { sort: 'top', when: 'custom', from, to }
150156
})
151157

152158
const meta = await client.query({
153159
query: ITEMS,
154-
variables: { sort: 'top', when: 'week', sub: 'meta' }
160+
variables: { sort: 'top', when: 'custom', from, to, sub: 'meta' }
155161
})
156162

157163
const ama = await client.query({
158164
query: ITEMS,
159-
variables: { sort: 'top', when: 'week', sub: 'ama' }
165+
variables: { sort: 'top', when: 'custom', from, to, sub: 'ama' }
160166
})
161167

162168
const boosts = await client.query({
@@ -167,9 +173,9 @@ async function main () {
167173
const topMeme = await bountyWinner('meme monday')
168174
const topFact = await bountyWinner('fun fact')
169175

170-
const topCowboys = await getTopUsers({ cowboys: true })
171-
const topStackers = await getTopUsers({ by: 'stacking' })
172-
const topSpenders = await getTopUsers({ by: 'spent' })
176+
const topCowboys = await getTopUsers({ cowboys: true, when: 'custom', from, to })
177+
const topStackers = await getTopUsers({ by: 'stacking', when: 'custom', from, to })
178+
const topSpenders = await getTopUsers({ by: 'spent', when: 'custom', from, to })
173179

174180
process.stdout.write(
175181
`Happy Sat-urday Stackers,
@@ -186,7 +192,7 @@ ${ama.data.items.items.slice(0, 3).map((item, i) =>
186192
`${i + 1}. [${item.title}](https://stacker.news/items/${item.id})
187193
- ${abbrNum(item.sats)} sats${item.boost ? ` \\ ${abbrNum(item.boost)} boost` : ''} \\ ${item.ncomments} comments \\ [@${item.user.name}](https://stacker.news/${item.user.name})\n`).join('')}
188194
189-
[**all AMAs**](https://stacker.news/~meta/top/posts/forever)
195+
[**all of this week's AMAs**](https://stacker.news/~AMA/top/posts/week)
190196
191197
##### Don't miss
192198
${top.data.items.items.map((item, i) =>
@@ -200,18 +206,18 @@ ${top.data.items.items.map((item, i) =>
200206
${meta.data.items.items.slice(0, 10).map((item, i) =>
201207
`- [${item.title}](https://stacker.news/items/${item.id})\n`).join('')}
202208
203-
[**all meta**](https://stacker.news/~meta/top/posts/week)
209+
[**all of this week's meta**](https://stacker.news/~meta/top/posts/week)
204210
205211
-------
206212
207-
##### Top Monday meme \\ ${abbrNum(topMeme?.winner.sats)} sats \\ [@${topMeme?.winner.user.name}](https://stacker.news/${topMeme?.winner.user.name})
208-
![](${topMeme?.winner.image})
213+
##### Top Monday meme
214+
![](${new URL(topMeme?.winner.image, 'https://imgprxy.stacker.news').href})
209215
210216
[**all monday memes**](https://stacker.news/items/${topMeme?.bounty})
211217
212218
------
213219
214-
##### Top Friday fun fact \\ ${abbrNum(topFact?.winner.sats)} sats \\ [@${topFact?.winner.user.name}](https://stacker.news/${topFact?.winner.user.name})
220+
##### Top Friday fun fact
215221
${topFact && quote(topFact?.winner.text)}
216222
217223
[**all friday fun facts**](https://stacker.news/items/${topFact?.bounty})
@@ -246,7 +252,7 @@ ${boosts.data.items.items.map((item, i) =>
246252
: `${i + 1}. [${item.title.trim()}](https://stacker.news/items/${item.id})\n`
247253
).join('')}
248254
249-
[**all jobs**](https://stacker.news/~jobs)
255+
[**all active boosts**](https://stacker.news/top/boosts/month?by=boost)
250256
251257
------
252258

0 commit comments

Comments
 (0)