forked from bluesky-social/feed-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publishFeedGen.ts
90 lines (75 loc) · 2.64 KB
/
publishFeedGen.ts
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
import dotenv from 'dotenv'
import { AtpAgent, BlobRef } from '@atproto/api'
import fs from 'fs/promises'
import { ids } from '../src/lexicon/lexicons'
const run = async () => {
dotenv.config()
// YOUR bluesky handle
// Ex: user.bsky.social
const handle = 'luten.dev'
// YOUR bluesky password, or preferably an App Password (found in your client settings)
// Ex: abcd-1234-efgh-5678
const password = process.env.BSKY_PASSWORD ?? ''
// A short name for the record that will show in urls
// Lowercase with no spaces.
// Ex: whats-hot
const recordName = 'ttrpg'
// A display name for your feed
// Ex: What's Hot
const displayName = 'TTRPG'
// (Optional) A description of your feed
// Ex: Top trending content from the whole network
const description =
"A complete feed of Dungeons & Dragons, Pathfinder, Critical Role, and all other TTRPG posts! Want to be removed or added? Ping @luten.dev or open a Pull Request (yes, it's open source!)"
// (Optional) The path to an image to be used as your feed's avatar
// Ex: ~/path/to/avatar.jpeg
const avatar: string = './d20.png'
// -------------------------------------
// NO NEED TO TOUCH ANYTHING BELOW HERE
// -------------------------------------
if (!process.env.FEEDGEN_SERVICE_DID && !process.env.FEEDGEN_HOSTNAME) {
throw new Error('Please provide a hostname in the .env file')
}
const feedGenDid =
process.env.FEEDGEN_SERVICE_DID ?? `did:web:${process.env.FEEDGEN_HOSTNAME}`
// only update this if in a test environment
const agent = new AtpAgent({ service: 'https://bsky.network' })
await agent.login({ identifier: handle, password })
try {
await agent.api.app.bsky.feed.describeFeedGenerator()
} catch (err) {
throw new Error(
'The bluesky server is not ready to accept published custom feeds yet',
)
}
let avatarRef: BlobRef | undefined
if (avatar) {
let encoding: string
if (avatar.endsWith('png')) {
encoding = 'image/png'
} else if (avatar.endsWith('jpg') || avatar.endsWith('jpeg')) {
encoding = 'image/jpeg'
} else {
throw new Error('expected png or jpeg')
}
const img = await fs.readFile(avatar)
const blobRes = await agent.api.com.atproto.repo.uploadBlob(img, {
encoding,
})
avatarRef = blobRes.data.blob
}
await agent.api.com.atproto.repo.putRecord({
repo: agent.session?.did ?? '',
collection: ids.AppBskyFeedGenerator,
rkey: recordName,
record: {
did: feedGenDid,
displayName: displayName,
description: description,
avatar: avatarRef,
createdAt: new Date().toISOString(),
},
})
console.log('All done 🎉')
}
run()