Edits history of script submission #11432 for ' Create Bluesky Post (bluesky)'

  • python3
    One script reply has been approved by the moderators
    Ap­pro­ved
    # import wmill
    from typing import TypedDict
    from atproto import Client, client_utils
    
    class bluesky(TypedDict):
        username: str
        password: str
    
    def main(creds: bluesky, text: str, shorten_urls: bool = True):
        client = Client()
        client.login(creds["username"], creds["password"])
    
        if shorten_urls:
            builder = builder_shorten_urls(text)
            response = client.send_post(builder)
        else:
            response = client.send_post(text=text)
    
        return response
    
    def builder_shorten_urls(buffer):
        import re
    
        builder = client_utils.TextBuilder()
    
        while True:
            match = re.search(r"(?P<url>https?://[^\s]+)", buffer)
            if not match:
                break
            
            builder.text(buffer[:match.start()])
            buffer = buffer[match.end():]
    
            url = match.group("url")
            builder.link(shorten_url(url, 9), url)
        
        builder.text(buffer)
    
        return builder
    
    def shorten_url(string_url, max_path_query_length):
        from urllib.parse import urlparse
    
        url = urlparse(string_url)
        netloc = url.netloc
        path_query = url.path
        if url.query:
            path_query += "?" + url.query
    
        # Keep the first "/" before the "...".
        if len(path_query) > 0:
            netloc += path_query[0]
            path_query = path_query[1:]
    
        # path_query is short enough.
        if len(path_query) <= max_path_query_length:
            return f"{netloc}{path_query}"
    
        # Shorten the path_query with a "...".
        path_query_length = max_path_query_length - 3
        if path_query_length > 0:
            return f"{netloc}...{path_query[-path_query_length:]}"
    
        # path_query too small to be added after the "...".
        return f"{netloc}..."
    

    Submitted by gabrielius.m896 494 days ago

  • python3
    # import wmill
    from typing import TypedDict
    from atproto import Client, client_utils
    
    class c_bluesky_credentials(TypedDict):
        password: str
        username: str
    
    def main(creds: c_bluesky_credentials, text: str, shorten_urls: bool = True):
        client = Client()
        client.login(creds["username"], creds["password"])
    
        if shorten_urls:
            builder = builder_shorten_urls(text)
            response = client.send_post(builder)
        else:
            response = client.send_post(text=text)
    
        return response
    
    def builder_shorten_urls(buffer):
        import re
    
        builder = client_utils.TextBuilder()
    
        while True:
            match = re.search(r"(?P<url>https?://[^\s]+)", buffer)
            if not match:
                break
            
            builder.text(buffer[:match.start()])
            buffer = buffer[match.end():]
    
            url = match.group("url")
            builder.link(shorten_url(url, 9), url)
        
        builder.text(buffer)
    
        return builder
    
    def shorten_url(string_url, max_path_query_length):
        from urllib.parse import urlparse
    
        url = urlparse(string_url)
        netloc = url.netloc
        path_query = url.path
        if url.query:
            path_query += "?" + url.query
    
        # Keep the first "/" before the "...".
        if len(path_query) > 0:
            netloc += path_query[0]
            path_query = path_query[1:]
    
        # path_query is short enough.
        if len(path_query) <= max_path_query_length:
            return f"{netloc}{path_query}"
    
        # Shorten the path_query with a "...".
        path_query_length = max_path_query_length - 3
        if path_query_length > 0:
            return f"{netloc}...{path_query[-path_query_length:]}"
    
        # path_query too small to be added after the "...".
        return f"{netloc}..."
    

    Submitted by gabrielius.m896 494 days ago