This commit is contained in:
parent
508c67fab7
commit
11a6d6c003
43
hsd.py
Normal file
43
hsd.py
Normal file
@ -0,0 +1,43 @@
|
||||
import requests
|
||||
import os
|
||||
import dotenv
|
||||
|
||||
dotenv.load_dotenv()
|
||||
|
||||
HSD_IP = '127.0.0.1'
|
||||
HSD_PORT = 12037
|
||||
HSD_API_KEY = ''
|
||||
|
||||
if os.getenv("HSD_IP"):
|
||||
HSD_IP = os.getenv("HSD_IP")
|
||||
|
||||
if os.getenv("HSD_PORT"):
|
||||
HSD_PORT = os.getenv("HSD_PORT")
|
||||
|
||||
if os.getenv("HSD_API_KEY"):
|
||||
HSD_API_KEY = os.getenv("HSD_API_KEY")
|
||||
|
||||
HSD_URL = f'http://x:{HSD_API_KEY}@{HSD_IP}:{HSD_PORT}'
|
||||
|
||||
if os.getenv("HSD_URL"):
|
||||
HSD_URL = os.getenv("HSD_URL")
|
||||
|
||||
|
||||
print(f"Using HSD_URL: {HSD_URL}")
|
||||
|
||||
def get_tx(txid):
|
||||
return requests.get(f"{HSD_URL}/tx/{txid}").json()
|
||||
|
||||
def get_block(blockhash):
|
||||
return requests.get(f"{HSD_URL}/block/{blockhash}").json()
|
||||
|
||||
|
||||
def get_name(name):
|
||||
return requests.post(HSD_URL, json={"method": "getnameinfo", "params": [name]}).json()
|
||||
|
||||
def get_name_resource(name):
|
||||
return requests.post(HSD_URL, json={"method": "getnameresource", "params": [name]}).json()
|
||||
|
||||
|
||||
def get_address(address):
|
||||
return requests.get(f"{HSD_URL}/tx/address/{address}").json()
|
79
server.py
79
server.py
@ -15,6 +15,7 @@ import json
|
||||
import requests
|
||||
from datetime import datetime
|
||||
import dotenv
|
||||
import hsd
|
||||
|
||||
dotenv.load_dotenv()
|
||||
|
||||
@ -76,6 +77,33 @@ def wellknown(path):
|
||||
def index():
|
||||
return render_template("index.html")
|
||||
|
||||
@app.route("/name")
|
||||
def name():
|
||||
if request.args.get("name"):
|
||||
name = request.args.get("name")
|
||||
data = hsd.get_name(name)
|
||||
dns = hsd.get_name_resource(name)
|
||||
data = json.dumps(data, indent=4) + "<br><br>" + json.dumps(dns, indent=4)
|
||||
return render_template("data.html", data=data)
|
||||
else:
|
||||
return render_template("index.html")
|
||||
|
||||
@app.route("/tx")
|
||||
def tx():
|
||||
if request.args.get("tx"):
|
||||
tx = hsd.get_tx(request.args.get("tx"))
|
||||
return render_template("data.html", data=json.dumps(tx, indent=4))
|
||||
else:
|
||||
return render_template("index.html")
|
||||
|
||||
@app.route("/block")
|
||||
def block():
|
||||
if request.args.get("block"):
|
||||
block = hsd.get_block(request.args.get("block"))
|
||||
return render_template("data.html", data=json.dumps(block, indent=4))
|
||||
else:
|
||||
return render_template("index.html")
|
||||
|
||||
|
||||
@app.route("/<path:path>")
|
||||
def catch_all(path: str):
|
||||
@ -102,6 +130,57 @@ def catch_all(path: str):
|
||||
# endregion
|
||||
|
||||
|
||||
# region API routes
|
||||
@app.route("/api/v1/version")
|
||||
def api_version():
|
||||
return jsonify({"version": "1.0.0"})
|
||||
|
||||
@app.route("/api/v1/tx/<txid>")
|
||||
def api_tx(txid):
|
||||
tx = hsd.get_tx(txid)
|
||||
if tx:
|
||||
return jsonify(tx)
|
||||
else:
|
||||
return jsonify({"error": "tx not found"}), 404
|
||||
|
||||
@app.route("/api/v1/block/<blockhash>")
|
||||
def api_block(blockhash):
|
||||
block = hsd.get_block(blockhash)
|
||||
if block:
|
||||
return jsonify(block)
|
||||
else:
|
||||
return jsonify({"error": "block not found"}), 404
|
||||
|
||||
@app.route("/api/v1/name/<name>")
|
||||
def api_name(name):
|
||||
name = hsd.get_name(name)
|
||||
if name:
|
||||
return jsonify(name)
|
||||
else:
|
||||
return jsonify({"error": "name not found"}), 404
|
||||
|
||||
@app.route("/api/v1/name/<name>/resource")
|
||||
@app.route("/api/v1/name/<name>/dns")
|
||||
@app.route("/api/v1/resource/<name>")
|
||||
@app.route("/api/v1/dns/<name>")
|
||||
def api_name_resource(name):
|
||||
name = hsd.get_name_resource(name)
|
||||
if name:
|
||||
return jsonify(name)
|
||||
else:
|
||||
return jsonify({"error": "name not found"}), 404
|
||||
|
||||
@app.route("/api/v1/address/<address>")
|
||||
def api_address(address):
|
||||
address = hsd.get_address(address)
|
||||
if address:
|
||||
return jsonify(address)
|
||||
else:
|
||||
return jsonify({"error": "address not found"}), 404
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
# region Error Catching
|
||||
# 404 catch all
|
||||
@app.errorhandler(404)
|
||||
|
21
templates/data.html
Normal file
21
templates/data.html
Normal file
@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Nathan.Woodburn/</title>
|
||||
<link rel="icon" href="/assets/img/favicon.png" type="image/png">
|
||||
<link rel="stylesheet" href="/assets/css/index.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="spacer"></div>
|
||||
<div class="centre">
|
||||
<h1>RESPONSE</h1>
|
||||
<pre style="text-align: left;">{{ data | safe }}</pre>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
@ -12,8 +12,22 @@
|
||||
<body>
|
||||
<div class="spacer"></div>
|
||||
<div class="centre">
|
||||
<h1>Nathan.Woodburn/</h1>
|
||||
<h1>Nathan.Woodburn/ EXPLORER ALPHA</h1>
|
||||
|
||||
<form action="/name" method="get">
|
||||
<input type="text" name="name" placeholder="Name">
|
||||
<button type="submit">Search</button>
|
||||
</form>
|
||||
<form action="/tx" method="get">
|
||||
<input type="text" name="tx" placeholder="TXID">
|
||||
<button type="submit">Search</button>
|
||||
</form>
|
||||
<form action="/block" method="get">
|
||||
<input type="text" name="block" placeholder="Blockhash or Height">
|
||||
<button type="submit">Search</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user