import os import requests from datetime import datetime, timedelta, UTC from zoneinfo import ZoneInfo from pymongo import MongoClient from dotenv import load_dotenv from pymongo.errors import PyMongoError from apscheduler.schedulers.blocking import BlockingScheduler load_dotenv() # Configuration ENDPOINT_URL = os.getenv("ENDPOINT_URL") MONGO_URI = os.getenv("MONGO_URI") DB_NAME = os.getenv("DB_NAME") COLLECTION_NAME = os.getenv("COLLECTION_NAME") TIMEZONE = ZoneInfo("Europe/Rome") def get_time_range(): """ Calculate the time range for the current execution. Returns a tuple of (min_timestamp, max_timestamp) in ISO 8601 format with Europe/Rome timezone. """ now = datetime.now(TIMEZONE) one_minute_ago = now - timedelta(minutes=1) # Format with colon in timezone offset (e.g., +01:00 instead of +0100) def format_with_colon(dt): base = dt.strftime("%Y-%m-%dT%H:%M:%S") # Get timezone offset and format with colon offset = dt.strftime("%z") if offset: sign = offset[0] hours = offset[1:3] minutes = offset[3:5] return f"{base}{sign}{hours}:{minutes}" return base min_timestamp = format_with_colon(one_minute_ago) max_timestamp = format_with_colon(now) return min_timestamp, max_timestamp def fetch_plexts(min_timestamp, max_timestamp): """ Fetch plexts from the endpoint. Args: min_timestamp: Start timestamp in ISO 8601 format max_timestamp: End timestamp in ISO 8601 format Returns: List of plexts or None if error occurs """ try: params = {"min_timestamp": min_timestamp, "max_timestamp": max_timestamp} response = requests.get(ENDPOINT_URL, params=params, timeout=30) response.raise_for_status() data = response.json() return data.get("plexts", []) except requests.RequestException as e: print(f"Error fetching plexts: {e}") return None def save_to_mongodb(plexts): """ Save plexts to MongoDB. Args: plexts: List of plext dictionaries to save Returns: True if successful, False otherwise """ if not plexts: print("No plexts to save") return True try: client = MongoClient(MONGO_URI) db = client[DB_NAME] collection = db[COLLECTION_NAME] # Insert all plexts result = collection.insert_many(plexts) print(f"Inserted {len(result.inserted_ids)} plexts to MongoDB") return True except PyMongoError as e: print(f"Error saving to MongoDB: {e}") return False finally: client.close() def job(): """ Main job function that runs every minute. """ print( f"\n[{datetime.now(TIMEZONE).strftime('%Y-%m-%d %H:%M:%S %Z')}] Starting job..." ) # Get time range min_timestamp, max_timestamp = get_time_range() print(f"Time range: {min_timestamp} to {max_timestamp}") # Fetch plexts plexts = fetch_plexts(min_timestamp, max_timestamp) if plexts is None: print("Failed to fetch plexts") return print(f"Fetched {len(plexts)} plexts") # Save to MongoDB if save_to_mongodb(plexts): print("Job completed successfully") else: print("Job completed with errors") def main(): """ Main function to schedule and run the job. """ print("Starting Plex monitoring scheduler...") print("Press Ctrl+C to stop") # Create scheduler scheduler = BlockingScheduler() # Schedule job to run every minute scheduler.add_job(job, "interval", minutes=1) # Run the job immediately on startup job() # Start the scheduler try: scheduler.start() except (KeyboardInterrupt, SystemExit): print("\nScheduler stopped by user") if __name__ == "__main__": main()