added option to get blocks by reason
This commit is contained in:
parent
0a19dd55d3
commit
987dd2f14f
63
api.py
63
api.py
@ -31,47 +31,62 @@ def info():
|
|||||||
}
|
}
|
||||||
|
|
||||||
@app.get(base_url+"/api")
|
@app.get(base_url+"/api")
|
||||||
def blocked(domain: str = None):
|
def blocked(domain: str = None, reason: str = None):
|
||||||
if domain == None:
|
if domain == None and reason == None:
|
||||||
raise HTTPException(status_code=400, detail="No domain specified")
|
raise HTTPException(status_code=400, detail="No filter specified")
|
||||||
conn = sqlite3.connect("blocks.db")
|
conn = sqlite3.connect("blocks.db")
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):])
|
if domain != None:
|
||||||
c.execute("select blocker, block_level, reason from blocks where blocked = ? or blocked = ? or blocked = ?", (domain, wildchar, get_hash(domain)))
|
wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):])
|
||||||
|
c.execute("select blocker, block_level, reason from blocks where blocked = ? or blocked = ? or blocked = ?", (domain, wildchar, get_hash(domain)))
|
||||||
|
else:
|
||||||
|
c.execute("select * from blocks where reason like ? and reason != ''", ("%"+reason+"%",))
|
||||||
blocks = c.fetchall()
|
blocks = c.fetchall()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
result = {}
|
result = {}
|
||||||
reasons = {}
|
reasons = {}
|
||||||
|
if domain != None:
|
||||||
for domain, block_level, reason in blocks:
|
for domain, block_level, reason in blocks:
|
||||||
if block_level in result:
|
if block_level in result:
|
||||||
result[block_level].append(domain)
|
result[block_level].append(domain)
|
||||||
else:
|
|
||||||
result[block_level] = [domain]
|
|
||||||
|
|
||||||
if reason != "":
|
|
||||||
if block_level in reasons:
|
|
||||||
reasons[block_level][domain] = reason
|
|
||||||
else:
|
else:
|
||||||
reasons[block_level] = {domain: reason}
|
result[block_level] = [domain]
|
||||||
|
|
||||||
|
if reason != "":
|
||||||
|
if block_level in reasons:
|
||||||
|
reasons[block_level][domain] = reason
|
||||||
|
else:
|
||||||
|
reasons[block_level] = {domain: reason}
|
||||||
|
return {"blocks": result, "reasons": reasons}
|
||||||
|
|
||||||
return {"blocks": result, "reasons": reasons}
|
for blocker, blocked, reason, block_level in blocks:
|
||||||
|
if block_level in result:
|
||||||
|
result[block_level].append({"blocker": blocker, "blocked": blocked, "reason": reason})
|
||||||
|
else:
|
||||||
|
result[block_level] = [{"blocker": blocker, "blocked": blocked, "reason": reason}]
|
||||||
|
return {"blocks": result}
|
||||||
|
|
||||||
@app.get(base_url+"/")
|
@app.get(base_url+"/")
|
||||||
def index(request: Request, domain: str = None):
|
def index(request: Request, domain: str = None, reason: str = None):
|
||||||
if domain == "":
|
if domain == "" or reason == "":
|
||||||
return responses.RedirectResponse("/")
|
return responses.RedirectResponse("/")
|
||||||
blocks = get(f"http://127.0.0.1:{port}{base_url}/api?domain={domain}")
|
|
||||||
info = None
|
info = None
|
||||||
if domain == None:
|
blocks = None
|
||||||
|
if domain == None and reason == None:
|
||||||
info = get(f"http://127.0.0.1:{port}{base_url}/info")
|
info = get(f"http://127.0.0.1:{port}{base_url}/info")
|
||||||
if not info.ok:
|
if not info.ok:
|
||||||
raise HTTPException(status_code=info.status_code, detail=info.text)
|
raise HTTPException(status_code=info.status_code, detail=info.text)
|
||||||
info = info.json()
|
info = info.json()
|
||||||
if not blocks.ok:
|
elif domain != None:
|
||||||
raise HTTPException(status_code=blocks.status_code, detail=blocks.text)
|
blocks = get(f"http://127.0.0.1:{port}{base_url}/api?domain={domain}")
|
||||||
return templates.TemplateResponse("index.html", {"request": request, "domain": domain, "blocks": blocks.json(), "info": info})
|
elif reason != None:
|
||||||
|
blocks = get(f"http://127.0.0.1:{port}{base_url}/api?reason={reason}")
|
||||||
|
if blocks != None:
|
||||||
|
if not blocks.ok:
|
||||||
|
raise HTTPException(status_code=blocks.status_code, detail=blocks.text)
|
||||||
|
blocks = blocks.json()
|
||||||
|
return templates.TemplateResponse("index.html", {"request": request, "domain": domain, "blocks": blocks, "reason": reason, "info": info})
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
uvicorn.run("api:app", host="127.0.0.1", port=port, log_level="info")
|
uvicorn.run("api:app", host="127.0.0.1", port=port, log_level="info")
|
||||||
|
23
index.html
23
index.html
@ -28,7 +28,23 @@
|
|||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
{% if domain %}
|
{% if reason %}
|
||||||
|
<h1>Instances that use "{{reason}}" in their Reason</h1>
|
||||||
|
{% for block_level in blocks.blocks %}
|
||||||
|
<div class="block_level">
|
||||||
|
<h2>{{block_level}} ({{blocks.blocks[block_level]|length}})</h2>
|
||||||
|
{% for block in blocks.blocks[block_level] %}
|
||||||
|
<div class="block">
|
||||||
|
<img src="https://chizu.love/fedi-block-api/favicons/{{block.blocker}}.png" width=16/>
|
||||||
|
<b><a href="https://{{block.blocker}}">{{block.blocker}}</a></b> ->
|
||||||
|
<img src="https://chizu.love/fedi-block-api/favicons/{{block.blocked}}.png" width=16/>
|
||||||
|
<b><a href="https://{{block.blocked}}">{{block.blocked}}</a></b><br/>
|
||||||
|
{{block.reason}}
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
{% elif blocks %}
|
||||||
<h1>Instances that block {{domain}}</h1>
|
<h1>Instances that block {{domain}}</h1>
|
||||||
{% for block_level in blocks.blocks %}
|
{% for block_level in blocks.blocks %}
|
||||||
<div class="block_level">
|
<div class="block_level">
|
||||||
@ -50,6 +66,11 @@
|
|||||||
<input type="text" name="domain" placeholder="example.com" />
|
<input type="text" name="domain" placeholder="example.com" />
|
||||||
<input type="submit" value="Submit" />
|
<input type="submit" value="Submit" />
|
||||||
</form>
|
</form>
|
||||||
|
<h1>Enter a Reason</h1>
|
||||||
|
<form>
|
||||||
|
<input type="text" name="reason" placeholder="free speech" />
|
||||||
|
<input type="submit" value="Submit" />
|
||||||
|
</form>
|
||||||
<div class="info">
|
<div class="info">
|
||||||
known instances: {{info.known_instances}}<br/>
|
known instances: {{info.known_instances}}<br/>
|
||||||
indexed instances: {{info.indexed_instances}}<br/>
|
indexed instances: {{info.indexed_instances}}<br/>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user