Skip to content

Instantly share code, notes, and snippets.

@alessandrocucci
Created May 22, 2015 11:26
Show Gist options
  • Save alessandrocucci/764e0ac1db04ccb703ab to your computer and use it in GitHub Desktop.
Save alessandrocucci/764e0ac1db04ccb703ab to your computer and use it in GitHub Desktop.
Meteo info script
#!/usr/bin/env python
__author__ = "Alessandro Cucci"
__license__ = "MIT"
__version__ = "0.0.1"
__email__ = "[email protected]"
__status__ = "Development"
import urllib, json
from datetime import datetime
import string
EXIT = ['no', 'No', 'nope', 'Nope', 'quit', 'Quit']
class Weather:
"""
Utilizza OpenWeatherMap.org per avere info sulle previsioni meteo.
"""
def __init__(self, type=None):
print '\n'*2
print "Meteo news by OpenWeatherMap.org"
print '\n'*2
if not type:
city = ''
while city not in EXIT:
city = raw_input("What city sir? \n")
print '\n'*2
self.city = city
now = self.three_hours(city)
city = raw_input("Done, sir. Would U like to search for another city? \n")
elif type == 'week':
city = ''
while city not in EXIT:
city = raw_input("What city sir? \n")
print '\n'*2
self.city = city
week = self.week(city)
city = raw_input("Done, sir. Would U like to search for another city? \n")
def three_hours(self, city):
URL = 'http://api.openweathermap.org/data/2.5/weather?q=' + city
try:
response = urllib.urlopen(URL);
data = json.loads(response.read())
#Nome Citta'
print data['name'], '-', data['sys']['country']
print 'coord.', str(data['coord']['lat']) + 'lat', str(data['coord']['lon']) + 'lon'
print '\nIl sole sorge alle:', datetime.fromtimestamp(data['sys']['sunrise']).strftime('%H:%M')
print 'e tramonta alle:', datetime.fromtimestamp(data['sys']['sunset']).strftime('%H:%M')
print
#Meteo
condition = data['weather'][0]['description']
print 'Weather:\n\t', condition
if 'rain' in data:
print "\train:", data['rain']['3h'], 'mm'
print '\twind', data['wind']['speed'], 'kn'
#Temperatura attuale
print
print 'Temp:', data['main']['temp'] - 272.15
#Umidita'
print
print 'Humidity:', str(data['main']['humidity']) + '%'
print '\n'*2
print 'updated:', datetime.fromtimestamp(data['dt']).strftime('%d-%m-%Y %H:%M:%S')
print '\n'*2
except:
print "Data not available, sorry"
return True
def week(self, city):
#GET THE CITY ID
URL = 'http://api.openweathermap.org/data/2.5/weather?q=' + city
try:
response = urllib.urlopen(URL);
data = json.loads(response.read())
city_id = str(data['id'])
URL = 'http://api.openweathermap.org/data/2.5/forecast/daily?id=' + city_id + '&cnt=7'
response = urllib.urlopen(URL);
data = json.loads(response.read())
#Nome Citta'
print data['city']['name'], '-', data['city']['country']
print 'coord.', str(data['city']['coord']['lat']) + ' lat', str(data['city']['coord']['lon']) + ' lon'
for i, item in enumerate(data['list']):
print '\n'
print datetime.fromtimestamp(item['dt']).strftime('%d-%m-%Y')
#Meteo
condition = string.capwords(item['weather'][0]['description'])
print 'Weather:\n\t', condition
if 'rain' in item:
print "\train:", item['rain'], 'mm'
print '\twind', item['speed'], 'kn'
#Temperatura attuale
print 'Temp:\n\t', 'max:', item['temp']['max'] - 272.15, '\n\tmin:', item['temp']['min'] - 272.15
if i < len(data['list']) - 1:
raw_input("Press any key to see the next day")
except:
print "Data not available, sorry"
return True
#TEST
def main():
selection = 0
while selection not in xrange(1,4):
print "Please select:"
print '\n'
print "1 - Current"
print "2 - Week"
print "3 - Exit"
selection = int(raw_input())
if selection == 1:
weather = Weather()
main()
elif selection == 2:
weather = Weather('week')
main()
elif selection == 3:
pass
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment