Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update publishFeedGen.ts #128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 51 additions & 93 deletions scripts/publishFeedGen.ts
Original file line number Diff line number Diff line change
@@ -1,102 +1,60 @@
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')
}
import { subscribeToFirehose } from './firehose';

export async function indexPosts() {
const subscription = await subscribeToFirehose();
subscription.on('post', (post) => {
const text = post.text.toLowerCase();
if (
text.includes('california native plants') ||
text.includes('ceanothus') ||
text.includes('manzanita') ||
text.includes('#californianatives') ||
text.includes('#nativeplants')
) {
saveToDatabase(post); // Save posts matching criteria
}
});
}

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,
},
])
import { SkeletonFeedPost } from '../types';
import { getIndexedPosts } from '../database'; // Replace with your actual database utility

export async function californiaNativePlantsFeed(): Promise<SkeletonFeedPost[]> {
const posts = await getIndexedPosts(); // Fetch posts from the database
const filteredPosts = posts.filter(post => {
const text = post.text.toLowerCase();
return (
text.includes('california native plants') ||
text.includes('ceanothus') ||
text.includes('manzanita') ||
text.includes('#californianatives') ||
text.includes('#nativeplants')
);
});

return filteredPosts.map(post => ({ post: post.uri }));
}

const { handle, password, recordName, displayName, description, avatar, service } = answers
import { californiaNativePlantsFeed } from './algos/californiaNativePlants';

const feedGenDid =
process.env.FEEDGEN_SERVICE_DID ?? `did:web:${process.env.FEEDGEN_HOSTNAME}`
feeds['at://did:your-did/app.bsky.feed.generator/California-native-plants'] = CaliforniaNativePlantsFeed;

// only update this if in a test environment
const agent = new AtpAgent({ service: service ? service : 'https://bsky.social' })
await agent.login({ identifier: handle, password})
const feedMetadata = {
name: 'California Native Plants',
avatar: 'https://example.com/path-to-avatar.png', // Optional avatar URL
description: 'A feed featuring posts about California native plants.',
feedUri: 'at://did:your-did/app.bsky.feed.generator/california-native-plants',
serviceDid: 'did:web:your-domain.com'
};

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
}
const feedMetadata = {
name: 'California Native Plants',
avatar: 'https://example.com/path-to-avatar.png', // Optional avatar URL
description: 'A feed featuring posts about California native plants.',
feedUri: 'at://did:your-did/app.bsky.feed.generator/california-native-plants',
serviceDid: 'did:web:your-domain.com'
};

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()