Advertisement
Guest User

Untitled

a guest
Nov 15th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. from atproto import Client
  2. from collections import Counter
  3. import time
  4.  
  5. # Initialize the AT Protocol client
  6. client = Client()
  7.  
  8. # Replace with your Bluesky username and password
  9. USERNAME = "YOURBKSYHANDLE"
  10. PASSWORD = "YOURPASSWORD"
  11. # at_client.login('YOURBKSYHANDLE', 'YOURPASSWORD')
  12. REQUEST_LIMIT = 50 # Maximum number of requests before delay
  13. DELAY_SECONDS = 10 # Delay duration in seconds
  14.  
  15. # Step 1: Authenticate and login
  16. client.login(USERNAME, PASSWORD)
  17.  
  18. def get_following_dids(user_handle: str):
  19. """
  20. Retrieve a list of DIDs (decentralized identifiers) for the accounts that the user is following.
  21. """
  22. following_dids = []
  23. cursor = None
  24. request_count = 0 # Initialize request counter
  25.  
  26. while True:
  27. # Fetch the list of accounts the user is following
  28. response = client.get_follows(actor=user_handle, cursor=cursor, limit=10)
  29. following_dids.extend([follow.did for follow in response.follows])
  30.  
  31. # Pagination support
  32. if not response.cursor:
  33. break
  34. cursor = response.cursor
  35.  
  36. # Increment request count and check if limit is reached
  37. request_count += 1
  38. if request_count >= REQUEST_LIMIT:
  39. print(f"Reached {REQUEST_LIMIT} requests, waiting {DELAY_SECONDS} seconds...")
  40. time.sleep(DELAY_SECONDS)
  41. request_count = 0 # Reset request count after delay
  42.  
  43. return following_dids
  44.  
  45. def get_handle_from_did(did: str, request_count):
  46. """
  47. Retrieve the handle for a given DID.
  48. """
  49. try:
  50. profile = client.get_profile(did)
  51. return profile.handle, request_count + 1 # Return updated request count
  52. except Exception as e:
  53. print(f"Error fetching handle for {did}: {e}")
  54. return None, request_count
  55.  
  56. def get_common_following_handles(your_handle: str):
  57. """
  58. Find the handles of accounts followed by at least 3 accounts you follow.
  59. """
  60. # Step 2: Get DIDs of accounts you are following
  61. your_following_dids = get_following_dids(your_handle)
  62.  
  63. # Step 3: Retrieve following lists for each followed account
  64. all_followings = []
  65. request_count = 0
  66.  
  67. for did in your_following_dids:
  68. try:
  69. following = get_following_dids(did)
  70. all_followings.extend(following)
  71. request_count += 1
  72. if request_count >= REQUEST_LIMIT:
  73. print(f"Reached {REQUEST_LIMIT} requests, waiting {DELAY_SECONDS} seconds...")
  74. time.sleep(DELAY_SECONDS)
  75. request_count = 0 # Reset request count after delay
  76. except Exception as e:
  77. print(f"Error fetching following for {did}: {e}")
  78.  
  79. # Step 4: Count occurrences to find commonly followed accounts
  80. following_counts = Counter(all_followings)
  81. common_following_dids = [did for did, count in following_counts.items() if count >= 3]
  82.  
  83. # Step 5: Convert DIDs to handles
  84. common_following_handles = []
  85. for did in common_following_dids:
  86. handle, request_count = get_handle_from_did(did, request_count)
  87. if handle:
  88. common_following_handles.append(handle)
  89.  
  90. # Check and enforce delay if request limit reached
  91. if request_count >= REQUEST_LIMIT:
  92. print(f"Reached {REQUEST_LIMIT} requests, waiting {DELAY_SECONDS} seconds...")
  93. time.sleep(DELAY_SECONDS)
  94. request_count = 0
  95.  
  96. return common_following_handles
  97.  
  98. # Usage
  99. your_handle = "your_handle" # Replace with your Bluesky handle (e.g., "yourhandle.bsky.social")
  100. common_accounts = get_common_following_handles(USERNAME)
  101. 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