Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from atproto import Client
- from collections import Counter
- import time
- # Initialize the AT Protocol client
- client = Client()
- # Replace with your Bluesky username and password
- USERNAME = "YOURBKSYHANDLE"
- PASSWORD = "YOURPASSWORD"
- # at_client.login('YOURBKSYHANDLE', 'YOURPASSWORD')
- REQUEST_LIMIT = 50 # Maximum number of requests before delay
- DELAY_SECONDS = 10 # Delay duration in seconds
- # Step 1: Authenticate and login
- client.login(USERNAME, PASSWORD)
- def get_following_dids(user_handle: str):
- """
- Retrieve a list of DIDs (decentralized identifiers) for the accounts that the user is following.
- """
- following_dids = []
- cursor = None
- request_count = 0 # Initialize request counter
- while True:
- # Fetch the list of accounts the user is following
- response = client.get_follows(actor=user_handle, cursor=cursor, limit=10)
- following_dids.extend([follow.did for follow in response.follows])
- # Pagination support
- if not response.cursor:
- break
- cursor = response.cursor
- # Increment request count and check if limit is reached
- request_count += 1
- if request_count >= REQUEST_LIMIT:
- print(f"Reached {REQUEST_LIMIT} requests, waiting {DELAY_SECONDS} seconds...")
- time.sleep(DELAY_SECONDS)
- request_count = 0 # Reset request count after delay
- return following_dids
- def get_handle_from_did(did: str, request_count):
- """
- Retrieve the handle for a given DID.
- """
- try:
- profile = client.get_profile(did)
- return profile.handle, request_count + 1 # Return updated request count
- except Exception as e:
- print(f"Error fetching handle for {did}: {e}")
- return None, request_count
- def get_common_following_handles(your_handle: str):
- """
- Find the handles of accounts followed by at least 3 accounts you follow.
- """
- # Step 2: Get DIDs of accounts you are following
- your_following_dids = get_following_dids(your_handle)
- # Step 3: Retrieve following lists for each followed account
- all_followings = []
- request_count = 0
- for did in your_following_dids:
- try:
- following = get_following_dids(did)
- all_followings.extend(following)
- request_count += 1
- if request_count >= REQUEST_LIMIT:
- print(f"Reached {REQUEST_LIMIT} requests, waiting {DELAY_SECONDS} seconds...")
- time.sleep(DELAY_SECONDS)
- request_count = 0 # Reset request count after delay
- except Exception as e:
- print(f"Error fetching following for {did}: {e}")
- # Step 4: Count occurrences to find commonly followed accounts
- following_counts = Counter(all_followings)
- common_following_dids = [did for did, count in following_counts.items() if count >= 3]
- # Step 5: Convert DIDs to handles
- common_following_handles = []
- for did in common_following_dids:
- handle, request_count = get_handle_from_did(did, request_count)
- if handle:
- common_following_handles.append(handle)
- # Check and enforce delay if request limit reached
- if request_count >= REQUEST_LIMIT:
- print(f"Reached {REQUEST_LIMIT} requests, waiting {DELAY_SECONDS} seconds...")
- time.sleep(DELAY_SECONDS)
- request_count = 0
- return common_following_handles
- # Usage
- your_handle = "your_handle" # Replace with your Bluesky handle (e.g., "yourhandle.bsky.social")
- common_accounts = get_common_following_handles(USERNAME)
- print("Common followed accounts (handles, followed by at least 3 accounts you follow):", common_accounts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement