Skip to content

Instantly share code, notes, and snippets.

@1a35e1
Created October 24, 2024 10:39
Show Gist options
  • Save 1a35e1/1839f7492c95e5b010288d96c0ed7f09 to your computer and use it in GitHub Desktop.
Save 1a35e1/1839f7492c95e5b010288d96c0ed7f09 to your computer and use it in GitHub Desktop.
Farcaster; Extract channel members
import { createObjectCsvWriter } from 'csv-writer';
const API_KEY = process.env.NEYNAR_API_KEY!;
const channelId = "daos"; // Replace with your actual channel ID
const limit = 50;
let cursor: string | null = null;
/**
* @usage NEYNAR_API_KEY=<YOUR_API_KEY> npx ts-node --transpile-only index.ts
*/
async function fetchMembers() {
const results = []; // Array to store all fetched members
do {
try {
const url = `https://api.neynar.com/v2/farcaster/channel/member/list?channel_id=${channelId}&limit=${limit}${cursor ? `&cursor=${cursor}` : ''}`;
const options = {
method: 'GET',
headers: { accept: 'application/json', api_key: API_KEY }
};
const data = await fetch(url, options)
.then(res => res.json())
.catch(err => console.error(err));
results.push(...data.members.map((member: any) => {
const verified_addresses = member.user.verified_addresses?.eth_addresses?.join(',');
const x = member.user.verified_accounts?.find((o: any) => o.platform === 'x')?.username;
return {
fid: member.user.fid,
username: member.user.username,
display_name: member.user.display_name,
custody_address: member.user.custody_address,
bio: member.user.profile.bio.text.replace(/"/g, '""').replace(/\n/g, '\\n'),
follower_count: member.user.follower_count,
following_count: member.user.following_count,
verified_addresses,
x: x ? x.replace(/"/g, '""').replace(/\n/g, '\\n') : null,
hasPowerBadge: member.user.power_badge,
};
}));
cursor = data.next.cursor;
} catch (err) {
console.error(err);
break; // Exit loop on error
}
} while (cursor); // Continue until there are no more pages
console.log(results.length);
console.log(JSON.stringify(results, null, 2));
// Write results to a CSV file
const csvWriter = createObjectCsvWriter({
path: `${channelId}-members.csv`, // Specify the output CSV file path
alwaysQuote: true,
header: [
{ id: 'fid', title: 'FID' },
{ id: 'username', title: 'Username' },
{ id: 'display_name', title: 'Display Name' },
{ id: 'custody_address', title: 'Custody Address' },
{ id: 'bio', title: 'Bio' },
{ id: 'follower_count', title: 'Follower Count' },
{ id: 'following_count', title: 'Following Count' },
{ id: 'verified_addresses', title: 'Verified Addresses' },
{ id: 'x', title: 'X' },
{ id: 'hasPowerBadge', title: 'Has Power Badge' },
],
});
await csvWriter.writeRecords(results); // Write records to CSV
console.log(`${results.length} members written to ${channelId}-members.csv`);
}
fetchMembers();
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment