Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import re, urllib2, sys
- if 2 > len( sys.argv ):
- print "ZIP Code not specified!"
- exit( 1 )
- else:
- arg_zip = sys.argv[1]
- # TODO: Make ZIP code configurable by environment variable.
- weather_url = 'http://mobile.wunderground.com/cgi-bin/findweather/getForecast?brand=mobile&query=' + arg_zip
- weather_regex_temp = re.compile( '<td>Temperature<\/td>. <td>. <span class="nowrap"><b>([0-9.]+)<\/b>°F', re.MULTILINE | re.DOTALL )
- weather_regex_cond = re.compile( '<td>Conditions<\/td>.<td><b>([A-Za-z\-. ]+)<\/b>', re.MULTILINE | re.DOTALL )
- weather_regex_hum = re.compile( '<td>Humidity<\/td>.<td><b>([0-9%.]+)<\/b>', re.MULTILINE | re.DOTALL )
- 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 )
- # = Main Procedure =
- # Read the body of the weather page.
- weather_file = urllib2.urlopen( weather_url )
- weather_body = weather_file.read( 4096 )
- # Get the temperature.
- weather_match_temp = weather_regex_temp.search( weather_body )
- # Get the conditions.
- weather_match_cond = weather_regex_cond.search( weather_body )
- # Get the humidity.
- weather_match_hum = weather_regex_hum.search( weather_body )
- # Get the wind.
- weather_match_wind = weather_regex_wind.search( weather_body )
- print weather_match_temp.groups()[0] + 'F, ' + \
- weather_match_hum.groups()[0] + ' Humidity, ' + \
- weather_match_cond.groups()[0] + ', ' + \
- 'Wind: ' + weather_match_wind.groups()[0] + ' ' + \
- weather_match_wind.groups()[1] + 'mph'
Advertisement
Add Comment
Please, Sign In to add comment