chore: Removed SQLite usage
This commit is contained in:
1 parent
1f216a8e8d
commit
35d63c3726
5 files changed
+7
-130
No files matched your search
@@ -7,8 +7,6 @@ RUN pip install --no-cache-dir -r requirements.txt
|
|||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
RUN mkdir -p /app/data
|
|
||||||
|
|
||||||
EXPOSE 5000
|
EXPOSE 5000
|
||||||
|
|
||||||
CMD ["python", "app.py"]
|
CMD ["python", "app.py"]
|
||||||
@@ -5,7 +5,6 @@ from dotenv import load_dotenv
|
|||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
import db
|
|
||||||
from clients import jellyfin
|
from clients import jellyfin
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@@ -94,7 +93,6 @@ def index():
|
|||||||
"JELLYFIN_API_KEY": os.environ.get("JELLYFIN_API_KEY", ""),
|
"JELLYFIN_API_KEY": os.environ.get("JELLYFIN_API_KEY", ""),
|
||||||
"JELLYFIN_USER_ID": os.environ.get("JELLYFIN_USER_ID", ""),
|
"JELLYFIN_USER_ID": os.environ.get("JELLYFIN_USER_ID", ""),
|
||||||
"TOTAL_EPISODES": os.environ.get("TOTAL_EPISODES", "1155"),
|
"TOTAL_EPISODES": os.environ.get("TOTAL_EPISODES", "1155"),
|
||||||
"DB_PATH": os.environ.get("DB_PATH", "/app/data/episodes.db"),
|
|
||||||
}
|
}
|
||||||
latest = get_latest_sunday()
|
latest = get_latest_sunday()
|
||||||
previous = get_previous_sunday()
|
previous = get_previous_sunday()
|
||||||
@@ -111,10 +109,10 @@ def index():
|
|||||||
|
|
||||||
@app.route("/api/report")
|
@app.route("/api/report")
|
||||||
def report():
|
def report():
|
||||||
week_start, week_end, _ = current_week() # This is the week range for display (e.g., Feb 15-21)
|
week_start, week_end, _ = current_week()
|
||||||
total_episodes = int(os.environ.get("TOTAL_EPISODES", 1155))
|
total_episodes = int(os.environ.get("TOTAL_EPISODES", 1155))
|
||||||
latest = get_latest_sunday() # e.g., 2026-02-21T16:00:00.000Z
|
latest = get_latest_sunday()
|
||||||
previous = get_previous_sunday() # e.g., 2026-02-14T16:00:00.000Z
|
previous = get_previous_sunday()
|
||||||
|
|
||||||
jellyfin.clear_debug_logs()
|
jellyfin.clear_debug_logs()
|
||||||
|
|
||||||
@@ -135,19 +133,8 @@ def report():
|
|||||||
prev_ep_info = jellyfin.find_last_one_piece_before_previous_sunday(previous_sunday_dt, user_id)
|
prev_ep_info = jellyfin.find_last_one_piece_before_previous_sunday(previous_sunday_dt, user_id)
|
||||||
previous_episode = prev_ep_info["episode_number"]
|
previous_episode = prev_ep_info["episode_number"]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_debug.add(f"✗ Could not find previous Sunday episode: {e}")
|
debug_output = jellyfin.get_debug_logs()
|
||||||
_debug.add("✓ Falling back to DB seed or latest episode")
|
return jsonify({"error": f"Could not find previous Sunday episode: {e}", "debug": debug_output}), 502
|
||||||
prev = db.get_previous_episode(week_start.isoformat())
|
|
||||||
previous_episode = prev["episode_number"] if prev else ep_info["episode_number"]
|
|
||||||
|
|
||||||
db.save_episode(
|
|
||||||
episode_number=ep_info["episode_number"],
|
|
||||||
week_start=week_start.isoformat(),
|
|
||||||
week_end=week_end.isoformat(),
|
|
||||||
item_id=ep_info.get("item_id"),
|
|
||||||
media_name=ep_info.get("media_name"),
|
|
||||||
watched_at=ep_info.get("watched_at"),
|
|
||||||
)
|
|
||||||
|
|
||||||
watched_this_week = ep_info["episode_number"] - previous_episode
|
watched_this_week = ep_info["episode_number"] - previous_episode
|
||||||
|
|
||||||
@@ -179,11 +166,11 @@ def report():
|
|||||||
|
|
||||||
@app.route("/api/history")
|
@app.route("/api/history")
|
||||||
def history():
|
def history():
|
||||||
return jsonify(db.get_all_history())
|
"""History endpoint removed — SQLite not used anymore. Use Jellyfin ActivityLog directly."""
|
||||||
|
return jsonify({"message": "History tracking via SQLite has been removed. All data is fetched live from Jellyfin."})
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
db.init_db()
|
|
||||||
port = int(os.environ.get("PORT", 5000))
|
port = int(os.environ.get("PORT", 5000))
|
||||||
app.run(host="0.0.0.0", port=port, debug=False)
|
app.run(host="0.0.0.0", port=port, debug=False)
|
||||||
|
|
||||||
@@ -1,102 +0,0 @@
|
|||||||
"""SQLite persistence for weekly episode tracking."""
|
|
||||||
import os
|
|
||||||
import sqlite3
|
|
||||||
from datetime import datetime, timezone, timedelta
|
|
||||||
from contextlib import contextmanager
|
|
||||||
|
|
||||||
DB_PATH = os.environ.get("DB_PATH", "/app/data/episodes.db")
|
|
||||||
SEED_EPISODE = 928
|
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
|
||||||
def _conn():
|
|
||||||
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
|
||||||
con = sqlite3.connect(DB_PATH)
|
|
||||||
con.row_factory = sqlite3.Row
|
|
||||||
try:
|
|
||||||
yield con
|
|
||||||
con.commit()
|
|
||||||
finally:
|
|
||||||
con.close()
|
|
||||||
|
|
||||||
|
|
||||||
def init_db():
|
|
||||||
"""Create table and seed with the initial previous-week episode if empty."""
|
|
||||||
with _conn() as con:
|
|
||||||
con.execute("""
|
|
||||||
CREATE TABLE IF NOT EXISTS weekly_episodes (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
episode_number INTEGER NOT NULL,
|
|
||||||
week_start TEXT NOT NULL,
|
|
||||||
week_end TEXT NOT NULL,
|
|
||||||
item_id TEXT,
|
|
||||||
media_name TEXT,
|
|
||||||
watched_at TEXT,
|
|
||||||
recorded_at TEXT DEFAULT (datetime('now'))
|
|
||||||
)
|
|
||||||
""")
|
|
||||||
count = con.execute("SELECT COUNT(*) FROM weekly_episodes").fetchone()[0]
|
|
||||||
if count == 0:
|
|
||||||
_seed(con)
|
|
||||||
|
|
||||||
|
|
||||||
def _seed(con):
|
|
||||||
"""Seed with episode 928 for the week before the current week."""
|
|
||||||
from datetime import date
|
|
||||||
PHT = timezone(timedelta(hours=8))
|
|
||||||
now_pht = datetime.now(PHT)
|
|
||||||
wd = now_pht.weekday()
|
|
||||||
days_since_sunday = (wd + 1) % 7
|
|
||||||
if wd == 6:
|
|
||||||
days_since_sunday = 7
|
|
||||||
current_sunday = (now_pht - timedelta(days=days_since_sunday)).date()
|
|
||||||
prev_sunday = current_sunday - timedelta(weeks=1)
|
|
||||||
prev_saturday = prev_sunday + timedelta(days=6)
|
|
||||||
con.execute(
|
|
||||||
"INSERT INTO weekly_episodes (episode_number, week_start, week_end, media_name) VALUES (?,?,?,?)",
|
|
||||||
(SEED_EPISODE, prev_sunday.isoformat(), prev_saturday.isoformat(), "seed"),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def save_episode(episode_number: int, week_start: str, week_end: str,
|
|
||||||
item_id: str = None, media_name: str = None, watched_at: str = None):
|
|
||||||
"""Insert or replace the episode record for this week."""
|
|
||||||
with _conn() as con:
|
|
||||||
existing = con.execute(
|
|
||||||
"SELECT id FROM weekly_episodes WHERE week_start = ?", (week_start,)
|
|
||||||
).fetchone()
|
|
||||||
if existing:
|
|
||||||
con.execute(
|
|
||||||
"""UPDATE weekly_episodes
|
|
||||||
SET episode_number=?, item_id=?, media_name=?, watched_at=?, recorded_at=datetime('now')
|
|
||||||
WHERE week_start=?""",
|
|
||||||
(episode_number, item_id, media_name, watched_at, week_start),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
con.execute(
|
|
||||||
"""INSERT INTO weekly_episodes
|
|
||||||
(episode_number, week_start, week_end, item_id, media_name, watched_at)
|
|
||||||
VALUES (?,?,?,?,?,?)""",
|
|
||||||
(episode_number, week_start, week_end, item_id, media_name, watched_at),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_previous_episode(week_start: str) -> dict | None:
|
|
||||||
"""Return the most recent episode record strictly before week_start."""
|
|
||||||
with _conn() as con:
|
|
||||||
row = con.execute(
|
|
||||||
"""SELECT * FROM weekly_episodes
|
|
||||||
WHERE week_start < ?
|
|
||||||
ORDER BY week_start DESC LIMIT 1""",
|
|
||||||
(week_start,),
|
|
||||||
).fetchone()
|
|
||||||
return dict(row) if row else None
|
|
||||||
|
|
||||||
|
|
||||||
def get_all_history() -> list:
|
|
||||||
"""Return all weekly episode records, newest first."""
|
|
||||||
with _conn() as con:
|
|
||||||
rows = con.execute(
|
|
||||||
"SELECT * FROM weekly_episodes ORDER BY week_start DESC"
|
|
||||||
).fetchall()
|
|
||||||
return [dict(r) for r in rows]
|
|
||||||
@@ -5,9 +5,4 @@ services:
|
|||||||
- "5001:5000"
|
- "5001:5000"
|
||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
volumes:
|
|
||||||
- episodes_data:/app/data
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
volumes:
|
|
||||||
episodes_data:
|
|
||||||
@@ -101,7 +101,6 @@ PREVIOUS_SUNDAY={{ previous_sunday }}
|
|||||||
JELLYFIN_HOST={{ config.JELLYFIN_HOST }}
|
JELLYFIN_HOST={{ config.JELLYFIN_HOST }}
|
||||||
JELLYFIN_USER_ID={{ config.JELLYFIN_USER_ID }}
|
JELLYFIN_USER_ID={{ config.JELLYFIN_USER_ID }}
|
||||||
TOTAL_EPISODES={{ config.TOTAL_EPISODES }}
|
TOTAL_EPISODES={{ config.TOTAL_EPISODES }}
|
||||||
DB_PATH={{ config.DB_PATH }}
|
|
||||||
|
|
||||||
(Report output will appear here after Generate Report is clicked)
|
(Report output will appear here after Generate Report is clicked)
|
||||||
</pre>
|
</pre>
|
||||||
|
|||||||
Reference in new issue
Block a user