Spaces:
Sleeping
Sleeping
Download TruthScan AI_backend/app/storage.py from Bjornpool/truthscan-ai-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.62 kB
-
https://huggingface.co/spaces/Bjornpool/truthscan-ai-backend/resolve/982bc35d1541cb4935b1807d664ed40e5ffc815b/TruthScan%20AI_backend/app/storage.py
- Command line
-
hf download 'hf://spaces/Bjornpool/truthscan-ai-backend@982bc35d1541cb4935b1807d664ed40e5ffc815b/TruthScan AI_backend/app/storage.py'
-
curl -L -o storage.py https://huggingface.co/spaces/Bjornpool/truthscan-ai-backend/resolve/982bc35d1541cb4935b1807d664ed40e5ffc815b/TruthScan%20AI_backend/app/storage.py
1.62 kB
| """ | |
| Warstwa dost臋pu do danych dla zapisanych artyku艂贸w (plik JSON). | |
| """ | |
| import json | |
| import threading | |
| from fastapi import HTTPException | |
| from .config import SAVED_FILE | |
| # Blokada w膮tk贸w dla operacji zapisu/odczytu | |
| _lock = threading.Lock() | |
| def ensure_file(): | |
| # Tworzy plik danych, je艣li nie istnieje | |
| if not SAVED_FILE.exists(): | |
| SAVED_FILE.write_text("[]", encoding="utf-8") | |
| def read_all(): | |
| # Zwraca wszystkie zapisane artyku艂y | |
| ensure_file() | |
| try: | |
| return json.loads(SAVED_FILE.read_text(encoding="utf-8")) | |
| except json.JSONDecodeError: | |
| return [] | |
| def append_article(article: dict): | |
| # Dodaje nowy artyku艂 do pliku JSON | |
| ensure_file() | |
| try: | |
| with _lock, open(SAVED_FILE, "r+", encoding="utf-8") as f: | |
| data = json.load(f) | |
| data.append(article) | |
| f.seek(0) | |
| json.dump(data, f, ensure_ascii=False, indent=4) | |
| return {"message": "Artyku艂 zapisany pomy艣lnie."} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| def delete_by_title(title: str): | |
| # Usuwa artyku艂 na podstawie tytu艂u | |
| ensure_file() | |
| try: | |
| with _lock, open(SAVED_FILE, "r+", encoding="utf-8") as f: | |
| saved = json.load(f) | |
| new_saved = [ | |
| a for a in saved if a.get("title") != title | |
| ] | |
| f.seek(0) | |
| f.truncate() | |
| json.dump(new_saved, f, ensure_ascii=False, indent=4) | |
| return {"message": "Artyku艂 usuni臋ty."} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |