File size: 1,928 Bytes
8e8c527
 
 
 
 
c7ed923
 
 
8e8c527
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
app.py (verbessert)

import asyncio import logging from scheduler import start_scheduler from config import setup_logging

Logging-Konfiguration

setup_logging()

if name == "main": try: start_scheduler() loop = asyncio.get_event_loop() loop.run_forever() except KeyboardInterrupt: logging.info("Bot wurde beendet") except Exception as e: logging.error(f"Kritischer Fehler: {str(e)}")

config.py (verbessert)

import json import os import logging from pathlib import Path

Pfade

BASE_DIR = Path(file).parent CONFIG_DIR = BASE_DIR / "config" FEEDS_FILE = CONFIG_DIR / "feeds.json" API_KEYS_FILE = CONFIG_DIR / "api_keys.json" BLACKLIST_FILE = CONFIG_DIR / "blacklist.txt" WHITELIST_FILE = CONFIG_DIR / "whitelist.txt"

Logging-Konfiguration

def setup_logging(): logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", handlers=[ logging.FileHandler("news_bot.log"), logging.StreamHandler() ] )

Konfigurationsladefunktionen

def load_json(file_path): try: with open(file_path, "r", encoding="utf-8") as f: return json.load(f) except FileNotFoundError: logging.error(f"Datei nicht gefunden: {file_path}") return {}

def load_text_list(file_path): try: with open(file_path, "r", encoding="utf-8") as f: return [line.strip().lower() for line in f if line.strip()] except FileNotFoundError: logging.warning(f"Datei nicht gefunden: {file_path}") return []

Laden der Konfiguration

FEEDS = load_json(FEEDS_FILE) API_KEYS = load_json(API_KEYS_FILE) BLACKLIST = load_text_list(BLACKLIST_FILE) WHITELIST = load_text_list(WHITELIST_FILE)

SETTINGS = { "check_interval": 5, "max_retries": 3, "request_timeout": 10, "max_articles": 50, "telegram_chat_id": API_KEYS.get("telegram_chat_id", ""), "telegram_bot_token": API_KEYS.get("telegram_bot_token", "") }

CATEGORY_FILTERS = { "technology": ["AI", "Machine Learning", "Cybersecurity"], "politics": ["Election", "Government", "Policy"] }