Añadir badger.py

This commit is contained in:
nix 2024-11-26 05:50:24 +00:00
parent 7f96fb9b62
commit 19dd19a4ba

233
badger.py Normal file
View File

@ -0,0 +1,233 @@
from flask import Flask, request, render_template_string
import json
from collections import Counter
app = Flask(__name__)
# Load the JSON data from the file
with open("output.json", "r", encoding="utf-8") as f:
data = json.load(f)
# Function to analyze the data
def analyze_data(results):
# Filter out unwanted titles and hostnames
filtered_titles = [
entry["title"]
for entry in results
if entry.get("title") and entry["title"].lower() not in ["unknown", "no title"]
]
filtered_hostnames = [
entry["hostname"]
for entry in results
if entry.get("hostname") and entry["hostname"].lower() != "unknown"
]
# Find the most common titles and hostnames
most_common_titles = Counter(filtered_titles).most_common(25)
most_common_hostnames = Counter(filtered_hostnames).most_common(20)
return most_common_titles, most_common_hostnames
# HTML Template
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Badger search engine</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #20201d;
color: #a6a28c;
margin: 20px;
display: flex;
flex-direction: row;
}
h1 {
color: #e0e0e0;
}
form {
margin-bottom: 20px;
}
input[type="text"] {
width: 300px;
padding: 8px;
margin-right: 10px;
background-color: #333;
color: #a6a28c;
border: 1px solid #555;
border-radius: 4px;
}
label {
margin-right: 15px;
color: #e0e0e0;
}
button {
padding: 8px 16px;
background-color: #444;
color: #a6a28c;
border: 1px solid #555;
cursor: pointer;
border-radius: 4px;
}
button:hover {
background-color: #555;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin-bottom: 10px;
padding: 10px;
background: #333;
color: #a6a28c;
border: 1px solid #555;
border-radius: 4px;
}
a {
color: #4e9fff;
text-decoration: none;
}
a:hover {
color: #80d4ff;
text-decoration: underline;
}
#statistics {
flex-shrink: 0;
width: 300px;
background-color: #2c2c28;
box-shadow: -4px 0 6px rgba(0, 0, 0, 0.3);
color: #a6a28c;
padding: 20px;
border-left: 1px solid #555;
height: 100vh;
overflow-y: auto;
}
#statistics h2, #statistics h3 {
margin-top: 0;
color: #e0e0e0;
}
#main-content {
flex: 1;
margin-right: 20px;
}
@media (max-width: 600px) {
#statistics {
width: 100%;
height: auto;
}
}
</style>
</head>
<body>
<div id="main-content">
<h1>Badger Search</h1>
<form method="POST">
<input type="text" name="search" placeholder="Search..." value="{{ search_query }}">
<div>
<label>
<input type="checkbox" name="has_title" {% if filters['has_title'] %}checked{% endif %}>
Has Title
</label>
<label>
<input type="checkbox" name="has_description" {% if filters['has_description'] %}checked{% endif %}>
Has Description
</label>
<label>
<input type="checkbox" name="has_hostname" {% if filters['has_hostname'] %}checked{% endif %}>
Has Hostname
</label>
</div>
<button type="submit">Search</button>
</form>
<h2>Results ({{ results_count }} found)</h2>
<ul>
{% for result in results %}
<li>
<strong>IP:</strong> <a href="http://{{ result.ip }}" target="_blank">{{ result.ip }}</a><br>
<strong>Hostname:</strong> {{ result.hostname }}<br>
<strong>Title:</strong> {{ result.title }}<br>
<strong>Description:</strong> {{ result.description }}
</li>
{% else %}
<li>No results found.</li>
{% endfor %}
</ul>
</div>
<div id="statistics">
<h2>Statistics</h2>
<h3>Most Common Titles</h3>
<ul>
{% for title, count in most_common_titles %}
<li>{{ title }} ({{ count }})</li>
{% endfor %}
</ul>
<h3>Most Common Hostnames</h3>
<ul>
{% for hostname, count in most_common_hostnames %}
<li>{{ hostname }} ({{ count }})</li>
{% endfor %}
</ul>
</div>
</body>
</html>
"""
@app.route("/", methods=["GET", "POST"])
def index():
results = data["results"] # Access the results key in the data
search_query = ""
filters = {
"has_title": False,
"has_description": False,
"has_hostname": False
}
if request.method == "POST":
# Get search query
search_query = request.form.get("search", "").strip().lower()
filters["has_title"] = "has_title" in request.form
filters["has_description"] = "has_description" in request.form
filters["has_hostname"] = "has_hostname" in request.form
# Filter based on the search query
if search_query:
results = [
entry for entry in results
if search_query in entry["ip"].lower()
or search_query in entry["title"].lower()
or search_query in entry["description"].lower()
or search_query in entry["hostname"].lower()
]
# Apply the checkbox filters
if filters["has_title"]:
results = [entry for entry in results if entry["title"] and entry["title"].lower() != "no title"]
if filters["has_description"]:
results = [entry for entry in results if entry["description"] and entry["description"].lower() != "no description"]
if filters["has_hostname"]:
results = [entry for entry in results if entry["hostname"] and entry["hostname"].lower() != "unknown"]
# Calculate the most common titles and hostnames
most_common_titles, most_common_hostnames = analyze_data(data["results"])
# Calculate the number of results
results_count = len(results)
return render_template_string(
HTML_TEMPLATE,
results=results,
search_query=search_query,
filters=filters,
results_count=results_count,
most_common_titles=most_common_titles,
most_common_hostnames=most_common_hostnames
)
if __name__ == "__main__":
app.run(debug=True)