r/learnpython • u/aaronamethyst • 1d ago
Random bluesky post code, but it needs help with efficiency
I had a program made that takes all my bluesky posts, and then spits one of them back randomly at me. However, I want it to actually cache all my bluesky posts, and ask something like "Would you like to refresh your cache" which would go back through my posts, grabbing all the new ones too.
How do I make it store what it's pulled and just run a random pick from that index?
import requests
import random
import time
BASE_URL = "https://public.api.bsky.app/xrpc/app.bsky.feed.getAuthorFeed"
username = "---.bsky.social"
feed = []
cursor = None
page = 1
while True:
params = {
"actor": username,
"limit": 100
}
if cursor:
params["cursor"] = cursor
print(f"Downloading page {page}...")
response = requests.get(BASE_URL, params=params)
if response.status_code != 200:
print("Error:", response.status_code)
print(response.text)
break
data = response.json()
items = data.get("feed", [])
if not items:
break
feed.extend(items)
cursor = data.get("cursor")
if not cursor:
break
page += 1
# Be nice to the API
time.sleep(0.2)
print(f"\nDownloaded {len(feed)} feed items.")
if not feed:
quit()
chosen = random.choice(feed)
post = chosen["post"]
record = post.get("record", {})
text = record.get("text", "(No text)")
author = post["author"]["displayName"]
handle = post["author"]["handle"]
print("\n============================")
print("Random Feed Item")
print("============================")
print(f"Author : {author} (@{handle})")
print()
if "reason" in chosen:
print("This is a REPOST")
print("Reposted by:", chosen["reason"]["by"]["displayName"])
print()
print(text)
print("\nURI:", post["uri"])
uri = post["uri"]
parts = uri.split("/")
did = parts[2]
postid = parts[4]
print(f"https://bsky.app/profile/{did}/post/{postid}")import requests
import random
import time
BASE_URL = "https://public.api.bsky.app/xrpc/app.bsky.feed.getAuthorFeed"
username = "aaronamethyst.bsky.social"
feed = []
cursor = None
page = 1
while True:
params = {
"actor": username,
"limit": 100
}
if cursor:
params["cursor"] = cursor
print(f"Downloading page {page}...")
response = requests.get(BASE_URL, params=params)
if response.status_code != 200:
print("Error:", response.status_code)
print(response.text)
break
data = response.json()
items = data.get("feed", [])
if not items:
break
feed.extend(items)
cursor = data.get("cursor")
if not cursor:
break
page += 1
# Be nice to the API
time.sleep(0.2)
print(f"\nDownloaded {len(feed)} feed items.")
if not feed:
quit()
chosen = random.choice(feed)
post = chosen["post"]
record = post.get("record", {})
text = record.get("text", "(No text)")
author = post["author"]["displayName"]
handle = post["author"]["handle"]
print("\n============================")
print("Random Feed Item")
print("============================")
print(f"Author : {author} (@{handle})")
print()
if "reason" in chosen:
print("This is a REPOST")
print("Reposted by:", chosen["reason"]["by"]["displayName"])
print()
print(text)
print("\nURI:", post["uri"])
uri = post["uri"]
parts = uri.split("/")
did = parts[2]
postid = parts[4]
print(f"https://bsky.app/profile/{did}/post/{postid}")
0
Upvotes
1
0
1
u/NorskJesus 1d ago
You could store the items into a file for example and read from that.