import subprocess
import json
import hashlib
import re


COMMAND = './external/rentry'
NAME = 'rentry'

def get_service_tag():
    return NAME

def upload(data):
    md5sum = hashlib.md5(json.dumps(data).encode('utf-8')).hexdigest()
    command = [ COMMAND, 'new', json.dumps(data)]
    output = subprocess.check_output(command, universal_newlines=True)
    #print(output)
    lines = output.strip().split('\n')
    url = re.search("(?P<url>https?://[^\s]+)", lines[0]).group("url")
    edit_code = lines[1].split(':')[-1].strip()
    return {'name': 'rentry_' + edit_code, 'service': 'Rentry', 'key': url, 'md5sum': md5sum}

