Skip to content

Instantly share code, notes, and snippets.

@unex
Created February 2, 2020 06:36
Show Gist options
  • Save unex/b47374a459ac9ac09a950d9c5d3d0a5a to your computer and use it in GitHub Desktop.
Save unex/b47374a459ac9ac09a950d9c5d3d0a5a to your computer and use it in GitHub Desktop.
ECE103 - Scrape the last 36 months of temperature data and wrap it up in a nice little csv file with a bow on top.
# Pulls the last 36 months of temp data and writes it to a csv file for ezpz use in matlab using csvread()
import os
import requests
import datetime
import statistics
import csv
API_KEY = os.environ.get("API_KEY") # wunderground has no public API so you have to yoink this from a browser request
LOCATION = "KFNL:9:US"
today = datetime.datetime.today()
month = today.month
year = today.year
with open('temps.csv', 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=["date", "min_temp", "max_temp", "avg_temp"])
writer.writeheader()
while today.year - year < 3:
for m in range(12):
r = requests.get(f'https://api.weather.com/v1/location/{LOCATION}/observations/historical.json?apiKey={API_KEY}&units=e&startDate={year}{month:02d}01', headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'})
print(f'{year}-{month:02d}-01 {r}')
obs = r.json()["observations"]
avg_temp = round(statistics.mean([ob["temp"] for ob in obs]), 2)
data = list(filter(lambda x: x["min_temp"] and x["max_temp"], obs))
# API does not aloways return min and max for some reason, may have to manually fill in some
if data:
writer.writerow({'date': f'{year}{month:02d}', 'min_temp': data[0]["min_temp"], 'max_temp': data[0]["max_temp"], "avg_temp": avg_temp})
else:
writer.writerow({'date': f'{year}{month:02d}', 'min_temp': '', 'max_temp': '', "avg_temp": avg_temp})
month -= 1
if month == 0:
month = 12
year -= 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment