mirror of
https://github.com/etienne-hd/lbc-finder.git
synced 2026-04-30 20:55:36 +02:00
Initial release
This commit is contained in:
36
searcher/id.py
Normal file
36
searcher/id.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from .logger import logger
|
||||
|
||||
from typing import List, Final
|
||||
import os
|
||||
import json
|
||||
|
||||
MAX_ID: Final[int] = 10_000
|
||||
|
||||
class ID:
|
||||
def __init__(self):
|
||||
self._ids: List[str] = self._get_ids()
|
||||
|
||||
@property
|
||||
def ids(self) -> List[str]:
|
||||
return self._ids
|
||||
|
||||
def _get_ids(self) -> List[str]:
|
||||
ids: List[str] = []
|
||||
if os.path.exists("id.json"):
|
||||
with open("id.json", "r") as f:
|
||||
try:
|
||||
ids = json.load(f)
|
||||
except json.JSONDecodeError:
|
||||
os.remove("id.json")
|
||||
except:
|
||||
logger.exception("An error occurred while attempting to open the id.json file.")
|
||||
return ids
|
||||
|
||||
def add(self, id: str) -> bool:
|
||||
if not id in self._ids:
|
||||
self._ids.append(id)
|
||||
with open("id.json", "w") as f:
|
||||
json.dump(self._ids[-MAX_ID:], f, indent=3)
|
||||
self._ids = self._ids[-MAX_ID:]
|
||||
return True
|
||||
return False
|
||||
Reference in New Issue
Block a user