Spaces:
Paused
Paused
File size: 1,583 Bytes
61b042a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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() |