#!/usr/bin/env python3

import sys
import requests
import json

SERVER_URL = "https://paste.yunohost.org"
TIMEOUT = 3

def create_snippet(data):
    try:
        url = SERVER_URL + "/documents"
        response = requests.post(url, data=data.encode('utf-8'), timeout=TIMEOUT)
        response.raise_for_status()
        dockey = json.loads(response.text)['key']
        return SERVER_URL + "/raw/" + dockey
    except requests.exceptions.RequestException as e:
        print("\033[31mError: {}\033[0m".format(e))
        sys.exit(1)


def main():
    output = sys.stdin.read()

    if not output:
        print("\033[31mError: No input received from stdin.\033[0m")
        sys.exit(1)

    url = create_snippet(output)

    print("\033[32mURL: {}\033[0m".format(url))

if __name__ == "__main__":
    main()
