-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
665 lines (553 loc) · 22.7 KB
/
Copy pathbot.py
File metadata and controls
665 lines (553 loc) · 22.7 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
"""
bot.py — Telegram-Bot für LeadBot-Steuerung
Verfügbare Commands:
/find [Branche] [Stadt] — Startet eine gezielte Suche
/hunt [Stadt] — Startet den automatischen 20-Branchen-Hunt
/stats — Zeigt Lead-Statistiken an
/best — Zeigt den Lead mit den meisten Problemen
Sicherheit: Alle Commands prüfen die AUTHORIZED_USER_ID aus .env.
"""
import argparse
import asyncio
import json
import logging
import os
import re
import signal
import sys
from pathlib import Path
from typing import Optional
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, ContextTypes, filters
from scraper import (
init_scraper_async,
shutdown_scraper_async,
hunt_leads,
analyze_website_async,
run_hunt_async,
save_lead,
HUNT_BRANCHES,
get_stats,
get_best_lead,
LeadsTable,
SEARCH_RADIUS_KM as SCRAPER_RADIUS,
)
logger = logging.getLogger("leadbot.bot")
# ─── Globale Zustandsvariablen ───────────────────────────────────────────────
AUTHORIZED_USER_ID: Optional[int] = None
active_searches: int = 0
hunt_in_progress: bool = False
hunt_progress_data: dict = {"completed": 0, "total": 0, "leads": 0}
hunt_chat_id: Optional[int] = None
hunt_app: Optional[Application] = None
RADIUS_CONFIG_PATH = Path(__file__).parent / "db" / "radius.json"
SEARCH_RADIUS_KM: int = 10
# Background-Mode Flag (gesetzt via argparse --background)
_HEADLESS_MODE: bool = False
def _load_radius() -> int:
"""Lädt den gespeicherten Radius aus der JSON-Datei."""
global SEARCH_RADIUS_KM
try:
if RADIUS_CONFIG_PATH.exists():
data = json.loads(RADIUS_CONFIG_PATH.read_text())
SEARCH_RADIUS_KM = int(data.get("radius_km", 10))
except Exception:
SEARCH_RADIUS_KM = 10
return SEARCH_RADIUS_KM
def _save_radius(radius: int):
"""Speichert den Radius in die JSON-Datei."""
global SEARCH_RADIUS_KM
SEARCH_RADIUS_KM = radius
try:
RADIUS_CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
RADIUS_CONFIG_PATH.write_text(json.dumps({"radius_km": radius}))
except Exception as e:
logger.error("Radius speichern fehlgeschlagen: %s", e)
def init_bot():
global AUTHORIZED_USER_ID
user_id = os.getenv("AUTHORIZED_USER_ID")
if user_id:
AUTHORIZED_USER_ID = int(user_id)
logger.info("Autorisierte User-ID gesetzt: %s", AUTHORIZED_USER_ID)
else:
logger.warning(
"AUTHORIZED_USER_ID nicht gesetzt — "
"der Bot wird auf ALLE Benutzer antworten (unsicher!)"
)
_load_radius()
# Scraper-Modul synchronisieren
import scraper
scraper.SEARCH_RADIUS_KM = SEARCH_RADIUS_KM
logger.info("Radius geladen: %dkm", SEARCH_RADIUS_KM)
def is_authorized(update: Update) -> bool:
if AUTHORIZED_USER_ID is None:
return True
user_id = update.effective_user.id
if user_id != AUTHORIZED_USER_ID:
logger.warning("Unautorisierter Zugriffsversuch von User-ID %s", user_id)
return False
return True
async def cmd_find(update: Update, context: ContextTypes.DEFAULT_TYPE):
global active_searches
if not is_authorized(update):
await update.message.reply_text("Zugriff verweigert.")
return
args = context.args
if len(args) < 2:
await update.message.reply_text(
"Usage: /find [Branche] [Stadt]\n"
"Beispiel: /find dachdecker Berlin"
)
return
branche = args[0]
stadt = " ".join(args[1:])
active_searches += 1
await update.message.reply_text(
f"Suche nach {branche} in {stadt}...\n"
f"📍 Radius: {SEARCH_RADIUS_KM}km\n"
f"(Google Maps + Verzeichnisse) Bitte warten."
)
try:
results = await hunt_leads(branche, stadt, max_results=15, radius_km=SEARCH_RADIUS_KM)
if not results:
await update.message.reply_text("Keine Ergebnisse gefunden.")
active_searches -= 1
return
await update.message.reply_text(f"🔍 {len(results)} Unternehmen gefunden. Analysiere jetzt...")
processed = 0
for biz in results:
website = biz.get("website", "")
if website:
quality = await analyze_website_async(website, biz.get("name", ""))
else:
quality = {
"has_ssl": False, "has_viewport": False, "has_copyright": False,
"copyright_year": 0, "copyright_age": 0, "is_perfect": False,
"qualifies": True,
"issues": ["Keine Website gefunden"],
"reasons": ["Keine Website gefunden"],
"design_criticism": ["Keine Website vorhanden"],
"uses_table_layout": False, "uses_deprecated_html": False,
"uses_system_fonts": False,
"phone": "", "screenshot_path": "",
"impressum_data": {"name": "", "phone": "", "email": "", "impressum_url": ""},
}
impressum = quality.get("impressum_data", {})
lead_data = {
"name": biz.get("name", ""),
"website": website,
"phone": biz.get("phone", ""),
"email": biz.get("email", ""),
"address": biz.get("address", ""),
"contact_name": impressum.get("name", ""),
"contact_phone": impressum.get("phone", ""),
"contact_email": impressum.get("email", ""),
"impressum_url": impressum.get("impressum_url", ""),
"google_rating": biz.get("rating", 0),
"branche": branche,
"stadt": stadt,
"source": biz.get("source", "google"),
"quality": quality,
}
saved = save_lead(lead_data)
if saved:
processed += 1
reasons_str = ", ".join(quality.get("reasons", []))
email_display = biz.get('email', '') or impressum.get('email', '') or 'N/A'
# SOFORTIGE Telegram-Nachricht pro Lead
message_text = (
f"🚀 *Neuer Lead gefunden!*\n\n"
f"🏢 *Firma:* {biz.get('name', 'Unbekannt')}\n"
f"🌐 *Web:* {website or 'N/A'}\n"
f"✉️ *Email:* {email_display}\n"
f"📞 *Tel:* {impressum.get('phone') or biz.get('phone', 'N/A')}\n"
f"👤 *Kontakt:* {impressum.get('name', 'N/A')}\n"
f"⭐ *Bewertung:* {biz.get('rating', 0)}\n"
f"⚠️ *Probleme:* {reasons_str}\n"
f"📍 *Quelle:* {biz.get('source', 'unknown')}"
)
await update.message.reply_text(message_text, parse_mode="Markdown")
if processed == 0:
await update.message.reply_text(
"Alle gefundenen Websites sind einwandfrei oder haben "
"zu wenige Probleme — keine qualifizierten Leads entdeckt."
)
else:
await update.message.reply_text(f"✅ Fertig! {processed} Leads gespeichert.")
except Exception as e:
logger.error("Suche fehlgeschlagen: %s", e)
await update.message.reply_text(f"Fehler bei der Suche: {e}")
finally:
active_searches -= 1
async def cmd_hunt(update: Update, context: ContextTypes.DEFAULT_TYPE):
global hunt_in_progress, hunt_progress_data, hunt_chat_id, hunt_app
if not is_authorized(update):
await update.message.reply_text("Zugriff verweigert.")
return
args = context.args
if not args:
await update.message.reply_text(
"Usage: /hunt [Stadt]\n"
"Beispiel: /hunt Berlin\n\n"
f"Scannt {len(HUNT_BRANCHES)} Branchen nacheinander."
)
return
if hunt_in_progress:
await update.message.reply_text("Hunt läuft bereits. Bitte warten.")
return
stadt = " ".join(args)
hunt_in_progress = True
hunt_progress_data = {"completed": 0, "total": len(HUNT_BRANCHES), "leads": 0}
hunt_chat_id = update.effective_chat.id
hunt_app = context.application
await update.message.reply_text(
f"🔪 HUNT MODE gestartet für {stadt}\n"
f"📍 Radius: {SEARCH_RADIUS_KM}km\n"
f"Scanne {len(HUNT_BRANCHES)} Branchen...\n"
f"Update nach jeder Branche."
)
async def progress_callback(completed, total, leads, branche, branch_leads, branch_skipped):
global hunt_progress_data
hunt_progress_data["completed"] = completed
hunt_progress_data["total"] = total
hunt_progress_data["leads"] = leads
if hunt_chat_id and hunt_app:
try:
await hunt_app.bot.send_message(
chat_id=hunt_chat_id,
text=(
f"✅ Branche {completed}/{total}: {branche}\n"
f" Leads: {branch_leads} Problemfälle\n"
f" Aussortiert: {branch_skipped} (gute Website)\n"
f" Gesamt: {leads} Leads"
),
)
except Exception as e:
logger.error("Hunt-Progress-Update fehlgeschlagen: %s", e)
async def lead_callback(lead):
"""SOFORTIGE Telegram-Nachricht für jeden einzelnen Lead im Hunt-Mode."""
if hunt_chat_id and hunt_app:
try:
reasons_str = lead.get("reason_string", "")
email_display = lead.get("email", "") or lead.get("contact_email", "") or "N/A"
await hunt_app.bot.send_message(
chat_id=hunt_chat_id,
text=(
f"🚀 *Neuer Lead im Hunt!*\n\n"
f"🏢 *Firma:* {lead.get('name', 'Unbekannt')}\n"
f"🌐 *Web:* {lead.get('website', 'N/A')}\n"
f"✉️ *Email:* {email_display}\n"
f"📞 *Tel:* {lead.get('contact_phone', lead.get('phone', 'N/A'))}\n"
f"👤 *Kontakt:* {lead.get('contact_name', 'N/A')}\n"
f"⭐ *Bewertung:* {lead.get('google_rating', 0)}\n"
f"⚠️ *Probleme:* {reasons_str}\n"
f"📍 *Branche:* {lead.get('branche', 'N/A')} | {lead.get('stadt', 'N/A')}"
),
parse_mode="Markdown",
)
except Exception as e:
logger.error("Hunt-Lead-Benachrichtigung fehlgeschlagen: %s", e)
try:
result = await run_hunt_async(
stadt,
progress_callback=progress_callback,
lead_callback=lead_callback,
radius_km=SEARCH_RADIUS_KM,
)
await update.message.reply_text(
f"🔪 HUNT ABGESCHLOSSEN\n\n"
f"Branchen gescannt: {result['branches_completed']}/{result['branches_total']}\n"
f"Problemfälle: {result['leads_exported']}\n"
f"Aussortiert: {result['leads_skipped']} (gute Website)\n"
f"Alle Leads in Notion exportiert."
)
except Exception as e:
logger.error("Hunt fehlgeschlagen: %s", e)
await update.message.reply_text(f"Hunt fehlgeschlagen: {e}")
finally:
hunt_in_progress = False
hunt_chat_id = None
hunt_app = None
async def cmd_stats(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not is_authorized(update):
await update.message.reply_text("Zugriff verweigert.")
return
stats = get_stats()
hunt_status = " (läuft)" if hunt_in_progress else ""
await update.message.reply_text(
f"📊 *Lead-Statistiken*\n"
f"Heute: {stats['today']}\n"
f"Gesamt: {stats['total']}\n"
f"Hunt: {hunt_progress_data['completed']}/{hunt_progress_data['total']}{hunt_status}",
parse_mode="Markdown",
)
async def cmd_best(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not is_authorized(update):
await update.message.reply_text("Zugriff verweigert.")
return
best = get_best_lead()
if not best:
await update.message.reply_text("Keine Leads in der Datenbank.")
return
reasons = best.get("reason_string", best.get("reasons", []))
if isinstance(reasons, list):
reasons_str = ", ".join(reasons)
else:
reasons_str = reasons
await update.message.reply_text(
f"🎯 *Lead mit höchstem Potenzial*\n\n"
f"🏢 {best.get('name', 'Unbekannt')}\n"
f"🌐 {best.get('website', 'N/A')}\n"
f"✉️ {best.get('email', 'N/A')}\n"
f"📞 {best.get('contact_phone', best.get('phone', 'N/A'))}\n"
f"👤 {best.get('contact_name', 'N/A')}\n"
f"⭐ {best.get('google_rating', 0)}\n"
f"⚠️ Score: {best.get('design_score', 0)}\n"
f"📍 {best.get('stadt', 'N/A')} | {best.get('branche', 'N/A')}\n\n"
f"Gründe: {reasons_str}",
parse_mode="Markdown",
)
async def cmd_radius(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not is_authorized(update):
await update.message.reply_text("Zugriff verweigert.")
return
args = context.args
if not args:
await update.message.reply_text(
f"Aktueller Radius: {SEARCH_RADIUS_KM}km\n\n"
"Usage: /radius {km}\n"
"Beispiel: /radius 25"
)
return
try:
new_radius = int(args[0])
if new_radius < 1 or new_radius > 100:
await update.message.reply_text(
"Radius muss zwischen 1 und 100 km liegen."
)
return
_save_radius(new_radius)
# Scraper-Modul synchronisieren
import scraper
scraper.SEARCH_RADIUS_KM = new_radius
await update.message.reply_text(
f"✅ Radius auf {new_radius}km gesetzt.\n"
f"Nächste Suche nutzt diesen Umkreis."
)
except ValueError:
await update.message.reply_text(
"Ungültiger Wert. Bitte eine Zahl eingeben.\n"
"Beispiel: /radius 25"
)
async def cmd_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not is_authorized(update):
await update.message.reply_text("Zugriff verweigert.")
return
await update.message.reply_text(
"Willkommen beim LeadBot!\n\n"
"Commands:\n"
"/find [Branche] [Stadt] — Google Maps + Verzeichnisse\n"
"/hunt [Stadt] — Scannt 20 Branchen automatisch\n"
"/radius {km} — Umkreis ändern (Standard: 10km)\n"
"/stats — Statistiken anzeigen\n"
"/best — Lead mit höchstem Potenzial\n\n"
"Du kannst auch natürlich fragen, z.B.:\n"
'"Wie viele Leads heute?"\n'
'"Hunt Berlin"\n'
'"Suche Elektriker in München"'
)
async def handle_natural_language(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not is_authorized(update):
return
text = update.message.text.lower().strip()
stats_patterns = [
r"wie viele.*heute", r"leads.*heute", r"heute.*leads",
r"statistik", r"stats", r"wieviele.*heute",
]
for pattern in stats_patterns:
if re.search(pattern, text):
stats = get_stats()
await update.message.reply_text(
f"Heute: {stats['today']} Leads\nGesamt: {stats['total']} Leads"
)
return
best_patterns = [
r"bester.*lead", r"lead.*meisten.*fehler",
r"highest.*potential", r"top.*lead", r"beste.*akquise",
]
for pattern in best_patterns:
if re.search(pattern, text):
best = get_best_lead()
if not best:
await update.message.reply_text("Keine Leads in der Datenbank.")
return
reasons = best.get("reason_string", best.get("reasons", []))
if isinstance(reasons, list):
reasons_str = ", ".join(reasons)
else:
reasons_str = reasons
await update.message.reply_text(
f"Bester Lead: {best.get('name')}\n"
f"Score: {best.get('design_score')} Fehler\n"
f"Website: {best.get('website', 'N/A')}\n"
f"Gründe: {reasons_str}"
)
return
hunt_patterns = [r"hunt\s+([a-zäöüß\s]+)", r"jage\s+([a-zäöüß\s]+)", r"scan\s+([a-zäöüß\s]+)"]
for pattern in hunt_patterns:
match = re.search(pattern, text)
if match:
stadt = match.group(1).strip()
context.args = stadt.split()
await cmd_hunt(update, context)
return
search_patterns = [
r"suche?\s+([a-zäöüß]+)\s+in\s+([a-zäöüß\s]+)",
r"find\s+([a-zäöüß]+)\s+([a-zäöüß\s]+)",
]
for pattern in search_patterns:
match = re.search(pattern, text)
if match:
branche = match.group(1)
stadt = match.group(2).strip()
context.args = [branche] + stadt.split()
await cmd_find(update, context)
return
# ─── PTB v20+ Lifecycle Callbacks ────────────────────────────────────────────
async def on_bot_start(application: Application):
"""
Wird von application.run_polling() aufgerufen NACHDEM der Event-Loop
gestartet wurde aber BEVOR das Polling beginnt.
Hier initialisieren wir den Browser — sicher im laufenden Loop.
"""
global hunt_app
hunt_app = application
async def on_captcha(message: str):
if hunt_chat_id and hunt_app:
try:
await hunt_app.bot.send_message(
chat_id=hunt_chat_id,
text=f"{message}",
)
except Exception as e:
logger.error("Captcha-Benachrichtigung fehlgeschlagen: %s", e)
await init_scraper_async(captcha_cb=on_captcha, headless=_HEADLESS_MODE)
init_bot()
logger.info("Browser initialisiert (headless=%s) — Bot bereit", _HEADLESS_MODE)
async def on_bot_stop(application: Application):
"""
Wird von application.run_polling() aufgerufen BEVOR der Event-Loop
geschlossen wird. Garantiert sauberer Shutdown.
"""
await shutdown_scraper_async()
logger.info("LeadBot heruntergefahren")
# ─── PID-Lock ────────────────────────────────────────────────────────────────
PID_FILE = Path(__file__).parent / ".bot.pid"
def acquire_pid_lock() -> bool:
"""
Verhindert Doppelstart durch PID-Datei.
Returns:
True wenn Lock erfolgreich, False wenn bereits ein Bot läuft.
"""
if PID_FILE.exists():
try:
old_pid = int(PID_FILE.read_text().strip())
os.kill(old_pid, 0)
logger.error(
"Bot läuft bereits (PID %s). "
"PID-Datei löschen oder laufenden Prozess beenden.",
old_pid,
)
return False
except (ProcessLookupError, ValueError):
logger.info("Stale PID-Datei gefunden (Prozess %s nicht aktiv) — überschreibe", old_pid)
PID_FILE.write_text(str(os.getpid()))
logger.info("PID-Lock gesetzt: %s", os.getpid())
return True
def release_pid_lock():
"""Entfernt die PID-Datei beim sauberen Beenden."""
try:
if PID_FILE.exists():
PID_FILE.unlink()
logger.info("PID-Lock entfernt")
except Exception as e:
logger.debug("PID-Lock entfernen fehlgeschlagen: %s", e)
# ─── Entry Point ─────────────────────────────────────────────────────────────
def build_application() -> Application:
"""
Baut die Application mit allen Handlern.
Keine async-Operationen hier — nur Konfiguration.
Browser-Init und Shutdown erfolgen über post_init/post_shutdown.
"""
token = os.getenv("TELEGRAM_BOT_TOKEN")
if not token:
raise ValueError("TELEGRAM_BOT_TOKEN nicht gesetzt")
application = (
Application.builder()
.token(token)
.post_init(on_bot_start)
.post_shutdown(on_bot_stop)
.build()
)
application.add_handler(CommandHandler("start", cmd_start))
application.add_handler(CommandHandler("find", cmd_find))
application.add_handler(CommandHandler("hunt", cmd_hunt))
application.add_handler(CommandHandler("radius", cmd_radius))
application.add_handler(CommandHandler("stats", cmd_stats))
application.add_handler(CommandHandler("best", cmd_best))
application.add_handler(
MessageHandler(filters.TEXT & ~filters.COMMAND, handle_natural_language)
)
logger.info("Telegram-Bot-Handler registriert")
return application
def main():
"""
Haupteinstiegspunkt mit PID-Lock, Signal-Handling, argparse und Conflict-Prävention.
"""
global _HEADLESS_MODE
parser = argparse.ArgumentParser(description="LeadBot — Telegram-gesteuerte Lead-Generierung")
parser.add_argument(
"--background", action="store_true",
help="Startet Playwright im Headless-Modus (kein sichtbares Browser-Fenster)",
)
args = parser.parse_args()
_HEADLESS_MODE = args.background
if _HEADLESS_MODE:
logger.info("Background-Mode aktiviert — Browser startet headless")
if not acquire_pid_lock():
sys.exit(1)
application = build_application()
def handle_signal(signum, frame):
"""
Sauberer Shutdown bei SIGINT/SIGTERM.
Ruft application.stop() und application.shutdown() auf.
"""
sig_name = signal.Signals(signum).name
logger.info("Signal %s empfangen — fahre Bot herunter...", sig_name)
try:
application.stop()
except Exception as e:
logger.debug("application.stop() Exception: %s", e)
try:
application.shutdown()
except Exception as e:
logger.debug("application.shutdown() Exception: %s", e)
release_pid_lock()
logger.info("Bot heruntergefahren")
sys.exit(0)
signal.signal(signal.SIGINT, handle_signal)
signal.signal(signal.SIGTERM, handle_signal)
logger.info("Starte Telegram-Bot...")
try:
application.run_polling(drop_pending_updates=True)
except Exception as e:
logger.error("Bot abgestürzt: %s", e)
finally:
release_pid_lock()
if __name__ == "__main__":
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s — %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
main()