indigoparadox

status-weather.py

Mar 2nd, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import re, urllib2, sys
  4.  
  5. if 2 > len( sys.argv ):
  6.    print "ZIP Code not specified!"
  7.    exit( 1 )
  8. else:
  9.    arg_zip = sys.argv[1]
  10.  
  11. # TODO: Make ZIP code configurable by environment variable.
  12. weather_url = 'http://mobile.wunderground.com/cgi-bin/findweather/getForecast?brand=mobile&query=' + arg_zip
  13. weather_regex_temp = re.compile( '<td>Temperature<\/td>.  <td>.  <span class="nowrap"><b>([0-9.]+)<\/b>&deg;F', re.MULTILINE | re.DOTALL )
  14. weather_regex_cond = re.compile( '<td>Conditions<\/td>.<td><b>([A-Za-z\-. ]+)<\/b>', re.MULTILINE | re.DOTALL )
  15. weather_regex_hum = re.compile( '<td>Humidity<\/td>.<td><b>([0-9%.]+)<\/b>', re.MULTILINE | re.DOTALL )
  16. weather_regex_wind = re.compile( '<td>Wind<\/td>.<td>.<b>([NESW]+|West|North|South|East)<\/b> at.  <span class="nowrap"><b>([0-9.])', re.MULTILINE | re.DOTALL )
  17.  
  18. # = Main Procedure =
  19.  
  20. # Read the body of the weather page.
  21. weather_file = urllib2.urlopen( weather_url )
  22. weather_body = weather_file.read( 4096 )
  23.  
  24. # Get the temperature.
  25. weather_match_temp = weather_regex_temp.search( weather_body )
  26.  
  27. # Get the conditions.
  28. weather_match_cond = weather_regex_cond.search( weather_body )
  29.  
  30. # Get the humidity.
  31. weather_match_hum = weather_regex_hum.search( weather_body )
  32.  
  33. # Get the wind.
  34. weather_match_wind = weather_regex_wind.search( weather_body )
  35.  
  36. print weather_match_temp.groups()[0] + 'F, ' + \
  37.    weather_match_hum.groups()[0] + ' Humidity, ' + \
  38.    weather_match_cond.groups()[0] + ', ' + \
  39.    'Wind: ' + weather_match_wind.groups()[0] + ' ' + \
  40.    weather_match_wind.groups()[1] + 'mph'
Advertisement
Add Comment
Please, Sign In to add comment