Last active
November 11, 2024 22:02
-
-
Save Debilski/9b5df92f1159024a7270ed457da91ca5 to your computer and use it in GitHub Desktop.
Add users from a file with Bluesky handles to a list that you own
This file contains hidden or 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
| import argparse | |
| import getpass | |
| import sys | |
| from atproto import Client | |
| parser = argparse.ArgumentParser( | |
| prog='add_to_list', | |
| description='Add handles from a textfile to a bluesky list') | |
| parser.add_argument('--user', required=True) | |
| parser.add_argument('--listname', required=True) | |
| parser.add_argument('--handlesfile', required=True) | |
| args = parser.parse_args() | |
| pw = getpass.getpass(prompt=f"Enter password for user ({args.user}): ") | |
| client = Client() | |
| client.login(args.user, pw) | |
| me_did = client.resolve_handle(args.user).did | |
| user_lists = client.app.bsky.graph.get_lists({ | |
| "actor": args.user, | |
| "limit": 30, | |
| }) | |
| for user_list in user_lists.lists: | |
| if user_list.name == args.listname: | |
| list_uri = user_list.uri | |
| break | |
| else: | |
| print(f"Could not find list with name {args.listname} for user {args.user}") | |
| sys.exit(1) | |
| with open(args.handlesfile) as file: | |
| handles_list = [line.rstrip().lstrip('@') for line in file] | |
| for handle in handles_list: | |
| if not handle: | |
| continue | |
| did = client.resolve_handle(handle).did | |
| print(f"Resolved handle {handle} to {did}") | |
| record={ | |
| "$type": "app.bsky.graph.listitem", | |
| "subject": did, | |
| "list": list_uri, | |
| "createdAt": client.get_current_time_iso(), | |
| } | |
| try: | |
| client.com.atproto.repo.create_record({'repo': me_did, 'collection': 'app.bsky.graph.listitem', 'record': record}) | |
| print(f"Added {handle} to {args.listname}") | |
| except Exception as e: | |
| print(f"Could not add {handle} to {args.listname}") | |
| print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment