diff --git a/badger.py b/badger.py new file mode 100644 index 0000000..77fbac2 --- /dev/null +++ b/badger.py @@ -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 = """ + + + + + + Badger search engine + + + +
+

Badger Search

+
+ +
+ + + +
+ +
+

Results ({{ results_count }} found)

+ +
+
+

Statistics

+

Most Common Titles

+ +

Most Common Hostnames

+ +
+ + + +""" + +@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)