Spaces:
Sleeping
Sleeping
Download Backend/app/JSONStorage.py from orakzai-io/Async_Web_scraper: direct link, hf CLI and curl.
- Browser
- Download file 1.34 kB
-
https://huggingface.co/spaces/orakzai-io/Async_Web_scraper/resolve/main/Backend/app/JSONStorage.py
- Command line
-
hf download hf://spaces/orakzai-io/Async_Web_scraper/Backend/app/JSONStorage.py
-
curl -L -o JSONStorage.py https://huggingface.co/spaces/orakzai-io/Async_Web_scraper/resolve/main/Backend/app/JSONStorage.py
1.34 kB
| import json | |
| import aiofiles | |
| import asyncio | |
| class JSONStorage: | |
| def __init__(self): | |
| # The Lock ensures that multiple async tasks don't | |
| # write to the same file at the exact same time. | |
| self._lock = asyncio.Lock() | |
| async def save_jsonl(self, new_data, path="data.jsonl"): | |
| """ | |
| Asynchronously appends a single dictionary as a JSON line. | |
| Strictly UTF-8 for global character support. | |
| """ | |
| async with self._lock: | |
| try: | |
| async with aiofiles.open(path, mode="a", encoding="utf-8") as f: | |
| # ensure_ascii=False keeps your UTF-8 characters readable | |
| line = json.dumps(new_data, ensure_ascii=False) | |
| await f.write(line + "\n") | |
| except Exception as e: | |
| print(f"❌ Storage Error: {e}") | |
| async def load_all(self, path="data.jsonl"): | |
| """ | |
| Reads the JSONL file and returns a list of dictionaries. | |
| """ | |
| data = [] | |
| try: | |
| async with aiofiles.open(path, mode="r", encoding="utf-8") as f: | |
| async for line in f: | |
| if line.strip(): | |
| data.append(json.loads(line)) | |
| except FileNotFoundError: | |
| pass | |
| return data | |