Skip to content

Instantly share code, notes, and snippets.

@monokrome
Created April 5, 2011 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monokrome/902933 to your computer and use it in GitHub Desktop.
Save monokrome/902933 to your computer and use it in GitHub Desktop.
A basic command-line weather interface in Python
#!/usr/bin/python
# <Knio> all of the things that remain constant (license, partner, whatever) should me the make class
# <Knio> and then when you ask it something it should return a Results object
# <Knio> and that object has .getTemp() or whatever
# <ScottG> Knio: I thought I had a good reason for setAttr but I don't see it now
# <Knio> so yeah, main class, SearchResults, and WeatherResults
# <Knio> unless searchresults is just like a list of strings, then it doesn't need to be a class
#TODO: Check for version numbers
import urllib
import sys
from xml.dom.minidom import *
class SearchResults:
def __init__(self, search_results_dom):
self.search = search_results_dom
self.error = ()
self.setErrorStatus()
def setErrorStatus(self):
error_type = None
error_message = None
error_node = None
error_node = self.search.getElementsByTagName('error')
if error_node:
error_node = error_node[0]
error_type = error_node.getElementsByTagName('err')[0].\
getAttribute('type')
error_message = error_node.\
getElementsByTagName('err')[0].childNodes[0].data
error = (int(error_type), str(error_message))
self.error = error
else:
self.error = ()
def getLocations(self):
if self.error:
return self.error
location_dict = {}
search_element_node = self.search.getElementsByTagName('search')[0]
if search_element_node.hasChildNodes():
locations = search_element_node.getElementsByTagName('loc')
for i, location in enumerate(locations):
location_dict[location.childNodes[0].data] =\
location.getAttribute('id')
return location_dict
class WeatherResults:
def __init__(self, weather_results_dom):
self.weather = weather_results_dom
self.error = ()
self.setErrorStatus()
self.setElements()
def setErrorStatus(self):
error_type = None
error_message = None
error_node = None
error_node = self.weather.getElementsByTagName('error')
if error_node:
error_node = error_node[0]
error_type = error_node.getElementsByTagName('err')[0].\
getAttribute('type')
error_message = error_node.\
getElementsByTagName('err')[0].childNodes[0].data
error = (int(error_type), error_message)
self.error = error
else:
self.error = ()
def setElements(self):
if self.error:
return self.error
self.head_element = self.weather.\
getElementsByTagName('head')[0]
self.loc_element = self.weather.\
getElementsByTagName('loc')[0]
self.cc_element = self.weather.\
getElementsByTagName('cc')[0]
def getTempUnits(self):
if self.error: return self.error
return self.head_element.\
getElementsByTagName('ut')[0].childNodes[0].data
def getDistUnits(self):
if self.error: return self.error
return self.head_element.\
getElementsByTagName('ud')[0].childNodes[0].data
def getSpeedUnits(self):
if self.error: return self.error
return self.head_element.\
getElementsByTagName('us')[0].childNodes[0].data
def getPUnits(self): #FIND OUT WHAT 'd' REPRESENTS IN THE WEATHER XML
if self.error: return self.error
return self.head_element.\
getElementsByTagName('us')[0].childNodes[0].data
def getRUnits(self): #FIND OUT WHAT 'd' REPRESENTS IN THE WEATHER XML
if self.error: return self.error
return self.head_element.\
getElementsByTagName('us')[0].childNodes[0].data
def getLocationID(self):
if self.error: return self.error
return self.loc_element.\
getAttribute('id')
def getLocationName(self):
if self.error: return self.error
return self.loc_element.\
getElementsByTagName('dnam')[0].childNodes[0].data
def getLocationTime(self):
if self.error: return self.error
return self.loc_element.\
getElementsByTagName('tm')[0].childNodes[0].data
def getLocationLat(self):
if self.error: return self.error
return self.loc_element.\
getElementsByTagName('lat')[0].childNodes[0].data
def getLocationLong(self):
if self.error: return self.error
return self.loc_element.\
getElementsByTagName('lon')[0].childNodes[0].data
def getLocationSunrise(self):
if self.error: return self.error
return self.loc_element.\
getElementsByTagName('sunr')[0].childNodes[0].data
def getLocationSunset(self):
if self.error: return self.error
return self.loc_element.\
getElementsByTagName('suns')[0].childNodes[0].data
def getLocationZoneNum(self):
if self.error: return self.error
return self.loc_element.\
getElementsByTagName('zone')[0].childNodes[0].data
def getLastUpdate(self):
if self.error: return self.error
return self.cc_element.\
getElementsByTagName('lsup')[0].childNodes[0].data
def getTemperature(self):
if self.error: return self.error
return self.cc_element.\
getElementsByTagName('tmp')[0].childNodes[0].data
def getFeelsLike(self):
if self.error: return self.error
return self.cc_element.\
getElementsByTagName('tmp')[0].childNodes[0].data
def getConditions(self):
if self.error: return self.error
return self.cc_element.\
getElementsByTagName('t')[0].childNodes[0].data
def getIconNum(self):
if self.error: return self.error
return self.cc_element.\
getElementsByTagName('icon')[0].childNodes[0].data
def getPressure(self):
if self.error: return self.error
return self.cc_element.getElementsByTagName('bar')[0].\
getElementsByTagName('r')[0].childNodes[0].data
def getPressureDirection(self):
if self.error: return self.error
return self.cc_element.getElementsByTagName('bar')[0].\
getElementsByTagName('d')[0].childNodes[0].data
def getWindSpeed(self):
if self.error: return self.error
return self.cc_element.getElementsByTagName('wind')[0].\
getElementsByTagName('s')[0].childNodes[0].data
def getGustSpeed(self):
if self.error: return self.error
return self.cc_element.getElementsByTagName('wind')[0].\
getElementsByTagName('gust')[0].childNodes[0].data
def getWindD(self): #FIND OUT WHAT 'd' REPRESENTS IN THE WEATHER XML
if self.error: return self.error
return self.cc_element.getElementsByTagName('wind')[0].\
getElementsByTagName('d')[0].childNodes[0].data
def getWindDirection(self):
if self.error: return self.error
return self.cc_element.getElementsByTagName('wind')[0].\
getElementsByTagName('t')[0].childNodes[0].data
def getHumidity(self):
if self.error: return self.error
return self.cc_element.\
getElementsByTagName('hmid')[0].childNodes[0].data
def getVisibility(self):
if self.error: return self.error
return self.cc_element.\
getElementsByTagName('vis')[0].childNodes[0].data
def getUVIndex(self):
if self.error: return self.error
return self.cc_element.getElementsByTagName('uv')[0].\
getElementsByTagName('i')[0].childNodes[0].data
def getUVRating(self):
if self.error: return self.error
return self.cc_element.getElementsByTagName('uv')[0].\
getElementsByTagName('t')[0].childNodes[0].data
def getDewPoint(self):
if self.error: return self.error
return self.cc_element.\
getElementsByTagName('dewp')[0].childNodes[0].data
def getMoonIconNum(self):
if self.error: return self.error
return self.cc_element.getElementsByTagName('moon')[0].\
getElementsByTagName('icon')[0].childNodes[0].data
def getMoonType(self):
if self.error: return self.error
return self.cc_element.getElementsByTagName('moon')[0].\
getElementsByTagName('t')[0].childNodes[0].data
class Weather():
def __init__(self, partner_id, license_key):
self.partner_id = partner_id
self.license_key = license_key
self.location_id = ''
self.forcast_days = ''
self.temperature_units = 's'
self.error = ()
self.weather = None
def getSearchResults(self, input_location):
search_url_file = urllib.urlopen(
'http://xoap.weather.com'
'/search/search?where=%s' %
input_location
)
search_results_dom = parse(search_url_file)
return SearchResults(search_results_dom)
def getWeatherResults(self, location_id, forcast_days, temperature_units):
weather_url_file = urllib.urlopen(
'http://xoap.weather.com/weather/'
'local/%(location_id)s?'
'cc=&'
'dayf=%(forcast_days)s&'
'link=xoap&'
'prod=xoap&'
'par=%(partner_id)s&'
'key=%(license_key)s&'
'unit=%(temperature_units)s' % {
'location_id': location_id,
'forcast_days':forcast_days,
'partner_id': self.partner_id,
'license_key': self.license_key,
'temperature_units': temperature_units
}
)
weather_results_dom = parse(weather_url_file)
return WeatherResults(weather_results_dom)
if __name__ == '__main__':
attr_dict = {'location': 'USNY0011', 'units': 'm'}
partner_id = 1072090735
license_key = '952089396d5c2cf0'
input_location = ''
if len(sys.argv) != 2:
print >> sys.stderr, 'Must supply exactly 1 argument'
sys.exit(1)
input_location = sys.argv[1]
weather = Weather(partner_id, license_key)
search_results = weather.getSearchResults(input_location)
search_results_dict = search_results.getLocations()
if not search_results_dict:
print 'No search results'
sys.exit(0)
if len(search_results_dict) == 1: location_id = search_results_dict[search_results_dict.keys()[0]]
else:
search_results_dict_keys = search_results_dict.keys()
for i, location in enumerate(search_results_dict.keys()):
print '(', i, ')', location, " ", search_results_dict[location]
location_id = search_results_dict[\
search_results_dict_keys[\
int(raw_input('Enter code: '))
]
]
forcast_days = ''
temperature_units = ''
weather_results = weather.getWeatherResults(
location_id, forcast_days, temperature_units
)
print 'weather_results.getTempUnits() =',\
weather_results.getTempUnits()
print 'weather_results.getDistUnits() =',\
weather_results.getDistUnits()
print 'weather_results.getSpeedUnits() =',\
weather_results.getSpeedUnits()
print 'weather_results.getPUnits() =',\
weather_results.getPUnits()
print 'weather_results.getRUnits() =',\
weather_results.getRUnits()
print 'weather_results.getLocationID() =',\
weather_results.getLocationID()
print 'weather_results.getLocationName() =',\
weather_results.getLocationName()
print 'weather_results.getLocationTime() =',\
weather_results.getLocationTime()
print 'weather_results.getLocationLat() =',\
weather_results.getLocationLat()
print 'weather_results.getLocationLong() =',\
weather_results.getLocationLong()
print 'weather_results.getLocationSunrise() =',\
weather_results.getLocationSunrise()
print 'weather_results.getLocationSunset() =',\
weather_results.getLocationSunset()
print 'weather_results.getLocationZoneNum() =',\
weather_results.getLocationZoneNum()
print 'weather_results.getLastUpdate() =',\
weather_results.getLastUpdate()
print 'weather_results.getTemperature() =',\
weather_results.getTemperature()
print 'weather_results.getFeelsLike() =',\
weather_results.getFeelsLike()
print 'weather_results.getConditions() =',\
weather_results.getConditions()
print 'weather_results.getIconNum() =',\
weather_results.getIconNum()
print 'weather_results.getPressure() =',\
weather_results.getPressure()
print 'weather_results.getPressureDirection() =',\
weather_results.getPressureDirection()
print 'weather_results.getWindSpeed() =',\
weather_results.getWindSpeed()
print 'weather_results.getGustSpeed() =',\
weather_results.getGustSpeed()
print 'weather_results.getWindD() =',\
weather_results.getWindD()
print 'weather_results.getWindDirection() =',\
weather_results.getWindDirection()
print 'weather_results.getHumidity() =',\
weather_results.getHumidity()
print 'weather_results.getVisibility() =',\
weather_results.getVisibility()
print 'weather_results.getUVIndex() =',\
weather_results.getUVIndex()
print 'weather_results.getUVRating() =',\
weather_results.getUVRating()
print 'weather_results.getDewPoint() =',\
weather_results.getDewPoint()
print 'weather_results.getMoonIconNum() =',\
weather_results.getMoonIconNum()
print 'weather_results.getMoonType() =',\
weather_results.getMoonType()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment