top of page

Your Day 3 Code: Weather + Stock + Email




STOCK ALERT
STOCK ALERT

Welcome to Day 3!

Hey, beginner! By now, you’ve fetched weather data (Day 1) and automated a text message (Day 2). Today, we’re going bigger: combining multiple APIs to automate a task that could impress your boss—or make them wonder why they’re paying you at all! We’ll keep it fun, practical, and beginner-friendly with a real-life example and code you can try. Ready to let robots steal the show?

Why Multiple APIs?

One API is cool, but combining them is where the real power kicks in. Think of it like a superhero team-up: each API brings its own strength, and together they solve bigger problems. Manual work? Dead. Your time? Freed up. Today, we’ll prove it.

Real-Life Example: Automating Coffee Shop Alerts

Imagine you work at a coffee shop (or just love coffee!). Your job is to check the weather and monitor coffee bean stock daily, then email the manager if it’s rainy (customers want hot drinks!) or if stock is low. Doing this by hand is tedious—looking up forecasts, checking inventory spreadsheets, typing emails. Let’s automate it with:

  1. OpenWeatherMap API: Grabs the weather.

  2. Google Sheets API: Tracks coffee bean stock (a simple inventory).

  3. SMTP (Email): Sends an alert to your boss.

By the end, your script will check the weather and stock, then email something like:“Boss, it’s rainy in New York (42°F) and we’re low on beans—stock up!”

Step 1: Setting Up

You’ll need:

  • Python: With requests (Day 1) and gspread for Google Sheets (pip install gspread oauth2client).

  • OpenWeatherMap API Key: From Day 1.

  • Google Sheets API:

    • Create a Google Sheet (e.g., “Coffee Inventory”).

    • Add a row like: Beans Stock: 5 bags.

    • Enable the Google Sheets API and get credentials (see Google’s quickstart—save as credentials.json).

  • Email Setup: Use Gmail with an App Password (enable 2FA and generate one in settings).


Here’s the script:

python


import requests # For weather import gspread # For Google Sheets from oauth2client.service_account import ServiceAccountCredentials

import smtplib # For email

from email.mime.text import MIMEText # Weather API (OpenWeatherMap)

api_key = "YOUR_OPENWEATHERMAP_API_KEY"

city = "New York"

response = requests.get(url)

data = response.json()

temp = data["main"]["temp"]

description = data["weather"][0]["description"] is_rainy = "rain" in description.lower() # Google Sheets API (Inventory)

creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope) client = gspread.authorize(creds)

sheet = client.open("Coffee Inventory").sheet1

stock = int(sheet.cell(1, 2).value.split()[0]) # e.g., "5" from "5 bags"

low_stock = stock < 10 # Build the alert message

alert = f"Boss, it’s {temp}°F with {description} in {city}."

if is_rainy:     alert += " Rainy day—expect more hot drink orders!"

if low_stock:     alert += f" Stock is low: {stock} bags left—time to reorder!"

# Email setup

password = "YOUR_APP_PASSWORD"  

# Use Gmail App Password

msg = MIMEText(alert)

msg["Subject"] = "Daily Coffee Shop Alert"

msg["From"] = sender msg["To"] = receiver

# Send the email

with smtplib.SMTP("smtp.gmail.com", 587) as server:     server.starttls() server.login(sender, password) server.send_message(msg)

print("Email sent! Here’s the alert:")

print(alert)

How It Works

  1. Weather: Checks if it’s rainy (more hot drinks!).

  2. Google Sheets: Reads stock from cell B1 (e.g., “5 bags” → 5).

  3. Logic: Flags rainy days or low stock (<10 bags).

  4. Email: Sends the alert to your boss via Gmail’s SMTP.



    HOW THIS CODE WORK
    HOW THIS CODE WORK

Try It Out!

  1. Set up your APIs and credentials (replace placeholders like YOUR_OPENWEATHERMAP_API_KEY).

  2. Create a Google Sheet with “Beans Stock: 5 bags” in cell A1:B1.

  3. Save as coffee_alert.py and run with python coffee_alert.py.

  4. Automate it daily (like Day 2) with Task Scheduler or cron.

Your boss gets an email, and you look like a tech genius—no manual work required!

Why This Matters

  • Multiple APIs: You’ve combined data (weather, stock) and action (email).

  • Real Value: This saves time and prevents stockouts or missed sales.

  • Robot Efficiency: Your boss might wonder why they need you—until they realize you built the robot!

Day 3 Takeaways

  • APIs team up to tackle bigger tasks.

  • Automation isn’t just cool—it’s practical.

  • You’re now a multi-API maestro!

What’s Next?

For Day 4, we could:

  • Add a news API for daily coffee industry updates.

  • Try a no-code tool like Zapier.

  • Automate something wild—like tweeting latte art pics.


Comments


Sign our petition

Join us to unlock a world of innovative content from cutting-edge AI insights to actionable business strategies—your journey starts now!
Dynamic digital sketch, rough painterly

© 2023 by DBQs. All rights reserved.

bottom of page