Skip to content

pastebin

Class info

Classes

Name Children Inherits
PastebinSharer
anyenv.text_sharing.pastebin
Share text via Pastebin.com.

    🛈 DocStrings

    Pastebin.com text sharing provider.

    PastebinSharer

    Bases: TextSharer

    Share text via Pastebin.com.

    Source code in src/anyenv/text_sharing/pastebin.py
    class PastebinSharer(TextSharer):
        """Share text via Pastebin.com."""
    
        def __init__(self, api_key: str | None = None) -> None:
            """Initialize the Pastebin sharer.
    
            Args:
                api_key: Pastebin API developer key. If not provided,
                         reads from PASTEBIN_API_KEY environment variable.
            """
            self._api_key = api_key or os.environ.get("PASTEBIN_API_KEY")
            if not self._api_key:
                msg = "Pastebin API key required. Set PASTEBIN_API_KEY or pass api_key parameter."
                raise ValueError(msg)
    
        @property
        def name(self) -> str:
            """Name of the sharing service."""
            return "Pastebin"
    
        async def share(
            self,
            content: str,
            *,
            title: str | None = None,
            syntax: str | None = None,
            visibility: Visibility = "unlisted",
            expires_in: int | None = None,
        ) -> ShareResult:
            """Share content on Pastebin.com.
    
            Args:
                content: The text content to share
                title: Title/name of the paste
                syntax: Syntax highlighting (e.g. "python", "markdown", "javascript")
                visibility: "public", "unlisted", or "private"
                expires_in: Expiration time in seconds (rounded to nearest valid value)
            """
            import anyenv
    
            data: dict[str, Any] = {
                "api_dev_key": self._api_key,
                "api_option": "paste",
                "api_paste_code": content,
                "api_paste_private": VISIBILITY_MAP.get(visibility, "1"),
                "api_paste_expire_date": _get_expiry_code(expires_in),
            }
    
            if title:
                data["api_paste_name"] = title
    
            if syntax:
                data["api_paste_format"] = syntax
    
            response = await anyenv.post("https://pastebin.com/api/api_post.php", data=data)
            url = response.text.strip()
            if url.startswith("Bad API request"):
                msg = f"Pastebin API error: {url}"
                raise RuntimeError(msg)
            paste_key = url.split("/")[-1]
            raw_url = f"https://pastebin.com/raw/{paste_key}"
            return ShareResult(url=url, raw_url=raw_url, id=paste_key)
    

    name property

    name: str
    

    Name of the sharing service.

    __init__

    __init__(api_key: str | None = None) -> None
    

    Initialize the Pastebin sharer.

    Parameters:

    Name Type Description Default
    api_key str | None

    Pastebin API developer key. If not provided, reads from PASTEBIN_API_KEY environment variable.

    None
    Source code in src/anyenv/text_sharing/pastebin.py
    def __init__(self, api_key: str | None = None) -> None:
        """Initialize the Pastebin sharer.
    
        Args:
            api_key: Pastebin API developer key. If not provided,
                     reads from PASTEBIN_API_KEY environment variable.
        """
        self._api_key = api_key or os.environ.get("PASTEBIN_API_KEY")
        if not self._api_key:
            msg = "Pastebin API key required. Set PASTEBIN_API_KEY or pass api_key parameter."
            raise ValueError(msg)
    

    share async

    share(
        content: str,
        *,
        title: str | None = None,
        syntax: str | None = None,
        visibility: Visibility = "unlisted",
        expires_in: int | None = None,
    ) -> ShareResult
    

    Share content on Pastebin.com.

    Parameters:

    Name Type Description Default
    content str

    The text content to share

    required
    title str | None

    Title/name of the paste

    None
    syntax str | None

    Syntax highlighting (e.g. "python", "markdown", "javascript")

    None
    visibility Visibility

    "public", "unlisted", or "private"

    'unlisted'
    expires_in int | None

    Expiration time in seconds (rounded to nearest valid value)

    None
    Source code in src/anyenv/text_sharing/pastebin.py
    async def share(
        self,
        content: str,
        *,
        title: str | None = None,
        syntax: str | None = None,
        visibility: Visibility = "unlisted",
        expires_in: int | None = None,
    ) -> ShareResult:
        """Share content on Pastebin.com.
    
        Args:
            content: The text content to share
            title: Title/name of the paste
            syntax: Syntax highlighting (e.g. "python", "markdown", "javascript")
            visibility: "public", "unlisted", or "private"
            expires_in: Expiration time in seconds (rounded to nearest valid value)
        """
        import anyenv
    
        data: dict[str, Any] = {
            "api_dev_key": self._api_key,
            "api_option": "paste",
            "api_paste_code": content,
            "api_paste_private": VISIBILITY_MAP.get(visibility, "1"),
            "api_paste_expire_date": _get_expiry_code(expires_in),
        }
    
        if title:
            data["api_paste_name"] = title
    
        if syntax:
            data["api_paste_format"] = syntax
    
        response = await anyenv.post("https://pastebin.com/api/api_post.php", data=data)
        url = response.text.strip()
        if url.startswith("Bad API request"):
            msg = f"Pastebin API error: {url}"
            raise RuntimeError(msg)
        paste_key = url.split("/")[-1]
        raw_url = f"https://pastebin.com/raw/{paste_key}"
        return ShareResult(url=url, raw_url=raw_url, id=paste_key)