From b5d344c9feb9396cd86db1f81121c76ed326d855 Mon Sep 17 00:00:00 2001 From: Alex Kelly Date: Fri, 3 Jan 2020 17:02:10 -0500 Subject: [PATCH] Added more helptext, added action handling for redir and html --- chainlink.py | 59 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/chainlink.py b/chainlink.py index c8e087b..e49f722 100755 --- a/chainlink.py +++ b/chainlink.py @@ -3,6 +3,7 @@ from bottle import Bottle, run, response, request import json import requests +import json2html VERSION = '0.0.1' @@ -12,28 +13,54 @@ app = Bottle() @app.route("/") def root(): host = request.get_header('host') - return f'specify domain in URL like {host}/domain.crypto' + helptext = f""" +

General format is {host}/<domain>/<action> +

If is blank, it will attempt to use the redirect +

You may also/optionally specify an action which can be any of the following + + + + + + + + + + + +
rawShow the raw json (formatted as an html table)
htmlHit the IPFS hash via cloudflare-ipfs.com
redirUse the redirect parameter and just return a 302 redirect to whatever is set
+ """ + return helptext @app.route("/") -def showdomain(domain): +@app.route("//") +def redirectDomain(domain, action=None): apiurl =f'https://unstoppabledomains.com/api/v1/{domain}' - redirect = requests.get(apiurl) - try: - if redirect.status_code == 200: - body = json.loads(redirect.content) + dnslookup = requests.get(apiurl) + if action == None or action == 'redir': + try: + if dnslookup.status_code == 200: + body = json.loads(dnslookup.content) + print(body) + redirect_url = body['ipfs']['redirect_domain'] + response.status = 302 + response.set_header('Location',redirect_url) + else: + return f'Error making call to {apiurl} for {domain}' + except KeyError: + return f'Did not find a redirect for {domain}' + elif action == 'html': + # TODO: clean this up by functionalizing this call. It's basically the same as above + if dnslookup.status_code == 200: + body = json.loads(dnslookup.content) print(body) - redirect_url = body['ipfs']['redirect_domain'] - # TODO: Add handler for the ipfs vs redirect -# if body['ipfs']['html']: -# print('We have html') response.status = 302 - response.set_header('Location',redirect_url) - else: - return f'Error making call to {apiurl} for {domain}' - except KeyError: - return f'Did not find a redirect for {domain}' - + response.set_header('Location', f"https://cloudflare-ipfs.com/ipfs/{body['ipfs']['html']}") + elif action == 'raw': + if dnslookup.status_code == 200: + body = json.loads(dnslookup.content) + return json2html.json2html.convert(json = body) if __name__ == "__main__": run(app, host='localhost', port='5000', reloader=True)