humbleParser/bundle_checker.py

69 lines
2.5 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# bundle_checker.py
import logging
import os
from datetime import datetime
from bundle_parser import BundleParser
from models import Base, Bundle, BundleVersion, BundleSalesHistory, BundleItem
# === Logging-Konfiguration ===
# Eigener Formatter, der lange Nachrichten für den Konsolen-Output kürzt
class TruncateFormatter(logging.Formatter):
def __init__(self, fmt=None, datefmt=None, max_length=300):
super().__init__(fmt, datefmt)
self.max_length = max_length
def format(self, record):
message = super().format(record)
if len(message) > self.max_length:
return message[:self.max_length] + " ... [truncated]"
return message
# Erstelle einen Logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Datei-Handler: Schreibt alle Logeinträge in eine Datei mit Zeitstempel im Dateinamen
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
log_filename = f"bundle_checker_{timestamp}.log"
file_handler = logging.FileHandler(log_filename)
file_handler.setLevel(logging.DEBUG)
file_formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s", "%Y-%m-%d %H:%M:%S")
file_handler.setFormatter(file_formatter)
# Konsolen-Handler: Gibt verkürzte Nachrichten aus
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_formatter = TruncateFormatter("%(asctime)s [%(levelname)s] %(message)s", "%Y-%m-%d %H:%M:%S", max_length=300)
console_handler.setFormatter(console_formatter)
# Füge beide Handler zum Logger hinzu
logger.addHandler(file_handler)
logger.addHandler(console_handler)
# === Ende Logging-Konfiguration ===
def main():
overview_url = "https://www.humblebundle.com/bundles"
# Wähle hier die gewünschte Kategorie, z.B. "books", "games" oder "software"
category = "books"
logger.info("Extrahiere Bundle-URLs von der Übersichtsseite ...")
overview_parser = BundleParser(overview_url, category=category)
bundle_urls = overview_parser.get_bundle_urls()
logger.info(f"Gefundene {len(bundle_urls)} Bundle-URLs für Kategorie '{category}'.")
for url in bundle_urls:
logger.info(f"Verarbeite Bundle: {url}")
parser = BundleParser(url)
try:
bundle_data = parser.get_relevant_bundle_data()
logger.info(f"Detaildaten: {bundle_data}")
items = parser.parse_items()
logger.info(f"Extrahierte Items: {items}")
except Exception as e:
logger.error(f"Fehler bei {url}: {e}")
if __name__ == "__main__":
main()