54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
from flask import Flask, render_template, request, send_file, jsonify
|
|
from datetime import datetime
|
|
from api import get_google_first_page
|
|
import io, json, csv, yaml, os, requests
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route("/", methods=["GET"])
|
|
def index():
|
|
|
|
return render_template("index.html", results=[])
|
|
|
|
@app.route("/search", methods=["POST"])
|
|
def search():
|
|
query = request.form.get("q", "").strip()
|
|
if not query:
|
|
return render_template("index.html", error="Zadejte dotaz.")
|
|
|
|
try:
|
|
results = get_google_first_page(query) # list of dicts
|
|
except Exception as e:
|
|
return render_template("index.html", error=f"Vyhledávání selhalo: {e}")
|
|
|
|
notice = None
|
|
if not results:
|
|
notice = (
|
|
"Nebyly nalezeny žádné výsledky/dosáhli jste limitu (5 sekund mezi požadavky)."
|
|
)
|
|
return render_template("results.html", query=query, results=results, notice=notice)
|
|
|
|
@app.route("/export", methods=["POST"])
|
|
def export():
|
|
data = request.get_json()
|
|
|
|
ext = data.get("format", "json")
|
|
|
|
results = data.get("results", [])
|
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
|
filename = f"results_{timestamp}.{ext}"
|
|
if ext == "json":
|
|
|
|
buf = io.BytesIO(json.dumps(results, ensure_ascii=False, indent=2).encode("utf-8"))
|
|
return send_file(buf, as_attachment=True, download_name=filename, mimetype="application/json")
|
|
else:
|
|
return jsonify({"error": "unsupported format"}), 400
|
|
|
|
if __name__ == "__main__":
|
|
port = int(os.environ.get("PORT", 5000))
|
|
app.run(host="0.0.0.0", port=port, debug=True)
|