push
This commit is contained in:
47
app.py
Normal file
47
app.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from flask import Flask, render_template, request, send_file, jsonify, make_response
|
||||
from scraper import get_google_first_page
|
||||
import io, json, csv, yaml, os
|
||||
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", [])
|
||||
filename = f"results.{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)
|
||||
Reference in New Issue
Block a user