Tag Archives: stornoway

My #RaspberryPi #Python code for tweeting the MetOffice weather for #BrizeNorton #Stornoway #BurnhamOnSea & #Newquay

I have been developing this python code and I found a great way of sharing my code but not my API keys! I also made a Python ‘function’ so that new locations can simply be added by defining a name and the longitude and latitude coordinates (the Met office API does the hard work is finding the nearest weather station to these coordinates) and calling this function with the 3 parameters.

For updates see this repository – https://github.com/soneups/MetOffice

#!/usr/bin/env python
# Tweet some weather data!

import time
import sys
import metoffer
from twython import Twython

# Define location of api files - recorded in a file - avoids a GitHub slurp for API keys!

# Read API keys from file
with open(keys_file) as f:
    CONSUMER_KEY = f.readline().strip("\n")
    CONSUMER_SECRET = f.readline().strip("\n")
    ACCESS_KEY = f.readline().strip("\n")
    ACCESS_SECRET = f.readline().strip("\n")

with open(metkeys_file) as f:
    met_api_key = f.readline().strip("\n")
# Setup API calls
met = metoffer.MetOffer(met_api_key)
# get local weather
def LookupObs( locationdescription, longitude, latitude ):
   weather_observations = met.nearest_loc_obs(float(longitude),float(latitude)) 
   weather_report = metoffer.parse_val(weather_observations)
   weather_data = weather_report.data[len(weather_report.data)-1]
   wind_info = weather_data['Wind Speed']
   wind_direction = weather_data['Wind Direction']
   print (time.strftime("%d/%m/%Y"))+"@"+(time.strftime("%H:%M:%S")) + " - " +locationdescription+": "+str((weather_data['Temperature'][0]))+"c/"+str(tmpf)+"f, Wind speed: "+str(wind_info[0])+wind_info[1]+" "+wind_direction[0]
   api.update_status(status=str(time.strftime("%d/%m/%Y"))+"@"+str(time.strftime("%H:%M:%S")) + " - " +locationdescription+": "+str((weather_data['Temperature'][0]))+"c/"+str(tmpf)+"f, Wind speed: "+str(wind_info[0])+wind_info[1]+" "+wind_direction[0])

LookupObs( locationdescription="#BrizeNorton", longitude=51.7616, latitude=-1.5779 );   #Brize
LookupObs( locationdescription="#Stornoway", longitude=58.1011, latitude=-4.9899 );     #Stornoway
LookupObs( locationdescription="#Burnhamonsea", longitude=51.2430, latitude=-2.9920 );  #BOS
LookupObs( locationdescription="#Newquay", longitude=50.3995, latitude=-5.1379 );       #Newquay