Initial release

This commit is contained in:
etienne-hd
2025-08-21 23:50:34 +02:00
commit dfbbcbdbbb
15 changed files with 618 additions and 0 deletions

2
searcher/__init__.py Normal file
View File

@@ -0,0 +1,2 @@
from .searcher import Searcher
from .logger import logger

36
searcher/id.py Normal file
View 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

23
searcher/logger.py Normal file
View File

@@ -0,0 +1,23 @@
import logging
import os
from datetime import datetime
# File management
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
file_path: str = os.path.join("logs", f"log_{timestamp}.log")
os.makedirs("logs", exist_ok=True)
# Config logging
logger = logging.getLogger("lbc-finder")
formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] [%(threadName)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
logger.setLevel(logging.INFO)
# Log File
file_handler = logging.FileHandler(file_path, mode='w', encoding='utf-8')
file_handler.setLevel(logging.WARNING)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

40
searcher/searcher.py Normal file
View File

@@ -0,0 +1,40 @@
from models import Search
from lbc import Client, Sort
from .id import ID
from .logger import logger
import time
import threading
from typing import List, Union
class Searcher:
def __init__(self, searches: Union[List[Search], Search], request_verify: bool = True):
self._searches: List[Search] = searches if isinstance(searches, list) else [searches]
self._request_verify = request_verify
self._id = ID()
def _search(self, search: Search) -> None:
client = Client(proxy=search.proxy, request_verify=self._request_verify)
while True:
before = time.time()
try:
response = client.search(**search.parameters._kwargs, sort=Sort.NEWEST)
logger.debug(f"Successfully found {response.total} ad{'s' if response.total > 1 else ''}.")
ads = [ad for ad in response.ads if self._id.add(ad.id)]
if len(ads):
logger.info(f"Successfully found {len(ads)} new ad{'s' if len(ads) > 1 else ''}!")
for ad in ads:
search.handler(ad, search.name)
except:
logger.exception(f"An error occured.")
time.sleep(search.delay - (time.time() - before) if search.delay - (time.time() - before) > 0 else 0)
def start(self) -> bool:
if not len(self._searches):
logger.warning("No search rules have been set. Please create search rules in config.py (see example in README.md).")
return False
for search in self._searches:
threading.Thread(target=self._search, args=(search,), name=search.name).start()
time.sleep(5) # Add latency between each thread to prevent spam
return True