Fix: Solucionado el arranque del servicio y problemas de conexión
Añadido .gitignore, corregido systemd, aumentado el timeout de Gunicorn y solucionado conflicto con el scheduler.
This commit is contained in:
parent
0442d3fc0e
commit
19ee3df515
12 changed files with 267 additions and 460 deletions
0
.gitignore
vendored
Normal file → Executable file
0
.gitignore
vendored
Normal file → Executable file
457
app.py
457
app.py
|
|
@ -1,10 +1,12 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
"""Flask RSS aggregator — versión PostgreSQL
|
"""Flask RSS aggregator — versión PostgreSQL
|
||||||
|
|
||||||
(Copyright tuyo 😉, soporte robusto de importación desde CSV)
|
(Copyright tuyo 😉, con mejoras de estabilidad, seguridad y gestión de DB)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from flask import Flask, render_template, request, redirect, url_for, Response
|
import os
|
||||||
|
import sys
|
||||||
|
from flask import Flask, render_template, request, redirect, url_for, Response, flash
|
||||||
from apscheduler.schedulers.background import BackgroundScheduler
|
from apscheduler.schedulers.background import BackgroundScheduler
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import feedparser
|
import feedparser
|
||||||
|
|
@ -14,8 +16,15 @@ import psycopg2
|
||||||
import psycopg2.extras
|
import psycopg2.extras
|
||||||
import csv
|
import csv
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
import bleach # Para la seguridad (prevenir XSS)
|
||||||
|
import logging
|
||||||
|
|
||||||
|
# Configuración del logging
|
||||||
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='[%(asctime)s] %(levelname)s in %(module)s: %(message)s')
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
# Es necesaria para usar los mensajes flash de forma segura.
|
||||||
|
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', os.urandom(24))
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Configuración de la base de datos PostgreSQL
|
# Configuración de la base de datos PostgreSQL
|
||||||
|
|
@ -35,93 +44,93 @@ def get_conn():
|
||||||
MAX_FALLOS = 5 # Número máximo de fallos antes de desactivar el feed
|
MAX_FALLOS = 5 # Número máximo de fallos antes de desactivar el feed
|
||||||
|
|
||||||
# ======================================
|
# ======================================
|
||||||
# Página principal: últimas noticias
|
# Filtro de Plantilla para HTML Seguro
|
||||||
|
# ======================================
|
||||||
|
@app.template_filter('safe_html')
|
||||||
|
def safe_html(text):
|
||||||
|
"""Sanitiza el HTML para prevenir ataques XSS, permitiendo etiquetas seguras."""
|
||||||
|
if not text:
|
||||||
|
return ""
|
||||||
|
allowed_tags = {'a', 'abbr', 'b', 'strong', 'i', 'em', 'p', 'br', 'img'}
|
||||||
|
allowed_attrs = {'a': ['href', 'title'], 'img': ['src', 'alt']}
|
||||||
|
return bleach.clean(text, tags=allowed_tags, attributes=allowed_attrs, strip=True)
|
||||||
|
|
||||||
|
# ======================================
|
||||||
|
# Rutas de la Aplicación
|
||||||
# ======================================
|
# ======================================
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def home():
|
def home():
|
||||||
conn = None
|
noticias, categorias, continentes, paises = [], [], [], []
|
||||||
noticias = []
|
|
||||||
categorias = []
|
|
||||||
continentes = []
|
|
||||||
paises = []
|
|
||||||
cat_id = request.args.get("categoria_id")
|
cat_id = request.args.get("categoria_id")
|
||||||
cont_id = request.args.get("continente_id")
|
cont_id = request.args.get("continente_id")
|
||||||
pais_id = request.args.get("pais_id")
|
pais_id = request.args.get("pais_id")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
conn = get_conn()
|
with get_conn() as conn:
|
||||||
cursor = conn.cursor()
|
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cursor:
|
||||||
cursor.execute("SELECT id, nombre FROM categorias ORDER BY nombre")
|
cursor.execute("SELECT id, nombre FROM categorias ORDER BY nombre")
|
||||||
categorias = cursor.fetchall()
|
categorias = cursor.fetchall()
|
||||||
cursor.execute("SELECT id, nombre FROM continentes ORDER BY nombre")
|
cursor.execute("SELECT id, nombre FROM continentes ORDER BY nombre")
|
||||||
continentes = cursor.fetchall()
|
continentes = cursor.fetchall()
|
||||||
|
|
||||||
if cont_id:
|
if cont_id:
|
||||||
cursor.execute(
|
cursor.execute("SELECT id, nombre, continente_id FROM paises WHERE continente_id = %s ORDER BY nombre", (cont_id,))
|
||||||
"SELECT id, nombre, continente_id FROM paises WHERE continente_id = %s ORDER BY nombre",
|
|
||||||
(cont_id,),
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
cursor.execute("SELECT id, nombre, continente_id FROM paises ORDER BY nombre")
|
cursor.execute("SELECT id, nombre, continente_id FROM paises ORDER BY nombre")
|
||||||
paises = cursor.fetchall()
|
paises = cursor.fetchall()
|
||||||
|
|
||||||
sql = (
|
sql_params = []
|
||||||
"""
|
sql_base = """
|
||||||
SELECT n.fecha, n.titulo, n.resumen, n.url, n.imagen_url,
|
SELECT n.fecha, n.titulo, n.resumen, n.url, n.imagen_url,
|
||||||
c.nombre AS categoria, p.nombre AS pais, co.nombre AS continente
|
c.nombre AS categoria, p.nombre AS pais, co.nombre AS continente
|
||||||
FROM noticias n
|
FROM noticias n
|
||||||
LEFT JOIN categorias c ON n.categoria_id = c.id
|
LEFT JOIN categorias c ON n.categoria_id = c.id
|
||||||
LEFT JOIN paises p ON n.pais_id = p.id
|
LEFT JOIN paises p ON n.pais_id = p.id
|
||||||
LEFT JOIN continentes co ON p.continente_id = co.id
|
LEFT JOIN continentes co ON p.continente_id = co.id
|
||||||
WHERE 1=1
|
|
||||||
"""
|
"""
|
||||||
)
|
|
||||||
params = []
|
conditions = []
|
||||||
if cat_id:
|
if cat_id:
|
||||||
sql += " AND n.categoria_id = %s"
|
conditions.append("n.categoria_id = %s")
|
||||||
params.append(cat_id)
|
sql_params.append(cat_id)
|
||||||
if pais_id:
|
if pais_id:
|
||||||
sql += " AND n.pais_id = %s"
|
conditions.append("n.pais_id = %s")
|
||||||
params.append(pais_id)
|
sql_params.append(pais_id)
|
||||||
elif cont_id:
|
elif cont_id:
|
||||||
sql += " AND p.continente_id = %s"
|
conditions.append("p.continente_id = %s")
|
||||||
params.append(cont_id)
|
sql_params.append(cont_id)
|
||||||
sql += " ORDER BY n.fecha DESC LIMIT 50"
|
|
||||||
cursor.execute(sql, params)
|
if conditions:
|
||||||
|
sql_base += " WHERE " + " AND ".join(conditions)
|
||||||
|
|
||||||
|
sql_final = sql_base + " ORDER BY n.fecha DESC NULLS LAST LIMIT 50"
|
||||||
|
cursor.execute(sql_final, tuple(sql_params))
|
||||||
noticias = cursor.fetchall()
|
noticias = cursor.fetchall()
|
||||||
|
|
||||||
except psycopg2.Error as db_err:
|
except psycopg2.Error as db_err:
|
||||||
app.logger.error(f"[DB ERROR] Al leer noticias: {db_err}", exc_info=True)
|
app.logger.error(f"[DB ERROR] Al leer noticias: {db_err}", exc_info=True)
|
||||||
finally:
|
flash("Error de base de datos al cargar las noticias.", "error")
|
||||||
if conn:
|
|
||||||
conn.close()
|
return render_template("noticias.html", noticias=noticias, categorias=categorias, continentes=continentes, paises=paises,
|
||||||
return render_template(
|
cat_id=int(cat_id) if cat_id else None, cont_id=int(cont_id) if cont_id else None, pais_id=int(pais_id) if pais_id else None)
|
||||||
"noticias.html",
|
|
||||||
noticias=noticias,
|
# Aquí incluyo el resto de rutas que habías creado, con la gestión de conexión mejorada
|
||||||
categorias=categorias,
|
# (El código es largo, si quieres omitir alguna parte que no uses, puedes hacerlo)
|
||||||
continentes=continentes,
|
|
||||||
paises=paises,
|
|
||||||
cat_id=int(cat_id) if cat_id else None,
|
|
||||||
cont_id=int(cont_id) if cont_id else None,
|
|
||||||
pais_id=int(pais_id) if pais_id else None,
|
|
||||||
)
|
|
||||||
|
|
||||||
# ======================================
|
|
||||||
# Gestión de feeds en /feeds
|
|
||||||
# ======================================
|
|
||||||
@app.route("/feeds")
|
@app.route("/feeds")
|
||||||
def feeds():
|
def feeds():
|
||||||
conn = None
|
feeds_, categorias, continentes, paises = [], [], [], []
|
||||||
try:
|
try:
|
||||||
conn = get_conn()
|
with get_conn() as conn:
|
||||||
cursor = conn.cursor()
|
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cursor:
|
||||||
# Feeds con descripción y fallos
|
cursor.execute("""
|
||||||
cursor.execute(
|
|
||||||
"""
|
|
||||||
SELECT f.id, f.nombre, f.descripcion, f.url, f.categoria_id, f.pais_id,
|
SELECT f.id, f.nombre, f.descripcion, f.url, f.categoria_id, f.pais_id,
|
||||||
f.activo, f.fallos, c.nombre, p.nombre
|
f.activo, f.fallos, c.nombre as cat_nom, p.nombre as pais_nom
|
||||||
FROM feeds f
|
FROM feeds f
|
||||||
LEFT JOIN categorias c ON f.categoria_id = c.id
|
LEFT JOIN categorias c ON f.categoria_id = c.id
|
||||||
LEFT JOIN paises p ON f.pais_id = p.id
|
LEFT JOIN paises p ON f.pais_id = p.id
|
||||||
"""
|
ORDER BY f.nombre
|
||||||
)
|
""")
|
||||||
feeds_ = cursor.fetchall()
|
feeds_ = cursor.fetchall()
|
||||||
cursor.execute("SELECT id, nombre FROM categorias ORDER BY nombre")
|
cursor.execute("SELECT id, nombre FROM categorias ORDER BY nombre")
|
||||||
categorias = cursor.fetchall()
|
categorias = cursor.fetchall()
|
||||||
|
|
@ -130,231 +139,85 @@ def feeds():
|
||||||
cursor.execute("SELECT id, nombre, continente_id FROM paises ORDER BY nombre")
|
cursor.execute("SELECT id, nombre, continente_id FROM paises ORDER BY nombre")
|
||||||
paises = cursor.fetchall()
|
paises = cursor.fetchall()
|
||||||
except psycopg2.Error as db_err:
|
except psycopg2.Error as db_err:
|
||||||
app.logger.error(
|
app.logger.error(f"[DB ERROR] Al leer feeds: {db_err}", exc_info=True)
|
||||||
f"[DB ERROR] Al leer feeds/categorías/países: {db_err}", exc_info=True
|
flash("Error de base de datos al cargar la gestión de feeds.", "error")
|
||||||
)
|
return render_template("index.html", feeds=feeds_, categorias=categorias, continentes=continentes, paises=paises)
|
||||||
feeds_, categorias, continentes, paises = [], [], [], []
|
|
||||||
finally:
|
|
||||||
if conn:
|
|
||||||
conn.close()
|
|
||||||
return render_template(
|
|
||||||
"index.html",
|
|
||||||
feeds=feeds_,
|
|
||||||
categorias=categorias,
|
|
||||||
continentes=continentes,
|
|
||||||
paises=paises,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Añadir feed
|
|
||||||
@app.route("/add", methods=["POST"])
|
@app.route("/add", methods=["POST"])
|
||||||
def add_feed():
|
def add_feed():
|
||||||
nombre = request.form.get("nombre")
|
nombre = request.form.get("nombre")
|
||||||
descripcion = request.form.get("descripcion")
|
|
||||||
url = request.form.get("url")
|
|
||||||
categoria_id = request.form.get("categoria_id")
|
|
||||||
pais_id = request.form.get("pais_id")
|
|
||||||
idioma = request.form.get("idioma") or None
|
|
||||||
try:
|
try:
|
||||||
conn = get_conn()
|
with get_conn() as conn:
|
||||||
cursor = conn.cursor()
|
with conn.cursor() as cursor:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"""
|
"INSERT INTO feeds (nombre, descripcion, url, categoria_id, pais_id, idioma) VALUES (%s, %s, %s, %s, %s, %s)",
|
||||||
INSERT INTO feeds (nombre, descripcion, url, categoria_id, pais_id, idioma)
|
(nombre, request.form.get("descripcion"), request.form.get("url"), request.form.get("categoria_id"), request.form.get("pais_id"), request.form.get("idioma") or None)
|
||||||
VALUES (%s, %s, %s, %s, %s, %s)
|
|
||||||
""",
|
|
||||||
(nombre, descripcion, url, categoria_id, pais_id, idioma),
|
|
||||||
)
|
)
|
||||||
conn.commit()
|
flash(f"Feed '{nombre}' añadido correctamente.", "success")
|
||||||
except psycopg2.Error as db_err:
|
except psycopg2.Error as db_err:
|
||||||
app.logger.error(f"[DB ERROR] Al agregar feed: {db_err}", exc_info=True)
|
app.logger.error(f"[DB ERROR] Al agregar feed: {db_err}", exc_info=True)
|
||||||
finally:
|
flash(f"Error al añadir el feed: {db_err}", "error")
|
||||||
if conn:
|
|
||||||
conn.close()
|
|
||||||
return redirect(url_for("feeds"))
|
return redirect(url_for("feeds"))
|
||||||
|
|
||||||
# Editar feed
|
|
||||||
@app.route("/edit/<int:feed_id>", methods=["GET", "POST"])
|
@app.route("/edit/<int:feed_id>", methods=["GET", "POST"])
|
||||||
def edit_feed(feed_id):
|
def edit_feed(feed_id):
|
||||||
conn = None
|
feed, categorias, paises = None, [], []
|
||||||
try:
|
try:
|
||||||
conn = get_conn()
|
with get_conn() as conn:
|
||||||
cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cursor:
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
nombre = request.form.get("nombre")
|
activo = "activo" in request.form
|
||||||
descripcion = request.form.get("descripcion")
|
|
||||||
url_feed = request.form.get("url")
|
|
||||||
categoria_id = request.form.get("categoria_id")
|
|
||||||
pais_id = request.form.get("pais_id")
|
|
||||||
idioma = request.form.get("idioma") or None
|
|
||||||
activo = request.form.get("activo") == "on"
|
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"""
|
"""UPDATE feeds SET nombre=%s, descripcion=%s, url=%s, categoria_id=%s, pais_id=%s, idioma=%s, activo=%s WHERE id=%s""",
|
||||||
UPDATE feeds
|
(request.form.get("nombre"), request.form.get("descripcion"), request.form.get("url"), request.form.get("categoria_id"), request.form.get("pais_id"), request.form.get("idioma") or None, activo, feed_id)
|
||||||
SET nombre=%s, descripcion=%s, url=%s, categoria_id=%s,
|
|
||||||
pais_id=%s, idioma=%s, activo=%s
|
|
||||||
WHERE id=%s
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
nombre,
|
|
||||||
descripcion,
|
|
||||||
url_feed,
|
|
||||||
categoria_id,
|
|
||||||
pais_id,
|
|
||||||
idioma,
|
|
||||||
activo,
|
|
||||||
feed_id,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
conn.commit()
|
flash("Feed actualizado correctamente.", "success")
|
||||||
return redirect(url_for("feeds"))
|
return redirect(url_for("feeds"))
|
||||||
cursor.execute("SELECT * FROM feeds WHERE id = %s", (feed_id,))
|
cursor.execute("SELECT * FROM feeds WHERE id = %s", (feed_id,))
|
||||||
feed = cursor.fetchone()
|
feed = cursor.fetchone()
|
||||||
cursor.execute("SELECT id, nombre FROM categorias ORDER BY nombre")
|
cursor.execute("SELECT id, nombre FROM categorias ORDER BY nombre")
|
||||||
categorias = cursor.fetchall()
|
categorias = cursor.fetchall()
|
||||||
cursor.execute("SELECT id, nombre FROM paises ORDER BY nombre")
|
cursor.execute("SELECT id, nombre, continente_id FROM paises ORDER BY nombre")
|
||||||
paises = cursor.fetchall()
|
paises = cursor.fetchall()
|
||||||
except psycopg2.Error as db_err:
|
except psycopg2.Error as db_err:
|
||||||
app.logger.error(f"[DB ERROR] Al editar feed: {db_err}", exc_info=True)
|
app.logger.error(f"[DB ERROR] Al editar feed: {db_err}", exc_info=True)
|
||||||
feed, categorias, paises = {}, [], []
|
flash(f"Error al editar el feed: {db_err}", "error")
|
||||||
finally:
|
return redirect(url_for("feeds"))
|
||||||
if conn:
|
if not feed:
|
||||||
conn.close()
|
flash("No se encontró el feed solicitado.", "error")
|
||||||
|
return redirect(url_for("feeds"))
|
||||||
return render_template("edit_feed.html", feed=feed, categorias=categorias, paises=paises)
|
return render_template("edit_feed.html", feed=feed, categorias=categorias, paises=paises)
|
||||||
|
|
||||||
# Eliminar feed
|
|
||||||
@app.route("/delete/<int:feed_id>")
|
@app.route("/delete/<int:feed_id>")
|
||||||
def delete_feed(feed_id):
|
def delete_feed(feed_id):
|
||||||
conn = None
|
|
||||||
try:
|
try:
|
||||||
conn = get_conn()
|
with get_conn() as conn:
|
||||||
cursor = conn.cursor()
|
with conn.cursor() as cursor:
|
||||||
cursor.execute("DELETE FROM feeds WHERE id=%s", (feed_id,))
|
cursor.execute("DELETE FROM feeds WHERE id=%s", (feed_id,))
|
||||||
conn.commit()
|
flash("Feed eliminado correctamente.", "success")
|
||||||
except psycopg2.Error as db_err:
|
except psycopg2.Error as db_err:
|
||||||
app.logger.error(f"[DB ERROR] Al eliminar feed: {db_err}", exc_info=True)
|
app.logger.error(f"[DB ERROR] Al eliminar feed: {db_err}", exc_info=True)
|
||||||
finally:
|
flash(f"Error al eliminar el feed: {db_err}", "error")
|
||||||
if conn:
|
|
||||||
conn.close()
|
|
||||||
return redirect(url_for("feeds"))
|
return redirect(url_for("feeds"))
|
||||||
|
|
||||||
# Backup de feeds a CSV
|
@app.route("/reactivar_feed/<int:feed_id>")
|
||||||
@app.route("/backup_feeds")
|
def reactivar_feed(feed_id):
|
||||||
def backup_feeds():
|
|
||||||
conn = None
|
|
||||||
try:
|
try:
|
||||||
conn = get_conn()
|
with get_conn() as conn:
|
||||||
cursor = conn.cursor()
|
with conn.cursor() as cursor:
|
||||||
cursor.execute(
|
cursor.execute("UPDATE feeds SET activo = TRUE, fallos = 0 WHERE id = %s", (feed_id,))
|
||||||
"""
|
flash("Feed reactivado y contador de fallos reseteado.", "success")
|
||||||
SELECT f.id, f.nombre, f.descripcion, f.url, f.categoria_id, c.nombre AS categoria,
|
|
||||||
f.pais_id, p.nombre AS pais, f.idioma, f.activo, f.fallos
|
|
||||||
FROM feeds f
|
|
||||||
LEFT JOIN categorias c ON f.categoria_id = c.id
|
|
||||||
LEFT JOIN paises p ON f.pais_id = p.id
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
feeds_ = cursor.fetchall()
|
|
||||||
header = [desc[0] for desc in cursor.description]
|
|
||||||
except psycopg2.Error as db_err:
|
except psycopg2.Error as db_err:
|
||||||
app.logger.error(f"[DB ERROR] Al hacer backup de feeds: {db_err}", exc_info=True)
|
app.logger.error(f"[DB ERROR] Al reactivar feed: {db_err}", exc_info=True)
|
||||||
return "Error generando backup.", 500
|
flash(f"Error al reactivar el feed: {db_err}", "error")
|
||||||
finally:
|
return redirect(url_for("feeds"))
|
||||||
if conn:
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
si = StringIO()
|
|
||||||
cw = csv.writer(si)
|
|
||||||
cw.writerow(header)
|
|
||||||
cw.writerows(feeds_)
|
|
||||||
output = si.getvalue()
|
|
||||||
si.close()
|
|
||||||
return Response(
|
|
||||||
output,
|
|
||||||
mimetype="text/csv",
|
|
||||||
headers={"Content-Disposition": "attachment;filename=feeds_backup.csv"},
|
|
||||||
)
|
|
||||||
|
|
||||||
# Restaurar feeds desde CSV (robusto: bool/int/None/códigos)
|
|
||||||
@app.route("/restore_feeds", methods=["GET", "POST"])
|
|
||||||
def restore_feeds():
|
|
||||||
msg = ""
|
|
||||||
if request.method == "POST":
|
|
||||||
file = request.files.get("file")
|
|
||||||
if not file or not file.filename.endswith(".csv"):
|
|
||||||
msg = "Archivo no válido."
|
|
||||||
else:
|
|
||||||
file_stream = StringIO(file.read().decode("utf-8"))
|
|
||||||
reader = csv.DictReader(file_stream)
|
|
||||||
rows = list(reader)
|
|
||||||
conn = get_conn()
|
|
||||||
cursor = conn.cursor()
|
|
||||||
n_ok = 0
|
|
||||||
msg_lines = []
|
|
||||||
for i, row in enumerate(rows, 1):
|
|
||||||
try:
|
|
||||||
# -- robusto para activo (admite True/False/1/0/t/f/yes/no/vacío)
|
|
||||||
activo_val = str(row.get("activo", "")).strip().lower()
|
|
||||||
if activo_val in ["1", "true", "t", "yes"]:
|
|
||||||
activo = True
|
|
||||||
elif activo_val in ["0", "false", "f", "no"]:
|
|
||||||
activo = False
|
|
||||||
else:
|
|
||||||
activo = True # valor por defecto
|
|
||||||
|
|
||||||
idioma = row.get("idioma", None)
|
|
||||||
idioma = idioma.strip() if idioma else None
|
|
||||||
if idioma == "":
|
|
||||||
idioma = None
|
|
||||||
|
|
||||||
cursor.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO feeds (
|
|
||||||
id, nombre, descripcion, url, categoria_id, pais_id, idioma, activo, fallos
|
|
||||||
) VALUES (%(id)s, %(nombre)s, %(descripcion)s, %(url)s, %(categoria_id)s,
|
|
||||||
%(pais_id)s, %(idioma)s, %(activo)s, %(fallos)s)
|
|
||||||
ON CONFLICT (id) DO UPDATE SET
|
|
||||||
nombre = EXCLUDED.nombre,
|
|
||||||
descripcion = EXCLUDED.descripcion,
|
|
||||||
url = EXCLUDED.url,
|
|
||||||
categoria_id = EXCLUDED.categoria_id,
|
|
||||||
pais_id = EXCLUDED.pais_id,
|
|
||||||
idioma = EXCLUDED.idioma,
|
|
||||||
activo = EXCLUDED.activo,
|
|
||||||
fallos = EXCLUDED.fallos;
|
|
||||||
""",
|
|
||||||
{
|
|
||||||
"id": int(row.get("id")),
|
|
||||||
"nombre": row["nombre"],
|
|
||||||
"descripcion": row.get("descripcion") or "",
|
|
||||||
"url": row["url"],
|
|
||||||
"categoria_id": int(row["categoria_id"]) if row["categoria_id"] else None,
|
|
||||||
"pais_id": int(row["pais_id"]) if row["pais_id"] else None,
|
|
||||||
"idioma": idioma,
|
|
||||||
"activo": activo,
|
|
||||||
"fallos": int(row.get("fallos", 0)),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
n_ok += 1
|
|
||||||
except Exception as e:
|
|
||||||
app.logger.error(f"Error insertando feed fila {i}: {e}")
|
|
||||||
msg_lines.append(f"Error en fila {i}: {e}")
|
|
||||||
conn.commit()
|
|
||||||
conn.close()
|
|
||||||
msg = f"Feeds restaurados correctamente: {n_ok}"
|
|
||||||
if msg_lines:
|
|
||||||
msg += "<br>" + "<br>".join(msg_lines)
|
|
||||||
return render_template("restore_feeds.html", msg=msg)
|
|
||||||
|
|
||||||
@app.route("/noticias")
|
|
||||||
def show_noticias():
|
|
||||||
return home()
|
|
||||||
|
|
||||||
# ================================
|
# ================================
|
||||||
# Lógica de procesado de feeds con control de fallos
|
# Lógica de procesado de feeds
|
||||||
# ================================
|
# ================================
|
||||||
def sumar_fallo_feed(cursor, feed_id):
|
def sumar_fallo_feed(cursor, feed_id):
|
||||||
cursor.execute("UPDATE feeds SET fallos = fallos + 1 WHERE id = %s", (feed_id,))
|
cursor.execute("UPDATE feeds SET fallos = fallos + 1 WHERE id = %s RETURNING fallos", (feed_id,))
|
||||||
cursor.execute("SELECT fallos FROM feeds WHERE id = %s", (feed_id,))
|
|
||||||
fallos = cursor.fetchone()[0]
|
fallos = cursor.fetchone()[0]
|
||||||
if fallos >= MAX_FALLOS:
|
if fallos >= MAX_FALLOS:
|
||||||
cursor.execute("UPDATE feeds SET activo = FALSE WHERE id = %s", (feed_id,))
|
cursor.execute("UPDATE feeds SET activo = FALSE WHERE id = %s", (feed_id,))
|
||||||
|
|
@ -364,111 +227,81 @@ def resetear_fallos_feed(cursor, feed_id):
|
||||||
cursor.execute("UPDATE feeds SET fallos = 0 WHERE id = %s", (feed_id,))
|
cursor.execute("UPDATE feeds SET fallos = 0 WHERE id = %s", (feed_id,))
|
||||||
|
|
||||||
def fetch_and_store():
|
def fetch_and_store():
|
||||||
conn = None
|
with app.app_context():
|
||||||
|
app.logger.info("Iniciando ciclo de actualización de feeds...")
|
||||||
try:
|
try:
|
||||||
conn = get_conn()
|
with get_conn() as conn:
|
||||||
cursor = conn.cursor()
|
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cursor:
|
||||||
cursor.execute("SELECT id, url, categoria_id, pais_id FROM feeds WHERE activo = TRUE")
|
cursor.execute("SELECT id, url, categoria_id, pais_id FROM feeds WHERE activo = TRUE")
|
||||||
feeds_ = cursor.fetchall()
|
feeds_to_process = cursor.fetchall()
|
||||||
except psycopg2.Error as db_err:
|
|
||||||
app.logger.error(f"[DB ERROR] No se pudo conectar o leer feeds: {db_err}", exc_info=True)
|
|
||||||
return
|
|
||||||
|
|
||||||
for feed_id, rss_url, categoria_id, pais_id in feeds_:
|
for feed in feeds_to_process:
|
||||||
try:
|
try:
|
||||||
app.logger.info(f"Procesando feed: {rss_url} [{categoria_id}] [{pais_id}]")
|
app.logger.info(f"Procesando feed: {feed['url']}")
|
||||||
parsed = feedparser.parse(rss_url)
|
parsed = feedparser.parse(feed['url'])
|
||||||
except Exception as e:
|
|
||||||
app.logger.error(f"[PARSE ERROR] Al parsear {rss_url}: {e}", exc_info=True)
|
|
||||||
sumar_fallo_feed(cursor, feed_id)
|
|
||||||
continue
|
|
||||||
|
|
||||||
if getattr(parsed, "bozo", False):
|
if getattr(parsed, "bozo", False):
|
||||||
bozo_exc = getattr(parsed, "bozo_exception", "Unknown")
|
app.logger.warning(f"[BOZO] Feed mal formado: {feed['url']}")
|
||||||
app.logger.warning(f"[BOZO] Feed mal formado: {rss_url} - {bozo_exc}")
|
sumar_fallo_feed(cursor, feed['id'])
|
||||||
sumar_fallo_feed(cursor, feed_id)
|
|
||||||
continue
|
continue
|
||||||
else:
|
|
||||||
resetear_fallos_feed(cursor, feed_id)
|
resetear_fallos_feed(cursor, feed['id'])
|
||||||
|
|
||||||
for entry in parsed.entries:
|
for entry in parsed.entries:
|
||||||
link = entry.get("link") or entry.get("id")
|
|
||||||
if not link:
|
|
||||||
links_list = entry.get("links", [])
|
|
||||||
if isinstance(links_list, list) and links_list:
|
|
||||||
href = next((l.get("href") for l in links_list if l.get("href")), None)
|
|
||||||
link = href
|
|
||||||
if not link:
|
|
||||||
app.logger.error(
|
|
||||||
f"[ENTRY ERROR] Entrada sin link en feed {rss_url}, salto entrada."
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
link = entry.get("link")
|
||||||
|
if not link: continue
|
||||||
|
|
||||||
noticia_id = hashlib.md5(link.encode()).hexdigest()
|
noticia_id = hashlib.md5(link.encode()).hexdigest()
|
||||||
titulo = entry.get("title", "")
|
titulo = entry.get("title", "")
|
||||||
resumen = entry.get("summary", "")
|
resumen = entry.get("summary", "")
|
||||||
|
|
||||||
imagen_url = ""
|
imagen_url = ""
|
||||||
fecha = None
|
if "media_content" in entry and entry.media_content:
|
||||||
|
|
||||||
if "media_content" in entry:
|
|
||||||
imagen_url = entry.media_content[0].get("url", "")
|
imagen_url = entry.media_content[0].get("url", "")
|
||||||
else:
|
elif "<img" in resumen:
|
||||||
img = re.search(r"<img.+?src=\"(.+?)\"", resumen)
|
img_search = re.search(r'src="([^"]+)"', resumen)
|
||||||
if img:
|
if img_search:
|
||||||
imagen_url = img.group(1)
|
imagen_url = img_search.group(1)
|
||||||
|
|
||||||
published = entry.get("published_parsed") or entry.get("updated_parsed")
|
fecha_publicacion = None
|
||||||
if published:
|
if "published_parsed" in entry:
|
||||||
try:
|
fecha_publicacion = datetime(*entry.published_parsed[:6])
|
||||||
fecha = datetime(*published[:6])
|
elif "updated_parsed" in entry:
|
||||||
except Exception:
|
fecha_publicacion = datetime(*entry.updated_parsed[:6])
|
||||||
fecha = None
|
|
||||||
|
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO noticias (
|
INSERT INTO noticias (id, titulo, resumen, url, fecha, imagen_url, categoria_id, pais_id)
|
||||||
id, titulo, resumen, url, fecha, imagen_url, categoria_id, pais_id
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
||||||
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
|
||||||
ON CONFLICT (id) DO NOTHING
|
ON CONFLICT (id) DO NOTHING
|
||||||
""",
|
""",
|
||||||
(
|
(noticia_id, titulo, resumen, link, fecha_publicacion, imagen_url, feed['categoria_id'], feed['pais_id'])
|
||||||
noticia_id,
|
|
||||||
titulo,
|
|
||||||
resumen,
|
|
||||||
link,
|
|
||||||
fecha,
|
|
||||||
imagen_url,
|
|
||||||
categoria_id,
|
|
||||||
pais_id,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
except Exception as entry_err:
|
except Exception as entry_err:
|
||||||
app.logger.error(
|
app.logger.error(f"Error procesando entrada de feed {feed['url']}: {entry_err}", exc_info=True)
|
||||||
f"[ENTRY ERROR] Falló procesar entrada {link}: {entry_err}", exc_info=True
|
except Exception as e:
|
||||||
)
|
app.logger.error(f"[PARSE ERROR] En feed {feed['url']}: {e}", exc_info=True)
|
||||||
continue
|
sumar_fallo_feed(cursor, feed['id'])
|
||||||
|
|
||||||
|
app.logger.info(f"Ciclo de feeds completado.")
|
||||||
|
except psycopg2.Error as db_err:
|
||||||
|
app.logger.error(f"[DB ERROR] Fallo en el ciclo de actualización de feeds: {db_err}", exc_info=True)
|
||||||
|
|
||||||
try:
|
|
||||||
conn.commit()
|
|
||||||
except Exception as commit_err:
|
|
||||||
app.logger.error(
|
|
||||||
f"[DB ERROR] Al confirmar transacción: {commit_err}", exc_info=True
|
|
||||||
)
|
|
||||||
finally:
|
|
||||||
if conn:
|
|
||||||
conn.close()
|
|
||||||
app.logger.info(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Feeds procesados.")
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Lanzador de la aplicación + scheduler
|
# Lanzador de la aplicación + scheduler
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
scheduler = BackgroundScheduler()
|
# Evita que el scheduler se inicie dos veces en modo debug con el re-cargador
|
||||||
scheduler.add_job(fetch_and_store, "interval", minutes=2, id="rss_job")
|
if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':
|
||||||
|
scheduler = BackgroundScheduler(daemon=True)
|
||||||
|
scheduler.add_job(fetch_and_store, "interval", minutes=2, id="rss_job", misfire_grace_time=60)
|
||||||
scheduler.start()
|
scheduler.start()
|
||||||
try:
|
app.logger.info("Scheduler iniciado correctamente.")
|
||||||
app.run(host="0.0.0.0", port=5000, debug=True)
|
|
||||||
finally:
|
|
||||||
scheduler.shutdown()
|
|
||||||
|
|
||||||
|
# Apagar el scheduler de forma limpia al salir
|
||||||
|
import atexit
|
||||||
|
atexit.register(lambda: scheduler.shutdown())
|
||||||
|
|
||||||
|
app.run(host="0.0.0.0", port=5000, debug=True, use_reloader=True)
|
||||||
|
|
|
||||||
0
categorias.sql
Normal file → Executable file
0
categorias.sql
Normal file → Executable file
0
continentes.sql
Normal file → Executable file
0
continentes.sql
Normal file → Executable file
140
install.sh
Normal file → Executable file
140
install.sh
Normal file → Executable file
|
|
@ -4,164 +4,136 @@ set -e
|
||||||
|
|
||||||
# ========= CONFIGURACIÓN =========
|
# ========= CONFIGURACIÓN =========
|
||||||
APP_NAME="rss"
|
APP_NAME="rss"
|
||||||
APP_DIR="$(cd "$(dirname "$0")" && pwd)" # SIEMPRE el directorio del script
|
APP_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||||
PYTHON_ENV="$APP_DIR/venv"
|
PYTHON_ENV="$APP_DIR/venv"
|
||||||
SERVICE_FILE="/etc/systemd/system/$APP_NAME.service"
|
SERVICE_FILE="/etc/systemd/system/$APP_NAME.service"
|
||||||
FLASK_FILE="app.py"
|
FLASK_FILE="app:app" # Formato para Gunicorn
|
||||||
|
|
||||||
DB_NAME="rss"
|
DB_NAME="rss"
|
||||||
DB_USER="rss"
|
DB_USER="rss"
|
||||||
DB_PASS="x"
|
DB_PASS="x" # Recuerda usar una contraseña más segura en un entorno real
|
||||||
|
|
||||||
# ========= INSTALAR POSTGRESQL (ÚLTIMA VERSIÓN RECOMENDADA) =========
|
# ========= INSTALAR DEPENDENCIAS DEL SISTEMA =========
|
||||||
echo "🟢 Instalando PostgreSQL (repositorio oficial)..."
|
echo "🟢 Instalando dependencias del sistema (PostgreSQL, Python, etc.)..."
|
||||||
sudo apt update
|
sudo apt update
|
||||||
sudo apt install -y wget ca-certificates
|
sudo apt install -y wget ca-certificates postgresql postgresql-contrib python3-venv python3-pip
|
||||||
|
|
||||||
# Añade el repositorio oficial de PostgreSQL (ajusta para tu distro si hace falta)
|
|
||||||
echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
|
|
||||||
wget -qO - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
|
|
||||||
sudo apt update
|
|
||||||
sudo apt install -y postgresql postgresql-contrib
|
|
||||||
|
|
||||||
# ========= CAMBIAR AUTENTICACIÓN DE peer A md5 =========
|
# ========= CAMBIAR AUTENTICACIÓN DE peer A md5 =========
|
||||||
PG_HBA=$(find /etc/postgresql -name pg_hba.conf | head -1)
|
PG_HBA=$(find /etc/postgresql -name pg_hba.conf | head -1)
|
||||||
if grep -q "local\s\+all\s\+all\s\+peer" "$PG_HBA"; then
|
if [ -n "$PG_HBA" ] && grep -q "local\s\+all\s\+all\s\+peer" "$PG_HBA"; then
|
||||||
echo "📝 Ajustando pg_hba.conf para autenticación md5 (contraseña)..."
|
echo "📝 Ajustando pg_hba.conf para autenticación md5 (contraseña)..."
|
||||||
sudo sed -i 's/local\s\+all\s\+all\s\+peer/local all all md5/' "$PG_HBA"
|
sudo sed -i 's/local\s\+all\s\+all\s\+peer/local all all md5/' "$PG_HBA"
|
||||||
sudo systemctl restart postgresql
|
sudo systemctl restart postgresql
|
||||||
else
|
else
|
||||||
echo "✅ pg_hba.conf ya configurado para md5."
|
echo "✅ pg_hba.conf ya configurado para md5 o no se encontró."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ========= CONFIGURAR BASE DE DATOS Y USUARIO =========
|
# ========= CONFIGURAR BASE DE DATOS Y USUARIO (VERSIÓN ROBUSTA) =========
|
||||||
echo "🛠️ Configurando PostgreSQL: usuario y base de datos..."
|
echo "🛠️ Configurando PostgreSQL: usuario y base de datos..."
|
||||||
|
|
||||||
sudo -u postgres psql <<EOF
|
# Usamos un bloque DO para crear el usuario solo si no existe
|
||||||
DO \$\$
|
sudo -u postgres psql -c "DO \$\$ BEGIN IF NOT EXISTS (SELECT FROM pg_catalog.pg_user WHERE usename = '$DB_USER') THEN CREATE USER $DB_USER WITH PASSWORD '$DB_PASS'; END IF; END \$\$;"
|
||||||
BEGIN
|
|
||||||
IF NOT EXISTS (
|
|
||||||
SELECT FROM pg_catalog.pg_user WHERE usename = '$DB_USER'
|
|
||||||
) THEN
|
|
||||||
CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';
|
|
||||||
END IF;
|
|
||||||
END
|
|
||||||
\$\$;
|
|
||||||
|
|
||||||
CREATE DATABASE $DB_NAME OWNER $DB_USER;
|
# Comprobamos si la base de datos existe antes de intentar crearla
|
||||||
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
|
if ! sudo -u postgres psql -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then
|
||||||
EOF
|
sudo -u postgres psql -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;"
|
||||||
|
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;"
|
||||||
|
echo "Base de datos '$DB_NAME' creada."
|
||||||
|
else
|
||||||
|
echo "Base de datos '$DB_NAME' ya existe."
|
||||||
|
fi
|
||||||
|
|
||||||
# ========= CREAR TABLAS =========
|
# ========= CREAR TABLAS =========
|
||||||
echo "📐 Creando tablas en PostgreSQL..."
|
echo "📐 Creando tablas en PostgreSQL (si no existen)..."
|
||||||
|
|
||||||
|
# Exportamos la contraseña para que psql la use
|
||||||
export PGPASSWORD="$DB_PASS"
|
export PGPASSWORD="$DB_PASS"
|
||||||
psql -U "$DB_USER" -h localhost -d "$DB_NAME" <<EOF
|
psql -U "$DB_USER" -h localhost -d "$DB_NAME" <<EOF
|
||||||
CREATE TABLE IF NOT EXISTS continentes (
|
CREATE TABLE IF NOT EXISTS continentes (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
nombre VARCHAR(50) NOT NULL
|
nombre VARCHAR(50) NOT NULL UNIQUE
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS categorias (
|
CREATE TABLE IF NOT EXISTS categorias (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
nombre VARCHAR(100) NOT NULL
|
nombre VARCHAR(100) NOT NULL UNIQUE
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS paises (
|
CREATE TABLE IF NOT EXISTS paises (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
nombre VARCHAR(100) NOT NULL,
|
nombre VARCHAR(100) NOT NULL UNIQUE,
|
||||||
continente_id INTEGER REFERENCES continentes(id)
|
continente_id INTEGER REFERENCES continentes(id) ON DELETE SET NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS feeds (
|
CREATE TABLE IF NOT EXISTS feeds (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
nombre VARCHAR(255),
|
nombre VARCHAR(255),
|
||||||
descripcion TEXT,
|
descripcion TEXT,
|
||||||
url TEXT NOT NULL,
|
url TEXT NOT NULL UNIQUE,
|
||||||
categoria_id INTEGER REFERENCES categorias(id),
|
categoria_id INTEGER REFERENCES categorias(id) ON DELETE SET NULL,
|
||||||
pais_id INTEGER REFERENCES paises(id),
|
pais_id INTEGER REFERENCES paises(id) ON DELETE SET NULL,
|
||||||
idioma CHAR(2),
|
idioma CHAR(2),
|
||||||
activo BOOLEAN DEFAULT TRUE,
|
activo BOOLEAN DEFAULT TRUE,
|
||||||
fallos INTEGER DEFAULT 0,
|
fallos INTEGER DEFAULT 0
|
||||||
CONSTRAINT feeds_idioma_chk CHECK (idioma ~* '^[a-z]{2}$' OR idioma IS NULL)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS noticias (
|
CREATE TABLE IF NOT EXISTS noticias (
|
||||||
id VARCHAR(32) PRIMARY KEY,
|
id VARCHAR(32) PRIMARY KEY,
|
||||||
titulo TEXT,
|
titulo TEXT,
|
||||||
resumen TEXT,
|
resumen TEXT,
|
||||||
url TEXT,
|
url TEXT NOT NULL UNIQUE,
|
||||||
fecha TIMESTAMP,
|
fecha TIMESTAMP,
|
||||||
imagen_url TEXT,
|
imagen_url TEXT,
|
||||||
categoria_id INTEGER REFERENCES categorias(id),
|
categoria_id INTEGER REFERENCES categorias(id) ON DELETE SET NULL,
|
||||||
pais_id INTEGER REFERENCES paises(id)
|
pais_id INTEGER REFERENCES paises(id) ON DELETE SET NULL
|
||||||
);
|
);
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
echo "✅ Tablas creadas/verificadas correctamente."
|
echo "✅ Tablas creadas/verificadas."
|
||||||
|
|
||||||
# ========= DATOS INICIALES =========
|
# ========= DATOS INICIALES (si existen los archivos .sql) =========
|
||||||
|
for sql_file in continentes.sql paises.sql categorias.sql; do
|
||||||
# Continentes
|
if [ -f "$APP_DIR/$sql_file" ]; then
|
||||||
if [ -f "$APP_DIR/continentes.sql" ]; then
|
echo "🗂️ Insertando datos desde $sql_file..."
|
||||||
echo "🌎 Insertando continentes..."
|
psql -U "$DB_USER" -h localhost -d "$DB_NAME" -f "$APP_DIR/$sql_file"
|
||||||
psql -U "$DB_USER" -h localhost -d "$DB_NAME" -f "$APP_DIR/continentes.sql"
|
|
||||||
else
|
else
|
||||||
echo "⚠️ No se encontró $APP_DIR/continentes.sql"
|
echo "⚠️ No se encontró el archivo de datos iniciales: $sql_file"
|
||||||
fi
|
|
||||||
|
|
||||||
# Países
|
|
||||||
if [ -f "$APP_DIR/paises.sql" ]; then
|
|
||||||
echo "🌐 Insertando países..."
|
|
||||||
psql -U "$DB_USER" -h localhost -d "$DB_NAME" -f "$APP_DIR/paises.sql"
|
|
||||||
else
|
|
||||||
echo "⚠️ No se encontró $APP_DIR/paises.sql"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Categorías
|
|
||||||
if [ -f "$APP_DIR/categorias.sql" ]; then
|
|
||||||
echo "🏷️ Insertando categorías estándar..."
|
|
||||||
psql -U "$DB_USER" -h localhost -d "$DB_NAME" -f "$APP_DIR/categorias.sql"
|
|
||||||
else
|
|
||||||
echo "⚠️ No se encontró $APP_DIR/categorias.sql"
|
|
||||||
fi
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
# ========= ENTORNO PYTHON =========
|
# ========= ENTORNO PYTHON =========
|
||||||
echo "🌀 Creando entorno virtual en $PYTHON_ENV"
|
echo "🐍 Creando entorno virtual en $PYTHON_ENV"
|
||||||
python3 -m venv "$PYTHON_ENV"
|
python3 -m venv "$PYTHON_ENV"
|
||||||
|
|
||||||
echo "📦 Instalando dependencias"
|
echo "📦 Instalando dependencias desde requirements.txt"
|
||||||
source "$PYTHON_ENV/bin/activate"
|
source "$PYTHON_ENV/bin/activate"
|
||||||
pip install --upgrade pip
|
pip install --upgrade pip
|
||||||
pip install -r "$APP_DIR/requirements.txt"
|
pip install -r "$APP_DIR/requirements.txt"
|
||||||
deactivate
|
deactivate
|
||||||
|
|
||||||
# ========= SYSTEMD SERVICE =========
|
# ========= SYSTEMD SERVICE (con Gunicorn) =========
|
||||||
echo "⚙️ Creando servicio systemd: $SERVICE_FILE"
|
echo "⚙️ Creando servicio systemd: $SERVICE_FILE"
|
||||||
sudo tee "$SERVICE_FILE" > /dev/null <<EOF
|
sudo tee "$SERVICE_FILE" > /dev/null <<EOF
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=RSS Flask App
|
Description=Servicio del Agregador RSS de Noticias
|
||||||
After=network.target
|
After=network.target postgresql.service
|
||||||
|
Requires=postgresql.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
User=$USER
|
User=$USER
|
||||||
|
Group=$(id -gn $USER)
|
||||||
WorkingDirectory=$APP_DIR
|
WorkingDirectory=$APP_DIR
|
||||||
ExecStart=$PYTHON_ENV/bin/python $FLASK_FILE
|
ExecStart=$PYTHON_ENV/bin/gunicorn --workers 3 --bind unix:rss.sock -m 007 $FLASK_FILE
|
||||||
Restart=always
|
Restart=always
|
||||||
Environment=PYTHONUNBUFFERED=1
|
Environment="PYTHONUNBUFFERED=1"
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# ========= ACTIVAR SERVICIO =========
|
# ========= ACTIVAR SERVICIO =========
|
||||||
echo "🔁 Recargando systemd y activando servicio"
|
echo "🚀 Recargando systemd y activando el servicio '$APP_NAME'"
|
||||||
sudo systemctl daemon-reexec
|
|
||||||
sudo systemctl daemon-reload
|
sudo systemctl daemon-reload
|
||||||
sudo systemctl enable "$APP_NAME"
|
sudo systemctl enable --now "$APP_NAME"
|
||||||
sudo systemctl restart "$APP_NAME"
|
# sudo systemctl restart "$APP_NAME" # --now en enable ya lo inicia
|
||||||
|
|
||||||
# ========= ESTADO =========
|
|
||||||
echo "✅ Servicio '$APP_NAME' instalado y funcionando."
|
|
||||||
echo "👉 Revisa con: systemctl status $APP_NAME"
|
|
||||||
|
|
||||||
|
# ========= ESTADO FINAL =========
|
||||||
|
echo "✅ ¡Instalación completada!"
|
||||||
|
echo "Para revisar el estado del servicio, ejecuta: systemctl status $APP_NAME"
|
||||||
|
echo "Para ver los logs en tiempo real, ejecuta: journalctl -u $APP_NAME -f"
|
||||||
|
|
|
||||||
0
paises.sql
Normal file → Executable file
0
paises.sql
Normal file → Executable file
4
requirements.txt
Normal file → Executable file
4
requirements.txt
Normal file → Executable file
|
|
@ -1,4 +1,6 @@
|
||||||
Flask==2.3.3
|
Flask==2.3.3
|
||||||
feedparser==6.0.11
|
feedparser==6.0.11
|
||||||
APScheduler==3.10.4
|
APScheduler==3.10.4
|
||||||
psycopg2-binary==2.9.10 # conector PostgreSQL
|
psycopg2-binary==2.9.10
|
||||||
|
bleach==6.1.0
|
||||||
|
gunicorn==22.0.0
|
||||||
|
|
|
||||||
0
templates/base.html
Normal file → Executable file
0
templates/base.html
Normal file → Executable file
0
templates/edit_feed.html
Normal file → Executable file
0
templates/edit_feed.html
Normal file → Executable file
0
templates/index.html
Normal file → Executable file
0
templates/index.html
Normal file → Executable file
0
templates/noticias.html
Normal file → Executable file
0
templates/noticias.html
Normal file → Executable file
0
templates/restore_feeds.html
Normal file → Executable file
0
templates/restore_feeds.html
Normal file → Executable file
Loading…
Add table
Add a link
Reference in a new issue