-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (68 loc) · 2.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import assert from 'assert'
import {asyncFilter, asyncIterToArray, asyncSlice} from 'iter-tools'
import Mastodon from '@lagunehq/core'
const {MRB_INSTANCE_URL, MRB_ACCESS_TOKEN, MRB_STARTING_FROM} = process.env
assert(MRB_INSTANCE_URL, 'MRB_INSTANCE_URL environment var is required')
assert(MRB_ACCESS_TOKEN, 'MWB_ACCESS_TOKEN environment var is required')
assert(MRB_STARTING_FROM, 'MRB_STARTING_FROM environment var is required')
const MAXIMUM_FETCH_REQUESTS = 290
const MAXIMUM_REBLOG_REQUESTS = 3
main()
async function main() {
const client = new Mastodon()
client.setUrl(MRB_INSTANCE_URL)
client.setToken(MRB_ACCESS_TOKEN)
console.log(`Fetching statuses... (${MAXIMUM_FETCH_REQUESTS} requests maximum)`)
const favouritedStatuses = await (
fetchStatusesPages(client)
|> asyncSlice(MAXIMUM_FETCH_REQUESTS)
|> asyncFlatten
|> asyncFilter(status => status.visibility === 'public' && status.favourites_count >= 2)
|> asyncIterToArray
)
if (favouritedStatuses.length) {
console.log(`Fetched ${favouritedStatuses.length} favourited statuses.\n`)
// Threshold used to found interesting statuses to reblog.
// Calculated from all fetched favourited statuses favourites count average
const favouritesCountThreshold =
favouritedStatuses.reduce((acc, status) => acc + status.favourites_count, 0) / favouritedStatuses.length
const createdAtThreshold = Date.now() - (1000 * 60 * 60 * 24)
console.log(`Favourites count threshold is ${favouritesCountThreshold}.`)
console.log(`Created at threshold is ${createdAtThreshold.toString()}.\n`)
const interestingStatuses = favouritedStatuses
.filter(status => {
const createdAt = new Date(status.created_at)
return !status.reblogged
&& createdAt >= MRB_STARTING_FROM
&& status.favourites_count >= favouritesCountThreshold
&& createdAt >= createdAtThreshold
})
console.log(`Found ${interestingStatuses.length} interesting statuses.\n`)
console.log(`Reblog interesting statuses... (${MAXIMUM_REBLOG_REQUESTS} requests maximum)`)
await Promise.all(
interestingStatuses
.reverse()
.slice(0, MAXIMUM_REBLOG_REQUESTS)
.map(status => client.reblogStatus(status.id))
)
console.log(`Done !`)
} else {
console.log(`Not enough favourited statuses.`)
}
}
async function* fetchStatusesPages(client, maxId = null) {
const statuses = await client.fetchPublicTimeline({
limit: 40,
max_id: maxId
})
yield statuses
const [lastStatus] = statuses.slice(-1)
if (lastStatus) {
yield* fetchStatusesPages(client, lastStatus.id)
}
}
async function* asyncFlatten(xs) {
for await (const x of xs) {
yield* x
}
}