Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 139 lines (111 sloc) 4.163 kB
#!/usr/bin/env python
from xml.dom.minidom import parseString
try:
from urllib.request import urlopen
except ImportError:
from urllib import urlopen
try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode
from sys import argv, exit, stderr
import os.path
VERSION = '.01'
URL = 'http://www.aaronfoltz.com'
def main():
try:
# Forecast option
if "-f" in argv[1]:
# See if there is a zip with the argument
try:
place = argv[2]
getForecast(place)
# If no zip is provided, get it from the geolocation service
except IndexError:
place = getPlace()
getForecast(place)
# Else there isn't an option there, it must be a zip code if anything
else:
try:
place = argv[1]
getCurrent(place)
# No arguments are provided, get the place from the geolocation service
except IndexError:
place = getPlace()
getCurrent(place)
except IndexError:
place = getPlace()
getCurrent(place)
def getCurrent(place):
# Call to get the weather
api = 'http://www.google.com/ig/api?%s' % urlencode({'weather': place})
dom = parseString(urlopen(api).read())
items = [ 'city'
, 'condition'
, 'temp_f'
, 'temp_c'
, 'humidity'
, 'wind_condition' ]
info = {}
try:
for item in items:
info[item] = dom.getElementsByTagName(item)[0].getAttribute('data')
except IndexError:
stderr.write('Invalid Area\n')
exit(1)
print('City: %s' % info['city'])
print('Condition: %s' % info['condition'])
print('Temperature: %sC/%sF' % (info['temp_c'], info['temp_f']))
print(info['humidity'])
print(info['wind_condition'])
def getForecast(place):
api = 'http://www.google.com/ig/api?%s' % urlencode({'weather': place})
dom = parseString(urlopen(api).read())
# Information that we can get in the forecast
items = [ 'day_of_week'
, 'high'
, 'low'
, 'condition']
# A main of information with 4 sublists - one for each day in the forecast
info = []
# Add city to the list
info.insert(0, dom.getElementsByTagName('city')[0].getAttribute('data'))
# Gather the number of days
days = dom.getElementsByTagName('day_of_week')
# Gather the information for each day
for day in range(1, len(days)+1):
# Add a sublist for the day
info.append([])
# Gather the available information for each day in the forecast
for item in items:
# Add the information to the end of the sublist for the day
info[day].append(dom.getElementsByTagName(item)[day-1].getAttribute('data'))
print('City: %s' % info[0])
# Print out the information for each day
for day in range(1, len(days)+1):
print('Day: %s' % info[day][0])
print('\tHigh: %dC/%sF' % (fahToCel(info[day][1]), info[day][1]))
print('\tLow: %dC/%sF' % (fahToCel(info[day][2]), info[day][2]))
print('\tCondition: %s' % info[day][3])
def fahToCel(value):
return (100.0/(212-32)) * (int(value) - 32)
def getPlace():
import re
path = "%s/.cliweather" % os.getenv("HOME")
if os.path.exists(path):
try:
f = open(path, 'r')
return f.read()
except:
print "Could not load .cliweather"
# Get the geoip information
geoip = urlopen('http://api.ipinfodb.com/v3/ip-city/?key=29e619f84fd83f32c4987573a4ccc11fd59b268234aa50c1289230f1fff0dbd2').read()
# The returned value is perfectly fine
if re.match("OK;;", geoip):
# regex to get the zip code
expression = "OK;;([^;]+;){5}(\w+)"
result = re.search(expression, geoip)
# return the zip code from the returned data
return result.group(2)
if __name__ == "__main__":
main()
Jump to Line
Something went wrong with that request. Please try again.