Skip to content

Commit

Permalink
012
Browse files Browse the repository at this point in the history
  • Loading branch information
bbelderbos committed Apr 10, 2017
1 parent 4397877 commit ee3bd0a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
2 changes: 2 additions & 0 deletions 012/requirements.txt
@@ -0,0 +1,2 @@
pytz==2017.2
requests==2.13.0
55 changes: 55 additions & 0 deletions 012/weather.py
@@ -0,0 +1,55 @@
from datetime import datetime
import os

import pytz
import requests

API_KEY = os.environ.get('WEATHER_API')
BASE_URL = 'http://api.openweathermap.org/data/2.5/weather?q='

TIMEZONES = {
'Sydney': 'Australia/Sydney',
'Alicante': 'Europe/Madrid',
}

# API docs: http://openweathermap.org/current
URL = ('http://api.openweathermap.org/data/2.5/weather?'
'q={}&mode=json&units=metric&appid={}')


def get_weather(cities=TIMEZONES.keys()):
fmt = 'In {} the weather is: {}, today sun rises at {} and sets at {}'

output = []
for city in cities:
resp = requests.get(URL.format(city, API_KEY))
info = resp.json()
main = info["weather"][0]["main"]
sunrise = get_local_tstamp(city, info["sys"]["sunrise"])
sunset = get_local_tstamp(city, info["sys"]["sunset"])
output.append(fmt.format(city, main, sunrise, sunset))

return '\n'.join(output)


def get_local_tstamp(city, utstamp):
if city not in TIMEZONES:
raise ValueError('Not a valid city, check what you call me with')
tz = TIMEZONES[city]

try:
utstamp = int(utstamp)
except ValueError:
raise

# http://stackoverflow.com/questions/12978391/localizing-epoch-time-with-pytz-in-python
utc_dt = datetime.utcfromtimestamp(utstamp).replace(tzinfo=pytz.utc)
loc_tz = pytz.timezone(tz)
dt = utc_dt.astimezone(loc_tz)

fmt = '%H:%M:%S %Z%z'
return dt.strftime(fmt)


if __name__ == '__main__':
print(get_weather())
2 changes: 1 addition & 1 deletion LOG.md
Expand Up @@ -13,7 +13,7 @@
| 009 | Apr 07, 2017 | [interactive script to create a new Pelican blog article](009) | lot of known concepts, but nice to bring a lot of functionality together, and above all a really useful script for our blog |
| 010 | Apr 08, 2017 | [script to spot cheap @transavia flights using their #API](010) | this was a nice exercise, and a very useful script for a monitoring cron job. TODO: wrap it in a Flask web app. Some modules I explored: calendar, datetime, dateutil.relativedelta, requests_cache |
| 011 | Apr 09, 2017 | [generic script to email the contents of a text file](011) | a script that uses your gmail account to email the contents of a text file. Current use case is to email web scraped data. smtplib, email, MIME |
| 012 | Apr 10, 2017 | [TITLE](012) | LEARNING |
| 012 | Apr 10, 2017 | [using OpenWeatherMap #API to compare weather in Australia vs Spain](012) | OpenWeatherMap API, pytz for timezone handling, datetime.utcfromtimestamp to parse unix timestamp to datetime, Google confirms sunset / sunrise times correct :) |
| 013 | Apr 11, 2017 | [TITLE](013) | LEARNING |
| 014 | Apr 12, 2017 | [TITLE](014) | LEARNING |
| 015 | Apr 13, 2017 | [TITLE](015) | LEARNING |
Expand Down

0 comments on commit ee3bd0a

Please sign in to comment.