Advertisement
msmouse

Untitled

Mar 13th, 2025
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from mastodon import Mastodon
  4. import random
  5.  
  6. # Configuration: Replace these with appropriate values
  7. INSTANCE_URL = "https://mastodon.example"  # Replace with your Mastodon instance
  8. ACCESS_TOKEN = "your_access_token"  # Replace with a valid access token
  9. USERNAME = "target_username"  # Replace with the username whose follows you want to fetch
  10. NUM_TO_PRINT = 5  # Adjust to the desired number of random accounts to print
  11.  
  12. def get_random_follows(instance_url, access_token, username, num_to_print):
  13.     """Fetch the list of accounts followed by the specified user, shuffle, and return random subset."""
  14.     mastodon = Mastodon(access_token=access_token, api_base_url=instance_url)
  15.  
  16.     # Get the account ID for the username
  17.     accounts = mastodon.account_search(username, limit=1)
  18.     if not accounts:
  19.         print(f"User '{username}' not found.")
  20.         return []
  21.  
  22.     user_id = accounts[0]['id']
  23.  
  24.     # Get the accounts the user follows
  25.     follows = mastodon.account_following(user_id)
  26.  
  27.     if not follows:
  28.         print("No follows found.")
  29.         return []
  30.  
  31.     # Extract timeline URLs
  32.     timeline_urls = [f"{instance_url}/@{account['acct']}" for account in follows]
  33.  
  34.     # Shuffle and select a random subset
  35.     random.shuffle(timeline_urls)
  36.     selected_urls = timeline_urls[:num_to_print]
  37.  
  38.     return selected_urls
  39.  
  40. if __name__ == "__main__":
  41.     random_timelines = get_random_follows(INSTANCE_URL, ACCESS_TOKEN, USERNAME, NUM_TO_PRINT)
  42.  
  43.     if random_timelines:
  44.         print(f"Random {NUM_TO_PRINT} Follower Timelines:")
  45.         for url in random_timelines:
  46.             print(url)
  47.     else:
  48.         print("No results found or an error occurred.")
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement