File size: 2,451 Bytes
8eb94c9
 
 
 
acf8dce
 
 
 
 
 
 
8eb94c9
 
 
 
 
cad28e4
8eb94c9
 
 
 
acf8dce
 
 
8eb94c9
 
 
cad28e4
8eb94c9
 
 
cad28e4
 
 
 
 
 
 
 
 
 
 
 
8eb94c9
 
 
 
 
 
 
 
 
 
cad28e4
8eb94c9
 
 
 
cad28e4
8eb94c9
 
 
5570d89
cad28e4
8eb94c9
c5a4ed0
cad28e4
8eb94c9
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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."""
    # Remove @ symbol, whitespace, and any invisible characters
    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"
    
    # Initialize client
    client = Client()
    
    # First try: raw input
    try:
        response = client.resolve_handle(handle)
        return f"DID: {response.did}"
    except BadRequestError:
        # Second try: cleaned input
        try:
            cleaned_handle = clean_handle(handle)
            if cleaned_handle != handle:  # Only try if cleaning actually changed something
                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)}"

# Create Gradio interface
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()