-
Notifications
You must be signed in to change notification settings - Fork 543
/
publishFeedGen.ts
102 lines (92 loc) · 2.77 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
91
92
93
94
95
96
97
98
99
100
101
102
import dotenv from 'dotenv'
import inquirer from 'inquirer'
import { AtpAgent, BlobRef } from '@atproto/api'
import fs from 'fs/promises'
import { ids } from '../src/lexicon/lexicons'
const run = async () => {
dotenv.config()
if (!process.env.FEEDGEN_SERVICE_DID && !process.env.FEEDGEN_HOSTNAME) {
throw new Error('Please provide a hostname in the .env file')
}
const answers = await inquirer
.prompt([
{
type: 'input',
name: 'handle',
message: 'Enter your Bluesky handle:',
required: true,
},
{
type: 'password',
name: 'password',
message: 'Enter your Bluesky password (preferably an App Password):',
},
{
type: 'input',
name: 'service',
message: 'Optionally, enter a custom PDS service to sign in with:',
default: 'https://bsky.social',
required: false,
},
{
type: 'input',
name: 'recordName',
message: 'Enter a short name or the record. This will be shown in the feed\'s URL:',
required: true,
},
{
type: 'input',
name: 'displayName',
message: 'Enter a display name for your feed:',
required: true,
},
{
type: 'input',
name: 'description',
message: 'Optionally, enter a brief description of your feed:',
required: false,
},
{
type: 'input',
name: 'avatar',
message: 'Optionally, enter a local path to an avatar that will be used for the feed:',
required: false,
},
])
const { handle, password, recordName, displayName, description, avatar, service } = answers
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: service ? service : 'https://bsky.social' })
await agent.login({ identifier: handle, password})
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()