Changeset 190:75d280708ff5 for StormSiren/XmlWeather.py
- Timestamp:
- 11/29/08 00:48:18 (3 years ago)
- Branch:
- default
- Parents:
- 189:8190540dae37 (diff), 185:c20e85874308 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - Files:
-
- 1 modified
-
StormSiren/XmlWeather.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
StormSiren/XmlWeather.py
r184 r190 42 42 """ 43 43 44 import urllib45 44 import logging 46 import sys47 from xml.dom import minidom48 45 49 46 from WeatherTypes import * 50 from CAP import CAP 47 from XmlFetcher import * 48 from WeatherAlert import WeatherAlert 51 49 52 50 class XmlWeather(object): 53 def __init__(self, state, history, simfetch = False):51 def __init__(self, state, simfetch = False, proxy = None): 54 52 super(XmlWeather,self).__init__() 55 53 self.__data = '' 56 54 self.__state = state 57 55 self.__dom = [] 58 self.__baseurl = 'http://www.weather.gov/alerts/' 59 self.__namespace = 'http://purl.org/dc/elements/1.1/' 60 self.__url = self.__baseurl + self.__state.lower() + ".cap" 61 self.__tag_item = 'cap:info' 62 self.__history = history 56 self.__baseurl = 'http://www.weather.gov/wwarss-tst/' 57 self.__url = self.__baseurl + self.__state.lower() + ".php?x=0" 58 self.__tag_item = 'entry' 63 59 self.__simfetch = simfetch 64 self.log = logging.getLogger("XmlWeather") 60 self.__proxy = proxy 61 self.__atoms = XmlFetcher(self.__url, self.__simfetch, self.__proxy) 62 self.log = logging.getLogger("XmlWeather") 65 63 66 64 self.log.setLevel(logging.DEBUG) … … 74 72 75 73 def __fetch(self): 76 self.log.info("Fetching: " + self.__url) 77 try: 78 if(self.__simfetch): 79 self.log.info("Simulating fetch using examples directory") 80 self.__dom = minidom.parse("examples/" + self.__state.lower() + ".cap") 81 else: 82 self.__dom = minidom.parse(urllib.urlopen(self.__url)) 83 except IOError,e: 84 # No net connection available, exit. 85 self.log.error("Internet connection not available?, exiting: %s" % e) 86 sys.exit() 74 self.__dom = self.__atoms.fetch() 87 75 88 76 def __str__(self): 89 77 str = "XmlWeather:\n" + \ 78 "SimFetch: " + self.__simfetch + "\n" + \ 79 "Proxy: " + self.__proxy + "\n" + \ 90 80 "State: " + self.__state + "\n" + \ 91 81 "URL: " + self.__url + "\n" 92 93 if(self.__simfetch):94 str += "Sim Fetch: yes\n"95 else:96 str += "Sim Fetch: no\n"97 82 return str 98 83 … … 102 87 def __parse(self): 103 88 for capXmlText in self.__dom.getElementsByTagName(self.__tag_item): 104 cap_item = CAP(capXmlText, self.__state) 105 #cap_item.display() 106 if(not self.__history.exists(cap_item.id)): 107 alerted = self.handleAlert(cap_item) 108 if(alerted): 109 self.__history.add(cap_item.id) 110 else: 111 self.log.debug("Ignoring %s: Already alerted on" % cap_item.id) 89 self.handleAtom(WeatherAlert(capXmlText, self.__state)) 112 90 91 def getSimFetch(self): 92 return self.__simfetch 113 93 114 def handleAlert(self,cap_item): 115 self.log.debug("handleAlert(%s) - %s/%s" % (cap_item.id, cap_item.county, cap_item.state)) 116 return True 94 simfetch = property(getSimFetch,None,None) 95 96 def getProxy(self): 97 return self.__proxy 98 99 proxy = property(getProxy,None,None)
