-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeather.py
50 lines (43 loc) · 1.4 KB
/
Weather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/python2
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 15 12:43:54 2015
@author: Simon
"""
from pyowm import OWM # Source: https://github.com/csparpa/pyowm
import argparse
import datetime
import json
with open('keys.json', 'r') as f:
key_dict = json.load(f)
api_key = key_dict['api_key']
# Parse arguments
parser = argparse.ArgumentParser(description='Show the current weather.')
parser.add_argument("-l", "--location", help="The location where you want to know the weather", default="Tampere, FI")
args = parser.parse_args()
location = args.location
# Collect info
owm = OWM(API_key=api_key, language='en')
observation = owm.weather_at_place(location)
weather = observation.get_weather()
temp = weather.get_temperature(unit='celsius')['temp']
status = weather.get_status()
forecaster = owm.three_hours_forecast(location)
forecast = forecaster.get_forecast()
location = observation.get_location().get_name()
print('')
print('Current weather in %s:' % (location))
print('Temperature: %.1f degrees celsius' % (temp))
print('Sky: %s' % (status))
print('')
print('Forecast:')
for i in range(4):
w = forecast.get(i)
time_unix = w.get_reference_time()
time_local = datetime.datetime.fromtimestamp(
int(time_unix)
).strftime('%Y-%m-%d %H:%M')
status = w.get_status()
temp = w.get_temperature(unit='celsius')['temp']
print('%s %s %.1f degrees' % (time_local, status, temp))
print('')