beccajharris commited on
Commit
501f369
·
verified ·
1 Parent(s): 8c5108c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -44
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: # Keeping the original function signature
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 not "." in handle:
25
  full_handle = f"{handle}.bsky.social"
26
  else:
27
  full_handle = handle
28
 
29
  try:
30
- # First, we need to use the public API that doesn't require authentication
31
- # This API endpoint lets us get public posts without authentication
32
- search_url = f"https://api.bsky.app/search/profiles?q={handle}"
33
- response = requests.get(search_url)
34
 
35
- if response.status_code != 200:
36
- return f"Error: Could not search for the profile. Status code: {response.status_code}"
37
 
38
- profiles = response.json()
39
- if not profiles or "profiles" not in profiles or len(profiles["profiles"]) == 0:
40
- return f"No Bluesky account found for @{handle}"
41
-
42
- # Find the matching profile
43
- matching_profile = None
44
- for profile in profiles["profiles"]:
45
- if profile["handle"].lower() == full_handle.lower():
46
- matching_profile = profile
47
- break
48
-
49
- if not matching_profile:
50
- return f"Could not find an exact match for @{handle}"
51
-
52
- # Use the profile's handle to get their feed through the public API
53
- feed_url = f"https://api.bsky.app/profile/{matching_profile['handle']}/feed"
54
- feed_response = requests.get(feed_url)
 
 
 
 
 
 
 
 
 
55
 
56
  if feed_response.status_code != 200:
57
- return f"Error: Could not retrieve posts. Status code: {feed_response.status_code}"
58
-
59
  feed_data = feed_response.json()
60
 
61
- # Look for the most recent post
62
- if not feed_data or "feed" not in feed_data or len(feed_data["feed"]) == 0:
63
- return f"@{handle} has no public posts on Bluesky"
64
-
65
- # Extract the latest post
66
  latest_post = feed_data["feed"][0]
67
- if "text" in latest_post:
68
- post_text = latest_post["text"]
69
- post_time = latest_post.get("indexedAt", "unknown time")
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