DEV Community

Sherif sani
Sherif sani

Posted on

Building a Weather reports Dashboard with python, S3 and Openweather API

Introduction

Have you ever wanted to build an application that fetches real-time weather data and securely stores it for later use? In this article, I'll walk you through how I developed a weather reporting app using Python, the OpenWeather API, and AWS S3.

This app:

  • Fetches current weather information for selected cities.
  • Displays the weather data on the console.
  • Stores the data in JSON format in an S3 bucket for archival purposes.

This is my first project in the 30-days DevOps challenge series.


Setting Up the Project

Requirements

  1. Python 3.x
  2. AWS CLI configured with credentials and permissions
   aws configure
   aws s3 mb s3://your_s3_bucket_name
Enter fullscreen mode Exit fullscreen mode
  1. OpenWeather API key
  2. A .env file to store environment variables:
   OPENWEATHER_API_KEY=your_openweather_api_key  
   AWS_BUCKET_NAME=your_s3_bucket_name  
Enter fullscreen mode Exit fullscreen mode

Installing Required Libraries

Install the necessary libraries:

pip install python-dotenv requests boto3
Enter fullscreen mode Exit fullscreen mode

Alternatively, use a requirements.txt file:

touch requirements.txt  
echo "python-dotenv" >> requirements.txt  
echo "requests" >> requirements.txt  
echo "boto3" >> requirements.txt  
pip install -r requirements.txt  
Enter fullscreen mode Exit fullscreen mode

The App

1. Importing Packages

import os  
import json  
import boto3  
import requests  
from datetime import datetime  
from dotenv import load_dotenv  
Enter fullscreen mode Exit fullscreen mode

2. Environment Setup

Initialize the app to interact with the API and AWS S3:

load_dotenv()  

class WeatherDashboard:  
    def __init__(self):  
        self.api_key = os.getenv('OPENWEATHER_API_KEY')  
        self.bucket_name = os.getenv('AWS_BUCKET_NAME')  
        self.s3_client = boto3.client('s3')  

    def create_bucket_if_not_exists(self):  
        try:  
            self.s3_client.head_bucket(Bucket=self.bucket_name)  
            print(f"Bucket {self.bucket_name} exists")  
        except:  
            print(f"Creating bucket {self.bucket_name}")  
            try:  
                self.s3_client.create_bucket(Bucket=self.bucket_name)  
                print(f"Successfully created bucket {self.bucket_name}")  
            except Exception as e:  
                print(f"Error creating bucket: {e}")  
Enter fullscreen mode Exit fullscreen mode

3. Fetching Weather Data

def fetch_weather(self, city):  
    base_url = "http://api.openweathermap.org/data/2.5/weather"  
    params = {  
        "q": city,  
        "appid": self.api_key,  
        "units": "imperial"  
    }  

    try:  
        response = requests.get(base_url, params=params)  
        response.raise_for_status()  
        return response.json()  
    except requests.exceptions.RequestException as e:  
        print(f"Error fetching weather data: {e}")  
        return None  
Enter fullscreen mode Exit fullscreen mode

4. Saving Data to S3

def save_to_s3(self, weather_data, city):  
    if not weather_data:  
        return False  

    timestamp = datetime.now().strftime('%Y%m%d-%H%M%S')  
    file_name = f"weather-data/{city}-{timestamp}.json"  

    try:  
        weather_data['timestamp'] = timestamp  
        self.s3_client.put_object(  
            Bucket=self.bucket_name,  
            Key=file_name,  
            Body=json.dumps(weather_data),  
            ContentType='application/json'  
        )  
        print(f"Successfully saved data for {city} to S3")  
        return True  
    except Exception as e:  
        print(f"Error saving to S3: {e}")  
        return False  
Enter fullscreen mode Exit fullscreen mode

5. Main Function

def main():  
    dashboard = WeatherDashboard()  

    # Create bucket if needed  
    dashboard.create_bucket_if_not_exists()  

    cities = ["Philadelphia", "Seattle", "New York"]  

    for city in cities:  
        print(f"\nFetching weather for {city}...")  
        weather_data = dashboard.fetch_weather(city)  
        if weather_data:  
            temp = weather_data['main']['temp']  
            feels_like = weather_data['main']['feels_like']  
            humidity = weather_data['main']['humidity']  
            description = weather_data['weather'][0]['description']  

            print(f"Temperature: {temp}°F")  
            print(f"Feels like: {feels_like}°F")  
            print(f"Humidity: {humidity}%")  
            print(f"Conditions: {description}")  

            # Save to S3  
            success = dashboard.save_to_s3(weather_data, city)  
            if success:  
                print(f"Weather data for {city} saved to S3!")  
        else:  
            print(f"Failed to fetch weather data for {city}")  

if __name__ == "__main__":  
    main()  
Enter fullscreen mode Exit fullscreen mode

Check your s3 bucket

aws s3 ls s3://your_s3_bucket_name 
Enter fullscreen mode Exit fullscreen mode

alternatively, you could use the console to the see the content of your bucket


Conclusion

Building a weather reporting app that integrates with AWS S3 demonstrates the power of combining APIs, cloud storage, and Python automation. This project retrieves live weather data using the OpenWeather API, processes it, and securely stores it in S3 for future use.

Further Enhancements:

  • Add a web interface.
  • Integrate data visualization tools.
  • Set up scheduled updates with AWS Lambda.

This project is a great way to sharpen your Python skills while exploring cloud technologies. Happy coding!

GitHub Repository

Top comments (0)