kesma

save_bsky_list.py

Dec 17th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import requests
  2. import json
  3. import os
  4.  
  5. # Get username and app password from environment variables
  6. USERNAME = os.getenv('BLUESKY_USERNAME')
  7. APP_PASSWORD = os.getenv('BLUESKY_PASSWORD')
  8.  
  9. # Replace with your actual list ID and output filename
  10. # CSS
  11. # LIST_ID = 'at://did:plc:rprxe2jmbftg3ajamqhrgxji/app.bsky.graph.list/3ka7ledivcx2c'
  12.  
  13. # Kraft
  14. LIST_ID = 'at://did:plc:h62svfxjcrjpprijpmfeud56/app.bsky.graph.list/3keziseo6ta24'
  15.  
  16. OUTPUT_FILE = 'MK_members_output.json'
  17.  
  18. # API base URL
  19. API_URL = 'https://bsky.social/xrpc/app.bsky.graph.getList'
  20.  
  21. def get_access_token(username, app_password):
  22. response = requests.post(
  23. 'https://bsky.social/xrpc/com.atproto.server.createSession',
  24. json={'identifier': username, 'password': app_password}
  25. )
  26. if response.status_code == 200:
  27. access_token = response.json().get('accessJwt')
  28. did = response.json().get('did')
  29. return access_token, did
  30. else:
  31. print(f"Failed to authenticate: {response.status_code} - {response.text}")
  32. return None, None
  33.  
  34. def fetch_and_save_list_members(list_id, output_file, headers):
  35. total_count = 0
  36. cursor = None
  37. params = {
  38. 'list': list_id,
  39. 'limit': 100, # Adjust the limit as per API's maximum allowed
  40. }
  41. all_members = []
  42.  
  43. while True:
  44. if cursor:
  45. params['cursor'] = cursor
  46.  
  47. response = requests.get(API_URL, headers=headers, params=params)
  48.  
  49. if response.status_code != 200:
  50. print(f"Error: {response.status_code} - {response.text}")
  51. break
  52.  
  53. data = response.json()
  54. items = data.get('items', [])
  55. total_count += len(items)
  56. all_members.extend(items)
  57.  
  58. print(f"Fetched {len(items)} members. Total so far: {total_count}")
  59.  
  60. # Check if there's a next page
  61. cursor = data.get('cursor')
  62. if not cursor:
  63. break
  64.  
  65. # Save all members to the specified output file
  66. with open(output_file, 'w') as f:
  67. json.dump(all_members, f, indent=4)
  68. print(f"Total members saved to {output_file}")
  69.  
  70. if __name__ == '__main__':
  71. access_token, user_did = get_access_token(USERNAME, APP_PASSWORD)
  72. if access_token:
  73. headers = {
  74. 'Authorization': f'Bearer {access_token}',
  75. }
  76. fetch_and_save_list_members(LIST_ID, OUTPUT_FILE, headers)
  77. else:
  78. print("Access token not obtained.")
  79.  
Advertisement
Add Comment
Please, Sign In to add comment