feat: make scraper and job service account-aware
- OptimizedTelegramScraper accepts account_id parameter; sets DATA_DIR to data/accounts/<id>/ when provided - Session path uses account_session_path() for per-account session files - initialize_client() reads per-account credentials from account state store - ScraperJobService passes account_id from payload to scraper constructor - Per-account state loaded from AccountStateStore before scraping
This commit is contained in:
1 parent
6e3966aeb5
commit
220d04b3a6
2 files changed
+34
-9
No files matched your search
@@ -21,7 +21,14 @@ from telethon.tl.types import (
|
||||
)
|
||||
from telethon.errors import FloodWaitError, SessionPasswordNeededError
|
||||
import qrcode
|
||||
from app_state import StateStore
|
||||
from app_state import (
|
||||
StateStore,
|
||||
account_data_dir,
|
||||
account_session_path,
|
||||
get_account_store,
|
||||
load_account,
|
||||
save_account,
|
||||
)
|
||||
|
||||
warnings.filterwarnings(
|
||||
"ignore", message="Using async sessions support is an experimental feature"
|
||||
@@ -73,13 +80,21 @@ class ForwardingRule:
|
||||
|
||||
|
||||
class OptimizedTelegramScraper:
|
||||
def __init__(self):
|
||||
self.DATA_DIR = Path("data")
|
||||
def __init__(self, account_id: Optional[str] = None):
|
||||
self.account_id = account_id
|
||||
self.SESSION_DIR = Path("session")
|
||||
self.DATA_DIR.mkdir(exist_ok=True)
|
||||
self.SESSION_DIR.mkdir(exist_ok=True)
|
||||
|
||||
if account_id:
|
||||
self.DATA_DIR = Path("data") / "accounts" / account_id
|
||||
self.state_store = get_account_store(Path("data"), account_id)
|
||||
else:
|
||||
self.DATA_DIR = Path("data")
|
||||
self.state_store = StateStore(self.DATA_DIR / "state.json")
|
||||
|
||||
self.DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||||
self.STATE_FILE = str(self.DATA_DIR / "state.json")
|
||||
self.state_store = StateStore(self.DATA_DIR / "state.json")
|
||||
|
||||
self.state = self.load_state()
|
||||
self.client = None
|
||||
self.continuous_scraping_active = False
|
||||
@@ -1480,8 +1495,9 @@ class OptimizedTelegramScraper:
|
||||
print("Invalid API ID. Must be a number.")
|
||||
return False
|
||||
|
||||
session = account_session_path(self.SESSION_DIR, self.account_id or "session")
|
||||
self.client = TelegramClient(
|
||||
str(self.SESSION_DIR / "session"),
|
||||
session,
|
||||
self.state["api_id"],
|
||||
self.state["api_hash"],
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user