import requests
import json
import hashlib


URL = 'https://pastie.io/documents'
NAME = 'pastie'

def get_service_tag():
    return NAME

def upload(data):
    try:
        json_data = json.dumps(data)
        md5sum = hashlib.md5(json_data.encode('utf-8')).hexdigest()
        response = requests.post('https://pastie.io/documents', data=json_data)
        if response.status_code == 200:
            key = response.json()['key']
            pastie_url = f'https://pastie.io/{key}'
           # print(f'JSON object uploaded to Pastie: {pastie_url}')

            return {
                  "service": NAME,
                  "name": NAME + '_' + key,
                  "key": pastie_url,
                  "md5sum": md5sum
            }
        else:
              return None
    except requests.exceptions.RequestException:
        return None

