Advertisement
Tylon

Tibber Oushover

Oct 5th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 5.64 KB | None | 0 0
  1. import json
  2. from datetime import datetime, timedelta
  3.  
  4. def lambda_handler(event, context):
  5.     print("Received event:", event)
  6.     event_data = json.loads(event['body'])
  7.     viewer_data = event_data['data']['viewer']['homes'][0]['currentSubscription']['priceInfo']
  8.  
  9.     def find_low_price_windows(prices):
  10.         if not prices:
  11.             return []
  12.    
  13.         average_price = sum([p['total'] for p in prices]) / len(prices)
  14.         low_price_windows = []
  15.         current_window = None
  16.    
  17.         for i, price_info in enumerate(prices):
  18.             start_time = datetime.fromisoformat(price_info['startsAt'])
  19.             if price_info['total'] < average_price:
  20.                 if not current_window:
  21.                     current_window = [start_time, None]
  22.                 current_window[1] = start_time + timedelta(hours=1)
  23.             else:
  24.                 if current_window:
  25.                     if current_window[0].hour == 23:  # Spezialfall für 23 Uhr
  26.                         current_window[1] = current_window[0].replace(hour=23, minute=59, second=59)
  27.                     low_price_windows.append(current_window)
  28.                     current_window = None
  29.         if current_window:
  30.             low_price_windows.append(current_window)
  31.    
  32.         return low_price_windows
  33.  
  34.     def find_special_price_windows(prices):
  35.         if not prices:
  36.             return [], []
  37.    
  38.         average_price = sum([p['total'] for p in prices]) / len(prices)
  39.         min_price = min([p['total'] for p in prices])
  40.         max_price = max([p['total'] for p in prices])
  41.    
  42.         low_threshold = (average_price + min_price) / 2
  43.         high_threshold = (average_price + max_price) / 2
  44.    
  45.         very_low_windows = []
  46.         very_high_windows = []
  47.         current_low_window = None
  48.         current_high_window = None
  49.    
  50.         for price_info in prices:
  51.             start_time = datetime.fromisoformat(price_info['startsAt'])
  52.             end_time = start_time + timedelta(hours=1)
  53.    
  54.             if price_info['total'] < low_threshold:
  55.                 if current_low_window and current_low_window[1] == start_time:
  56.                     # Erweitere das aktuelle niedrige Preisfenster
  57.                     current_low_window[1] = end_time
  58.                 else:
  59.                     # Beginne ein neues niedriges Preisfenster
  60.                     if current_low_window:
  61.                         very_low_windows.append(current_low_window)
  62.                     current_low_window = [start_time, end_time]
  63.             elif current_low_window:
  64.                 very_low_windows.append(current_low_window)
  65.                 current_low_window = None
  66.    
  67.             if price_info['total'] > high_threshold:
  68.                 if current_high_window and current_high_window[1] == start_time:
  69.                     # Erweitere das aktuelle hohe Preisfenster
  70.                     current_high_window[1] = end_time
  71.                 else:
  72.                     # Beginne ein neues hohes Preisfenster
  73.                     if current_high_window:
  74.                         very_high_windows.append(current_high_window)
  75.                     current_high_window = [start_time, end_time]
  76.             elif current_high_window:
  77.                 very_high_windows.append(current_high_window)
  78.                 current_high_window = None
  79.    
  80.         # Füge das letzte Zeitfenster hinzu, falls es nicht abgeschlossen wurde
  81.         if current_low_window:
  82.             very_low_windows.append(current_low_window)
  83.         if current_high_window:
  84.             very_high_windows.append(current_high_window)
  85.    
  86.         return very_low_windows, very_high_windows
  87.  
  88.  
  89.     def format_window(window):
  90.         start, end = window
  91.         return f"am {start.strftime('%d.%m.')} zwischen {start.strftime('%H Uhr')} und {end.strftime('%H Uhr')}"
  92.  
  93.     # Heutige Preise verarbeiten
  94.     today_prices = viewer_data['today']
  95.     today_low_price_windows = find_low_price_windows(today_prices)
  96.     today_very_low, today_very_high = find_special_price_windows(today_prices)
  97.    
  98.     formatted_today_windows = [format_window(window) for window in today_low_price_windows]
  99.     formatted_today_very_low = [format_window(window) for window in today_very_low]
  100.     formatted_today_very_high = [format_window(window) for window in today_very_high]
  101.  
  102.     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)
  103.  
  104.     # Morgen Preise verarbeiten, falls vorhanden
  105.     tomorrow_response = ""
  106.     if 'tomorrow' in viewer_data and viewer_data['tomorrow']:
  107.         tomorrow_prices = viewer_data['tomorrow']
  108.         tomorrow_low_price_windows = find_low_price_windows(tomorrow_prices)
  109.         tomorrow_very_low, tomorrow_very_high = find_special_price_windows(tomorrow_prices)
  110.        
  111.         formatted_tomorrow_windows = [format_window(window) for window in tomorrow_low_price_windows]
  112.         formatted_tomorrow_very_low = [format_window(window) for window in tomorrow_very_low]
  113.         formatted_tomorrow_very_high = [format_window(window) for window in tomorrow_very_high]
  114.  
  115.         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)
  116.  
  117.     # Gesamtantwort zusammenstellen
  118.     response = today_response
  119.     if tomorrow_response:
  120.         response += "\n\n" + tomorrow_response
  121.  
  122.     return {
  123.         'statusCode': 200,
  124.         'body': json.dumps(response)
  125.     }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement