Last active
January 9, 2025 16:30
-
-
Save jargnar/66c19fa5b4c90fab43241959520df21a to your computer and use it in GitHub Desktop.
Bluesky: Add all users followed by a particular user to a list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from atproto import Client, models | |
from datetime import datetime | |
import time | |
# ============================= | |
# -- Update these constants -- | |
# ============================= | |
BSKY_HANDLE = '' # Your handle | |
BSKY_PASSWORD = '' # Your password or app password | |
LIST_NAME = '' # Name of the existing custom list | |
TARGET_USER = '' # The user whose follows you're copying | |
def find_list_by_name(client, list_name): | |
# Get all your lists | |
params = {"actor": client.me.did} | |
response = client.app.bsky.graph.get_lists(params) | |
# Find the list with matching name | |
for lst in response.lists: | |
if lst.name.lower() == list_name.lower(): | |
return lst.uri | |
raise Exception(f"List '{list_name}' not found") | |
def main(): | |
# 1. Login | |
client = Client() | |
client.login(BSKY_HANDLE, BSKY_PASSWORD) | |
print("Logged in successfully") | |
# 2. Find your list URI | |
list_uri = find_list_by_name(client, LIST_NAME) | |
print(f"Found list URI: {list_uri}") | |
# 3. Get target user's DID | |
target_profile = client.get_profile(TARGET_USER) | |
target_did = target_profile.did | |
print(f"Found target user DID: {target_did}") | |
# 4. Fetch all follows | |
cursor = None | |
follows = [] | |
while True: | |
response = client.get_follows(target_did, cursor=cursor) | |
follows.extend(response.follows) | |
if not response.cursor: | |
break | |
cursor = response.cursor | |
time.sleep(0.5) # Rate limiting precaution | |
print(f"Found {len(follows)} follows") | |
# 5. Add each user to the list | |
for follow in follows: | |
try: | |
# Create the record as a dictionary | |
record = { | |
"$type": "app.bsky.graph.listitem", | |
"subject": follow.did, | |
"list": list_uri, | |
"createdAt": datetime.utcnow().isoformat() + "Z" | |
} | |
# Create the record on the repository | |
client.com.atproto.repo.create_record({ | |
"repo": client.me.did, | |
"collection": "app.bsky.graph.listitem", | |
"record": record | |
}) | |
print(f"Added {follow.handle} to list") | |
time.sleep(0.5) # Rate limiting precaution | |
except Exception as e: | |
print(f"Failed to add {follow.handle}: {str(e)}") | |
print("Finished adding users to list") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment