Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,71 +9,77 @@ from Gradio_UI import GradioUI
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
-
def get_bluesky_post(arg: str) -> str:
|
| 13 |
-
"""Fetches the most recent post from a Bluesky account.
|
| 14 |
Args:
|
| 15 |
arg: The Bluesky handle without the domain (e.g., 'beccalewy')
|
| 16 |
Returns:
|
| 17 |
A string containing the most recent post content or an error message
|
| 18 |
"""
|
| 19 |
import requests
|
| 20 |
-
import json
|
| 21 |
|
| 22 |
# Format handle properly
|
| 23 |
-
handle = arg # Using arg as the handle
|
| 24 |
-
if
|
| 25 |
full_handle = f"{handle}.bsky.social"
|
| 26 |
else:
|
| 27 |
full_handle = handle
|
| 28 |
|
| 29 |
try:
|
| 30 |
-
#
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
return f"Error: Could not search for the profile. Status code: {response.status_code}"
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
if feed_response.status_code != 200:
|
| 57 |
-
return f"
|
| 58 |
-
|
| 59 |
feed_data = feed_response.json()
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
if not feed_data
|
| 63 |
-
return f"@{handle} has no
|
| 64 |
-
|
| 65 |
-
# Extract
|
| 66 |
latest_post = feed_data["feed"][0]
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
# Format the result
|
| 72 |
-
result = f"{matching_profile.get('displayName', handle)} (@{handle}) posted on {post_time}:\n\"{post_text}\""
|
| 73 |
-
return result
|
| 74 |
-
else:
|
| 75 |
-
return f"Could not extract text from the latest post by @{handle}"
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
return f"Error fetching Bluesky post: {str(e)}"
|
| 79 |
|
|
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
+
def get_bluesky_post(arg: str) -> str:
|
| 13 |
+
"""Fetches the most recent post from a Bluesky account.
|
| 14 |
Args:
|
| 15 |
arg: The Bluesky handle without the domain (e.g., 'beccalewy')
|
| 16 |
Returns:
|
| 17 |
A string containing the most recent post content or an error message
|
| 18 |
"""
|
| 19 |
import requests
|
|
|
|
| 20 |
|
| 21 |
# Format handle properly
|
| 22 |
+
handle = arg.strip() # Using arg as the handle and removing whitespace
|
| 23 |
+
if "." not in handle:
|
| 24 |
full_handle = f"{handle}.bsky.social"
|
| 25 |
else:
|
| 26 |
full_handle = handle
|
| 27 |
|
| 28 |
try:
|
| 29 |
+
# Step 1: Use the public API endpoint to resolve the handle to a DID
|
| 30 |
+
# As per docs: public endpoints can be called directly against https://public.api.bsky.app
|
| 31 |
+
resolver_url = "https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle"
|
| 32 |
+
resolver_params = {"handle": full_handle}
|
| 33 |
|
| 34 |
+
resolver_response = requests.get(resolver_url, params=resolver_params)
|
|
|
|
| 35 |
|
| 36 |
+
if resolver_response.status_code != 200:
|
| 37 |
+
return f"Could not resolve handle @{handle}. Status code: {resolver_response.status_code}"
|
| 38 |
+
|
| 39 |
+
resolver_data = resolver_response.json()
|
| 40 |
+
user_did = resolver_data.get("did")
|
| 41 |
+
|
| 42 |
+
if not user_did:
|
| 43 |
+
return f"Could not find DID for Bluesky account @{handle}"
|
| 44 |
+
|
| 45 |
+
# Step 2: Get the profile information
|
| 46 |
+
profile_url = "https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile"
|
| 47 |
+
profile_params = {"actor": user_did}
|
| 48 |
+
|
| 49 |
+
profile_response = requests.get(profile_url, params=profile_params)
|
| 50 |
+
|
| 51 |
+
if profile_response.status_code != 200:
|
| 52 |
+
return f"Could not fetch profile for @{handle}. Status code: {profile_response.status_code}"
|
| 53 |
+
|
| 54 |
+
profile_data = profile_response.json()
|
| 55 |
+
display_name = profile_data.get("displayName", handle)
|
| 56 |
+
|
| 57 |
+
# Step 3: Get the author's feed
|
| 58 |
+
feed_url = "https://public.api.bsky.app/xrpc/app.bsky.feed.getAuthorFeed"
|
| 59 |
+
feed_params = {"actor": user_did, "limit": 1}
|
| 60 |
+
|
| 61 |
+
feed_response = requests.get(feed_url, params=feed_params)
|
| 62 |
|
| 63 |
if feed_response.status_code != 200:
|
| 64 |
+
return f"Could not fetch posts for @{handle}. Status code: {feed_response.status_code}"
|
| 65 |
+
|
| 66 |
feed_data = feed_response.json()
|
| 67 |
|
| 68 |
+
# Check if there are any posts
|
| 69 |
+
if not feed_data.get("feed") or len(feed_data["feed"]) == 0:
|
| 70 |
+
return f"@{handle} has no posts on Bluesky"
|
| 71 |
+
|
| 72 |
+
# Extract post information
|
| 73 |
latest_post = feed_data["feed"][0]
|
| 74 |
+
post = latest_post.get("post", {})
|
| 75 |
+
record = post.get("record", {})
|
| 76 |
+
post_text = record.get("text", "")
|
| 77 |
+
post_time = record.get("createdAt", "unknown time")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# Format the result for the AI agent
|
| 80 |
+
result = f"{display_name} (@{handle}) posted on {post_time}:\n\"{post_text}\""
|
| 81 |
+
return result
|
| 82 |
+
|
| 83 |
except Exception as e:
|
| 84 |
return f"Error fetching Bluesky post: {str(e)}"
|
| 85 |
|