beccajharris commited on
Commit
882af33
·
verified ·
1 Parent(s): 501f369

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -5
app.py CHANGED
@@ -12,29 +12,45 @@ from Gradio_UI import GradioUI
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")
 
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 (e.g., 'beccalewy' or 'resistrebelrevolt.net')
16
  Returns:
17
  A string containing the most recent post content or an error message
18
  """
19
  import requests
20
 
21
+ # Clean up the handle (remove any extra spaces)
22
+ handle = arg.strip()
23
+
24
+ # Don't automatically append .bsky.social if the handle already has a domain
25
+ # (like resistrebelrevolt.net)
26
  if "." not in handle:
27
  full_handle = f"{handle}.bsky.social"
28
  else:
29
  full_handle = handle
30
 
31
  try:
32
+ print(f"Trying to resolve handle: {full_handle}")
33
+
34
  # Step 1: Use the public API endpoint to resolve the handle to a DID
 
35
  resolver_url = "https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle"
36
  resolver_params = {"handle": full_handle}
37
 
38
  resolver_response = requests.get(resolver_url, params=resolver_params)
39
 
40
  if resolver_response.status_code != 200:
41
+ # Try alternate formats if the first attempt fails
42
+ if "." in handle:
43
+ # Maybe they included the domain but not in the right format
44
+ alternate_handle = handle # Original handle without modification
45
+ print(f"First attempt failed, trying alternate handle: {alternate_handle}")
46
+
47
+ resolver_params = {"handle": alternate_handle}
48
+ resolver_response = requests.get(resolver_url, params=resolver_params)
49
+
50
+ if resolver_response.status_code != 200:
51
+ return f"Could not resolve handle @{handle}. Status code: {resolver_response.status_code}"
52
+ else:
53
+ return f"Could not resolve handle @{handle}. Status code: {resolver_response.status_code}"
54
 
55
  resolver_data = resolver_response.json()
56
  user_did = resolver_data.get("did")