Get daily weather forecasts

I enjoy my procrastination days, typically falling on Mondays and Fridays. However, with Memorial Day off, this week’s laziness extends to Tuesday as well. These are the days when things actually get done: cleaning the house, doing the dishes, and scripting various projects to pass the time. It’s amazing how productive one can be when avoiding paycheck-based work.

The weather section on my kid’s bedtime points page needed fixing, and I’m always looking for ways to move away from commercial products with more independent solutions.

The key below is not mine, but from their own page. Feel free to use it. ¯\(ツ)

Weather

weather.py

    import requests
    import json
    import re
    from slack_lib import send_message_to_slack

    def get_weather_narratives(url):
        response = requests.get(url)
        if response.status_code == 200:
            data = response.json()
            narratives = data.get('narrative', [])
            return narratives[0:3] if len(narratives) > 3 else []
        else:
            print(f"Failed to fetch data. HTTP Status code: {response.status_code}")
            return []

    def add_weather_emojis(narrative):
        emoji_map = {
            "sunshine": "☀️",
            "sunny": "☀️",
            "sun": "☀️",
            "rain": "🌧️",
            "clouds": "☁️",
            "cloud": "☁️",
            "snow": "❄️",
            "storm": "⛈️",
            "fog": "🌁",
            "wind": "💨"
        }
        for word, emoji in emoji_map.items():
            narrative = re.sub(word, emoji, narrative, flags=re.IGNORECASE)
        return narrative

    url = "https://api.weather.com/v3/wx/forecast/daily/7day?apiKey=e1f10a1e78da46f5b10a1e78da96f525&geocode=37.223%2C-121.984&language=en-US&units=e&format=json"
    narratives = get_weather_narratives(url)
    if narratives:
        combined_narrative = ""
        for i, narrative in enumerate(narratives, start=1):
            narrative_with_emojis = add_weather_emojis(narrative)
            combined_narrative += f"> Day {i}: {narrative_with_emojis}\n"
        send_message_to_slack(combined_narrative, username="🌡️ Weather Bot ☁️")
Previous: Runnin Next: Set all Amazon searches to Prime automatically