|
import gradio as gr |
|
from atproto import Client |
|
from atproto_client.exceptions import BadRequestError |
|
|
|
def clean_handle(handle: str) -> str: |
|
"""Clean the handle by removing special characters and whitespace.""" |
|
|
|
handle = ''.join(char for char in handle if char.isprintable()) |
|
handle = handle.strip().replace('@', '').strip() |
|
return handle |
|
|
|
def get_did_from_handle(handle: str) -> str: |
|
""" |
|
Get the DID for a given Bluesky handle. |
|
|
|
Args: |
|
handle (str): Bluesky handle (any valid format) |
|
|
|
Returns: |
|
str: Success or error message |
|
""" |
|
if not handle: |
|
return "Error: Please enter a handle" |
|
|
|
|
|
client = Client() |
|
|
|
|
|
try: |
|
response = client.resolve_handle(handle) |
|
return f"DID: {response.did}" |
|
except BadRequestError: |
|
|
|
try: |
|
cleaned_handle = clean_handle(handle) |
|
if cleaned_handle != handle: |
|
response = client.resolve_handle(cleaned_handle) |
|
return f"DID: {response.did}" |
|
except BadRequestError as e: |
|
return (f"Error: Could not resolve handle. Please check the format.\n" |
|
f"Tried both '{handle}' and '{cleaned_handle}'") |
|
except Exception as e: |
|
return f"Error: {str(e)}" |
|
except Exception as e: |
|
return f"Error: {str(e)}" |
|
|
|
|
|
demo = gr.Interface( |
|
fn=get_did_from_handle, |
|
inputs=[ |
|
gr.Textbox( |
|
label="Enter Bluesky Handle", |
|
placeholder="username.bsky.social", |
|
info="Enter a Bluesky handle to get its DID (with or without @ symbol)" |
|
) |
|
], |
|
outputs=gr.Textbox(label="Result"), |
|
title="Bluesky DID Lookup", |
|
description="Look up the DID (Decentralized Identifier) for any Bluesky handle.<br><br>Built by <a href='https://bsky.app/profile/danielvanstrien.bsky.social'>@danielvanstrien.bsky.social</a><br>Part of <a href='https://huggingface.co/bluesky-community'>Bluesky Community</a>", |
|
examples=[ |
|
["atproto.bsky.social"], |
|
["bsky.app"], |
|
["danielvanstrien.bsky.social"], |
|
["@jay.bsky.team"], |
|
], |
|
theme=gr.themes.Default(), |
|
allow_flagging="never" |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |