Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from datetime import datetime, timedelta
- def lambda_handler(event, context):
- print("Received event:", event)
- event_data = json.loads(event['body'])
- viewer_data = event_data['data']['viewer']['homes'][0]['currentSubscription']['priceInfo']
- def find_low_price_windows(prices):
- if not prices:
- return []
- average_price = sum([p['total'] for p in prices]) / len(prices)
- low_price_windows = []
- current_window = None
- for i, price_info in enumerate(prices):
- start_time = datetime.fromisoformat(price_info['startsAt'])
- if price_info['total'] < average_price:
- if not current_window:
- current_window = [start_time, None]
- current_window[1] = start_time + timedelta(hours=1)
- else:
- if current_window:
- if current_window[0].hour == 23: # Spezialfall für 23 Uhr
- current_window[1] = current_window[0].replace(hour=23, minute=59, second=59)
- low_price_windows.append(current_window)
- current_window = None
- if current_window:
- low_price_windows.append(current_window)
- return low_price_windows
- def find_special_price_windows(prices):
- if not prices:
- return [], []
- average_price = sum([p['total'] for p in prices]) / len(prices)
- min_price = min([p['total'] for p in prices])
- max_price = max([p['total'] for p in prices])
- low_threshold = (average_price + min_price) / 2
- high_threshold = (average_price + max_price) / 2
- very_low_windows = []
- very_high_windows = []
- current_low_window = None
- current_high_window = None
- for price_info in prices:
- start_time = datetime.fromisoformat(price_info['startsAt'])
- end_time = start_time + timedelta(hours=1)
- if price_info['total'] < low_threshold:
- if current_low_window and current_low_window[1] == start_time:
- # Erweitere das aktuelle niedrige Preisfenster
- current_low_window[1] = end_time
- else:
- # Beginne ein neues niedriges Preisfenster
- if current_low_window:
- very_low_windows.append(current_low_window)
- current_low_window = [start_time, end_time]
- elif current_low_window:
- very_low_windows.append(current_low_window)
- current_low_window = None
- if price_info['total'] > high_threshold:
- if current_high_window and current_high_window[1] == start_time:
- # Erweitere das aktuelle hohe Preisfenster
- current_high_window[1] = end_time
- else:
- # Beginne ein neues hohes Preisfenster
- if current_high_window:
- very_high_windows.append(current_high_window)
- current_high_window = [start_time, end_time]
- elif current_high_window:
- very_high_windows.append(current_high_window)
- current_high_window = None
- # Füge das letzte Zeitfenster hinzu, falls es nicht abgeschlossen wurde
- if current_low_window:
- very_low_windows.append(current_low_window)
- if current_high_window:
- very_high_windows.append(current_high_window)
- return very_low_windows, very_high_windows
- def format_window(window):
- start, end = window
- return f"am {start.strftime('%d.%m.')} zwischen {start.strftime('%H Uhr')} und {end.strftime('%H Uhr')}"
- # Heutige Preise verarbeiten
- today_prices = viewer_data['today']
- today_low_price_windows = find_low_price_windows(today_prices)
- today_very_low, today_very_high = find_special_price_windows(today_prices)
- formatted_today_windows = [format_window(window) for window in today_low_price_windows]
- formatted_today_very_low = [format_window(window) for window in today_very_low]
- formatted_today_very_high = [format_window(window) for window in today_very_high]
- today_response = "Heute Niedrigpreisfenster:\n" + "\n".join(formatted_today_windows) + "\n\nHeute besonders niedrig:\n" + "\n".join(formatted_today_very_low) + "\n\nHeute besonders teuer:\n" + "\n".join(formatted_today_very_high)
- # Morgen Preise verarbeiten, falls vorhanden
- tomorrow_response = ""
- if 'tomorrow' in viewer_data and viewer_data['tomorrow']:
- tomorrow_prices = viewer_data['tomorrow']
- tomorrow_low_price_windows = find_low_price_windows(tomorrow_prices)
- tomorrow_very_low, tomorrow_very_high = find_special_price_windows(tomorrow_prices)
- formatted_tomorrow_windows = [format_window(window) for window in tomorrow_low_price_windows]
- formatted_tomorrow_very_low = [format_window(window) for window in tomorrow_very_low]
- formatted_tomorrow_very_high = [format_window(window) for window in tomorrow_very_high]
- tomorrow_response = "Morgen Niedrigpreisfenster:\n" + "\n".join(formatted_tomorrow_windows) + "\n\nMorgen besonders niedrig:\n" + "\n".join(formatted_tomorrow_very_low) + "\n\nMorgen besonders teuer:\n" + "\n".join(formatted_tomorrow_very_high)
- # Gesamtantwort zusammenstellen
- response = today_response
- if tomorrow_response:
- response += "\n\n" + tomorrow_response
- return {
- 'statusCode': 200,
- 'body': json.dumps(response)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement