Mastodon
Go back

An Alfred (OS X) Workflow for Weather Underground Personal Weather Station (PWS) Data

I’ve operated a Weather Underground Personal Weather Station (PWS) [KMEBERWI7] off-and-on (hardware issues notwithstanding) for as long as I can remember, and I thought it was about time to finally do an Alfred↔PWS mashup. My personal requirements were quite modest:

Alfred makes all this possible via customized workflows that support many scripting environments, including Python. Here’s a quick preview before going into the details:

pws-alfred-results

It’s a fairly simple workflow:

The full Python code is below and on github, and you can hit that github link or Packal for the compiled workflow. It’s been tested on Mavericks, but more eyes are always welcome.

Customizing the workflow for your own/favorite PWS is as simple as changing the station variable.

There’s plenty of room for improvement, including

Don’t hesitate to post issues or pull requests and check back for updates (as I’m sure some of those improvements will be making their way into the workflow).

import re
import csv
import sys
import datetime
from lib import requests
from workflow import Workflow, web
from StringIO import StringIO

# retrieve today's history for station "X"

def get_wx_data(station):

  tdy = datetime.datetime.today()
    
  # construct the URL for "today"
  url = 'http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=%s&day=%d&month=%d&year=%d&graphspan=day&format=1' % (station, tdy.day, tdy.month, tdy.year)
    
  r = web.get(url) # get the data
     
  r.raise_for_status() # report any errors
    
  return(re.sub("\n\n", "\n", re.sub("^\n|<br>", "", r.text))) # clean up the output & pass it back to main control


# main workflow control

def main(wf):

  station = "KMEBERWI7" # change to use your own/favorite station

  resp = get_wx_data(station)

  # only want last 5 readings, change this to whatever you want
  max = 5

  i=0
  for row in reversed(list(csv.reader(StringIO(resp)))):
    wf.add_item(title=row[0] + " | " + row[1] + "F | " + row[3] + "in | " + row[8] + "%", 
                subtitle=station, # so you know where you're getting data from
                arg=station, # passed as query to URL action - http://www.wunderground.com/personal-weather-station/dashboard?ID={query}#history
                valid=True, # it can be opened in the browser
                icon="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarInfo.icns", # info icon
                copytext=resp) # get the whole CSV file in a copy
    if (i==max): break
    i += 1

  # output to alfred

  wf.send_feedback()

if __name__ == u"__main__":
  wf = Workflow()
  sys.exit(wf.run(main))

Join the conversation:follow@hrbrmstr@rud.ison the fediverse to read and reply to this post from your own instance.

No comments yet.


Previous Post
Charting/Mapping the Scottish Vote with R (an rvest/dplyr/tidyr/TopoJSON/ggplot tutorial)
Next Post
R version of "An exploratory technique for visualizing the distributions of 100 variables:"