]> git.mxchange.org Git - fba.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Sun, 21 May 2023 09:27:16 +0000 (11:27 +0200)
committerRoland Häder <roland@mxchange.org>
Sun, 21 May 2023 09:27:16 +0000 (11:27 +0200)
- rewrote a bit

api.py

diff --git a/api.py b/api.py
index 05debfabba159c379fb8e98cebdc4f4da85ecb16..d6ce394a2a794e5165481952d7d1d819eb07c472 100644 (file)
--- a/api.py
+++ b/api.py
@@ -1,10 +1,10 @@
 from fastapi import FastAPI, Request, HTTPException, responses, Query
 from fastapi.templating import Jinja2Templates
-from requests import get
 from datetime import datetime
 from email import utils
 
 import uvicorn
+import requests
 import sqlite3
 import re
 import fba
@@ -18,10 +18,10 @@ def info():
     known, indexed, blocks = fba.c.fetchone()
 
     return {
-        "known_instances": known,
+        "known_instances"  : known,
         "indexed_instances": indexed,
-        "blocks_recorded": blocks,
-        "slogan": fba.config["slogan"]
+        "blocks_recorded"  : blocks,
+        "slogan"           : fba.config["slogan"]
     }
 
 @app.get(fba.config["base_url"] + "/top")
@@ -44,7 +44,7 @@ def top(blocked: int = None, blockers: int = None, reference: int = None):
     scores = fba.c.fetchall()
 
     scoreboard = []
-    print(scores)
+
     for domain, highscore in scores:
         scoreboard.append({"domain": domain, "highscore": highscore})
 
@@ -86,32 +86,31 @@ def blocked(domain: str = None, reason: str = None, reverse: str = None):
     return result
 
 @app.get(fba.config["base_url"] + "/scoreboard")
-def index(request: Request, blockers: int = None, blocked: int = None, reference: int = None):
+def index(request: Request, blockers: int = None, blocked: int = None, reference: int = None):
     scores = None
 
     if blockers == None and blocked == None and reference == None:
         raise HTTPException(status_code=400, detail="No filter specified")
     elif blockers != None:
-        scores = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?blockers={blockers}")
+        scores = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?blockers={blockers}")
     elif blocked != None:
-        scores = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?blocked={blocked}")
+        scores = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?blocked={blocked}")
     elif reference != None:
-        scores = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?reference={reference}")
-
-    if scores != None:
-        if not scores.ok:
-            raise HTTPException(status_code=scores.status_code, detail=scores.text)
+        scores = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?reference={reference}")
 
-        scores = scores.json()
+    if scores = None:
+        raise HTTPException(status_code=500, detail="Could not determine scores")
+    elif not scores.ok:
+        raise HTTPException(status_code=scores.status_code, detail=scores.text)
 
     return templates.TemplateResponse("index.html", {
-        "base_url": fba.config["base_url"],
-        "request": request,
+        "base_url"  : fba.config["base_url"],
+        "request"   : request,
         "scoreboard": True,
-        "blockers": blockers,
-        "blocked": blocked,
-        "reference": reference,
-        "scores": scores
+        "blockers"  : blockers,
+        "blocked"   : blocked,
+        "reference" : reference,
+        "scores"    : scores.json()
     })
 
 @app.get(fba.config["base_url"] + "/")
@@ -123,18 +122,18 @@ def index(request: Request, domain: str = None, reason: str = None, reverse: str
     blocks = None
 
     if domain == None and reason == None and reverse == None:
-        info = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/info")
+        info = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/info")
 
         if not info.ok:
             raise HTTPException(status_code=info.status_code, detail=info.text)
 
         info = info.json()
     elif domain != None:
-        blocks = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?domain={domain}")
+        blocks = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?domain={domain}")
     elif reason != None:
-        blocks = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?reason={reason}")
+        blocks = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?reason={reason}")
     elif reverse != None:
-        blocks = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?reverse={reverse}")
+        blocks = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?reverse={reverse}")
 
     if blocks != None:
         if not blocks.ok: