Spaces:
Paused
Paused
| import asyncio | |
| import aiofiles | |
| import os | |
| from logger import get_logger | |
| log = get_logger() | |
| class AppState: | |
| def __init__(self): | |
| self.status = "STOPPED" # STOPPED, RUNNING, PAUSED | |
| self.concurrency = 10 | |
| self.min_length = 4 | |
| self.total_words = 0 | |
| self.processed = 0 | |
| self.counts = {"taken": 0, "unavailable": 0, "sold": 0, "auction": 0, "available": 0, "forsale": 0} | |
| self.queue = None # Will be initialized in the event loop | |
| self.proxies = [] | |
| # Ensure output directory exists | |
| if not os.path.exists("results"): | |
| os.makedirs("results") | |
| async def init_queue(self): | |
| if self.queue is None: | |
| self.queue = asyncio.Queue() | |
| async def add_to_queue(self, words): | |
| await self.init_queue() | |
| for w in words: | |
| await self.queue.put(w) | |
| self.total_words = self.queue.qsize() + self.processed | |
| async def save_result(self, word, status): | |
| self.processed += 1 | |
| clean_status = status.lower().replace("_", "") | |
| if clean_status in self.counts: | |
| self.counts[clean_status] += 1 | |
| # Append immediately to disk so we never lose data on crash | |
| filename = f"results/{clean_status}.txt" | |
| try: | |
| async with aiofiles.open(filename, "a", encoding="utf-8") as f: | |
| await f.write(f"{word}\n") | |
| except Exception as e: | |
| log.error(f"Failed to write {word} to {filename}: {e}") | |
| # Global singleton instance | |
| state = AppState() |