Weather Info Script (Openweathermap.org api)

0 Ozan Enginoglu · April 19, 2015
Hi there,

After training some of functions of urllib library, I wanted to work on a nice weather project where it retrieves weather info from openweathermap.org api and prints our some useful information about the city I live. So here is the code I could write and then i will ask my question.

#!/usr/bin/env python

# Ozan Enginoglu - 17 Nisan 2015

import urllib.request
import json
from time import sleep

weather_update_interval = 60 # 1 Minute(s)

def get_weather():
    # Acquire and decode the openweathermap.org weather information
    api_url = 'http://api.openweathermap.org/data/2.5/weather?id='
    api_url += '311046' # Izmir ID
    api_url += '&units=metric' # Metric Units
    url = urllib.request.urlopen(api_url) # Load the api web page
    api_data = url.read() # Read the page and assign the data to a variable
    decoded_output = json.loads(api_data.decode()) # Convert the api_data to an appr. format
    return decoded_output

#def is_weather_updated(previous_weather_info, weather_info):
#    if(weather_info['main']['temp']==previous_weather_info['main']['temp']):
#        return 0
#    else:
#        return 1
    

while True: # This is the main loop
    weather_info = get_weather() # Get weather information from the server
#    if(is_weather_updated(previous_weather_info,weather_info)): # If the weather info is different print...
    print('Temp %s\nHumidity %s\nPressure %s\nWind Speed %s\n' % (weather_info['main']['temp'],
                                                                  weather_info['main']['humidity'],
                                                                  weather_info['main']['pressure'],
                                                                  weather_info['wind']['speed']))
#        previous_weather_info = weather_info # Store the newly received data as previous one
    sleep(weather_update_interval)


Actually this code works as it is and it prints out the weather data every weather_update_interval [second] but I want to add an extra feature to my script. Since the data pulled from the server does not change every interval (it changes approximately every 2 hours) i want to check if the weather info is changed before printing it. I tried some things (commented parts in the code) but could not figure it out.

I hope you guys will help me.

Thank you,

Post a Reply

Replies

Oldest  Newest  Rating
0 Michael Okoloba · April 20, 2015
# Ozan Enginoglu - 17 Nisan 2015

import urllib.request
import json
from time import sleep

weather_update_interval = 60 # 1 Minute(s)

changed = True # global variables are bad but for this case its manageable
counter = 0 # comment this whole line and corresponding if statement inside While True
def get_weather():
    # Acquire and decode the openweathermap.org weather information
    api_url = 'http://api.openweathermap.org/data/2.5/weather?id='
    api_url += '311046' # Izmir ID
    api_url += '&units=metric' # Metric Units
    url = urllib.request.urlopen(api_url) # Load the api web page
    api_data = url.read() # Read the page and assign the data to a variable
    decoded_output = json.loads(api_data.decode()) # Convert the api_data to an appr. format
    return decoded_output

def is_weather_updated(old_weather): # You can even use this function but and store old_weather as global variable contact me if you have any probs
    weather_info = get_weather()

    new_weather = [weather_info['main']['temp'],\
                    weather_info['main']['humidity'],\
                    weather_info['main']['pressure'],\
                    weather_info['wind']['speed']
                ]

    weather = [i for i in old_weather if i not in new_weather]

    if len(weather) > 0:
        return True
    else:
        return False
    

while True: # This is the main loop
    weather_info = get_weather() # Get weather information from the server

    while changed:
        old_weather = [
                    weather_info['main']['temp'],\
                    weather_info['main']['humidity'],\
                    weather_info['main']['pressure'],\
                    weather_info['wind']['speed']
                ]
        
        print('Temp %s\nHumidity %s\nPressure %s\nWind Speed %s\n' % (weather_info['main']['temp'],
                                                                  weather_info['main']['humidity'],
                                                                  weather_info['main']['pressure'],
                                                                  weather_info['wind']['speed']))

        changed = is_weather_updated(old_weather)

    print("Sleeping for 1min... btw weather has changed {0} time(s)" .format(counter)) # put counter in place of {0} its zero based like list
    sleep(weather_update_interval)
    counter +=1
    print("Am awake... going for check")
    changed = is_weather_updated(old_weather) # outer loop does a check if weather has change it becomes true (changed) so inner loop can run again

#        previous_weather_info = weather_info # Store the newly received data as previous one
   


Let me know if this helps
0 Ozan Enginoglu · April 20, 2015
Michael thank you for the code. I certainly could follow and learn from your code. I guess now it is time for me to make a gui (using Tkinter) for this small application.
0 Michael Okoloba · April 21, 2015
Am glad it was helpful. I never played with GUI on python really. In my thoughts I would say work with db and write to csv. While you have done well getting data. I have a site you can try to scrap. If you want i can give you it would help think because the site is messy. The real world is messy though.
  • 1

Python

107,003 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator