def _get_high_tide_value(self, AM=True, jour=0): if AM: time = DateTime(CleanText( '//tr[@id="MareeJours_%s"]/td[1]/b[1]' % jour), strict=False)(self) value = CleanDecimal('//tr[@id="MareeJours_0"]/td[2]/b[1]', replace_dots=True)(self) else: time, value = None, None if len( XPath('//tr[@id="MareeJours_%s"]/td[1]/b' % jour)(self)) > 1: time = DateTime(CleanText( '//tr[@id="MareeJours_%s"]/td[1]/b[2]' % jour), strict=False, default=None)(self) value = CleanDecimal('//tr[@id="MareeJours_0"]/td[2]/b[2]', replace_dots=True, default=None)(self) if time and value: measure = GaugeMeasure() measure.level = float(value) measure.date = time + timedelta(days=jour) return measure
def get_sensors_list(self): paraphs = self.document.xpath('//p[@align="center"]') sensors = [] for i in range(len(paraphs)): sensor = GaugeSensor("dd-%s" % self.name[i].lower()) sensor.name = self.name[i] sensor.unit = self.unit[i] sensor.forecast = NotAvailable sensor.history = NotAvailable sensor.gaugeid = u"private-dresden" paraph = paraphs[i] lastvalue = GaugeMeasure() lastvalue.alarm = NotAvailable if i == 0: text = paraph.xpath('b/span/font[@size="4"]')[1].text lastvalue.level = float(text.split('\n')[1].split(u'°')[0]) if i == 1: text = paraph.xpath('b/span/font')[2].text lastvalue.level = float(text.split('\n')[1]) if i == 2: text = paraph.xpath('span/font/b')[0].text lastvalue.level = float(text.split('\n')[2].split('hPa')[0]) if i == 3: text = paraph.xpath('span/font[@size="4"]/b')[0].text lastvalue.level = float(text.split('\n')[2].split(u'%')[0] .split(':')[1]) if i == 4: text = paraph.xpath('b/font[@size="4"]/span')[0].text lastvalue.level = float(text.split('\n')[0]) if i == 5: text = paraph.xpath('b/font/span')[0].text lastvalue.level = float(text.split('\n')[1]) sensor.lastvalue = lastvalue sensors.append(sensor) return sensors
def _create_attach_sensor(self, value, gauge_id, last_update, adresse): levelattach = GaugeSensor(gauge_id + '-attach') levelattach.name = u'Attach' levelattach.address = u'%s' % adresse lastvalue = GaugeMeasure() if lastvalue.level < 1: lastvalue.alarm = u'Full station' lastvalue.level = float(value) lastvalue.date = last_update levelattach.lastvalue = lastvalue levelattach.history = NotLoaded levelattach.gaugeid = gauge_id return levelattach
def _create_bikes_sensor(self, value, gauge_id, last_update, adresse): levelbikes = GaugeSensor(gauge_id + '-bikes') levelbikes.name = u'Bikes' levelbikes.address = u'%s' % adresse lastvalue = GaugeMeasure() lastvalue.level = float(value) lastvalue.date = last_update if lastvalue.level < 1: lastvalue.alarm = u'Empty station' levelbikes.lastvalue = lastvalue levelbikes.history = NotLoaded levelbikes.gaugeid = gauge_id return levelbikes
def get_sensors_list(self): paraphs = self.document.xpath('//p[@align="center"]') sensors = [] for i in range(len(paraphs)): sensor = GaugeSensor("dd-%s" % self.name[i].lower()) sensor.name = self.name[i] sensor.unit = self.unit[i] sensor.forecast = NotAvailable sensor.history = NotAvailable sensor.gaugeid = u"wetter" paraph = paraphs[i] lastvalue = GaugeMeasure() lastvalue.alarm = NotAvailable if i == 0: text = paraph.xpath('b/span/font[@size="4"]')[1].text lastvalue.level = float(text.split('\n')[1].split(u'°')[0]) if i == 1: text = paraph.xpath('b/span/font')[2].text lastvalue.level = float(text.split('\n')[1]) if i == 2: text = paraph.xpath('span/font/b')[0].text lastvalue.level = float(text.split('\n')[2].split('hPa')[0]) if i == 3: text = paraph.xpath('span/font[@size="4"]/b')[0].text lastvalue.level = float(text.split('\n')[2].split(u'%')[0] .split(':')[1]) if i == 4: text = paraph.xpath('b/font[@size="4"]/span')[0].text lastvalue.level = float(text.split('\n')[0]) if i == 5: text = paraph.xpath('b/font/span')[0].text lastvalue.level = float(text.split('\n')[1]) sensor.lastvalue = lastvalue sensors.append(sensor) return sensors
def _get_coef_value(self, AM=True, jour=0): if AM: time = DateTime(CleanText('//tr[@id="MareeJours_%s"]/td[1]/b[1]' % jour))(self) value = CleanText('//tr[@id="MareeJours_%s"]/td[3]/b[1]' % jour)(self) else: time, value = None, None if len(XPath('//tr[@id="MareeJours_%s"]/td[1]/b' % jour)(self)) > 1: time = DateTime(CleanText('//tr[@id="MareeJours_%s"]/td[1]/b[2]' % jour))(self) value = CleanText('//tr[@id="MareeJours_%s"]/td[3]/b[2]' % jour)(self) if time and value: measure = GaugeMeasure() measure.level = float(value) measure.date = time + timedelta(days=jour) return measure
def add_sensor(self, sensors, name, unit, value, forecast, alarm, date): sensor = GaugeSensor("%s-%s" % (self.obj.id, name.lower())) sensor.name = name sensor.unit = unit sensor.forecast = forecast lastvalue = GaugeMeasure() lastvalue.alarm = alarm try: lastvalue.level = float(value) except ValueError: lastvalue.level = NotAvailable lastvalue.date = date sensor.lastvalue = lastvalue sensor.history = NotLoaded sensor.gaugeid = self.obj.id sensors.append(sensor)
def _get_high_tide_value(self, AM=True, jour=0): if AM: time = DateTime(CleanText('//tr[@id="MareeJours_%s"]/td[1]/b[1]' % jour))(self) value = CleanDecimal('//tr[@id="MareeJours_0"]/td[2]/b[1]', replace_dots=True)(self) else: time, value = None, None if len(XPath('//tr[@id="MareeJours_%s"]/td[1]/b' % jour)(self)) > 1: time = DateTime(CleanText('//tr[@id="MareeJours_%s"]/td[1]/b[2]' % jour), default=None)(self) value = CleanDecimal('//tr[@id="MareeJours_0"]/td[2]/b[2]', replace_dots=True, default=None)(self) if time and value: measure = GaugeMeasure() measure.level = float(value) measure.date = time + timedelta(days=jour) return measure
def iter_history(self, sensor): table = self.document.getroot().cssselect('table[width="215"]') lines = table[0].cssselect("tr") lines.pop(0) # remove header lines.pop(0) # remove first value (already in lastvalue) for line in lines: history = GaugeMeasure() leveldate = date(*reversed([int(x) for x in line[0].text_content().split(' ')[0].split(".")])) leveltime = time(*[int(x) for x in line[0].text_content().split(' ')[1].split(":")]) history.date = datetime.combine(leveldate, leveltime) if sensor.name == u"Level": try: history.level = float(line[1].text_content()) except: history.level = NotAvailable elif sensor.name == u"Flow": try: history.level = float(line[2].text_content()) except: history.level = NotAvailable # TODO: history.alarm yield history
def _create_status_sensor(self, value, gauge_id, last_update, adresse): levelstatus = GaugeSensor(gauge_id + '-status') levelstatus.name = u'Status' levelstatus.address = u'%s' % adresse lastvalue = GaugeMeasure() status = float(value) if status == 0: status = 1 else: status = -1 if lastvalue.level < 1: lastvalue.alarm = u'Not available station' lastvalue.level = float(status) lastvalue.date = last_update levelstatus.lastvalue = lastvalue levelstatus.history = NotLoaded levelstatus.gaugeid = gauge_id return levelstatus
def _get_low_tide_value(self, AM=True, jour=0): slow_tide_pos = 1 if self._is_low_tide_first(jour) else 2 m = re.findall( '(\d{2}h\d{2})', CleanText('//tr[@id="MareeJours_%s"]/td[1]' % jour)(self)) re_time = '(\d{2}h\d{2}).*(\d{2}h\d{2}).*(\d{2}h\d{2})' re_value = '(.*)m(.*)m(.*)m' if len(m) > 3: re_time = '(\d{2}h\d{2}).*(\d{2}h\d{2}).*(\d{2}h\d{2}).*(\d{2}h\d{2})' re_value = '(.*)m(.*)m(.*)m(.*)m' if AM: time = DateTime( Regexp(CleanText('//tr[@id="MareeJours_%s"]/td[1]' % jour), re_time, '\\%s' % slow_tide_pos))(self) value = CleanDecimal(Regexp( CleanText('//tr[@id="MareeJours_%s"]/td[2]' % jour), re_value, '\\%s' % slow_tide_pos), replace_dots=True, default=None)(self) else: slow_tide_pos += 2 time, value = None, None if len(m) > slow_tide_pos - 1: time = DateTime( Regexp( CleanText('//tr[@id="MareeJours_%s"]/td[1]' % jour), re_time, '\\%s' % slow_tide_pos))(self) value = CleanDecimal(Regexp( CleanText('//tr[@id="MareeJours_%s"]/td[2]' % jour), re_value, '\\%s' % slow_tide_pos), replace_dots=True, default=None)(self) if time and value: measure = GaugeMeasure() measure.level = float(value) measure.date = time + timedelta(days=jour) return measure
def _get_low_tide_value(self, AM=True, jour=0): slow_tide_pos = 1 if self._is_low_tide_first(jour) else 2 m = re.findall('(\d{2}h\d{2})', CleanText('//tr[@id="MareeJours_%s"]/td[1]' % jour)(self)) re_time = '(\d{2}h\d{2}).*(\d{2}h\d{2}).*(\d{2}h\d{2})' re_value = '(.*)m(.*)m(.*)m' if len(m) > 3: re_time = '(\d{2}h\d{2}).*(\d{2}h\d{2}).*(\d{2}h\d{2}).*(\d{2}h\d{2})' re_value = '(.*)m(.*)m(.*)m(.*)m' if AM: time = DateTime(Regexp(CleanText('//tr[@id="MareeJours_%s"]/td[1]' % jour), re_time, '\\%s' % slow_tide_pos))(self) value = CleanDecimal(Regexp(CleanText('//tr[@id="MareeJours_%s"]/td[2]' % jour), re_value, '\\%s' % slow_tide_pos), replace_dots=True, default=None)(self) else: slow_tide_pos += 2 time, value = None, None if len(m) > slow_tide_pos - 1: time = DateTime(Regexp(CleanText('//tr[@id="MareeJours_%s"]/td[1]' % jour), re_time, '\\%s' % slow_tide_pos))(self) value = CleanDecimal(Regexp(CleanText('//tr[@id="MareeJours_%s"]/td[2]' % jour), re_value, '\\%s' % slow_tide_pos), replace_dots=True, default=None)(self) if time and value: measure = GaugeMeasure() measure.level = float(value) measure.date = time + timedelta(days=jour) return measure
def _get_coef_value(self, AM=True, jour=0): if AM: time = DateTime( CleanText('//tr[@id="MareeJours_%s"]/td[1]/b[1]' % jour))(self) value = CleanText('//tr[@id="MareeJours_%s"]/td[3]/b[1]' % jour)(self) else: time, value = None, None if len( XPath('//tr[@id="MareeJours_%s"]/td[1]/b' % jour)(self)) > 1: time = DateTime( CleanText('//tr[@id="MareeJours_%s"]/td[1]/b[2]' % jour))(self) value = CleanText('//tr[@id="MareeJours_%s"]/td[3]/b[2]' % jour)(self) if time and value: measure = GaugeMeasure() measure.level = float(value) measure.date = time + timedelta(days=jour) return measure
def obj_lastvalue(self): lastvalue = GaugeMeasure() lastvalue.level = Split(CleanText('.'), 0)(self) lastvalue.alarm = NotAvailable return lastvalue
def get_rivers_list(self): for pegel in self.document.getroot().xpath(".//a[@onmouseout='pegelaus()']"): data = pegel.attrib['onmouseover'].strip('pegelein(').strip(')').replace(",'", ",").split("',") gauge = Gauge(int(data[7])) gauge.name = unicode(data[0].strip("'")) gauge.city = gauge.name.split(' ')[0] # TODO: real regexp to remove the number gauge.object = unicode(data[1]) sensors = [] try: lastdate = date(*reversed([int(x) for x in data[2].split(' ')[0].split(".")])) lasttime = time(*[int(x) for x in data[2].split(' ')[1].split(":")]) lastdate = datetime.combine(lastdate, lasttime) except: lastdate = NotAvailable bildforecast = data[5] if bildforecast == "pf_gerade.png": forecast = u"stable" elif bildforecast == "pf_unten.png": forecast = u"Go down" elif bildforecast == "pf_oben.png": forecast = u"Go up" else: forecast = NotAvailable try: level = float(data[3]) levelsensor = GaugeSensor(gauge.id + "-level") levelsensor.name = u"Level" # TODO levelsensor.unit = levelsensor.forecast = forecast lastvalue = GaugeMeasure() lastvalue.level = level lastvalue.date = lastdate # TODO lastvalue.alarm = levelsensor.lastvalue = lastvalue levelsensor.history = NotLoaded levelsensor.gaugeid = gauge.id sensors.append(levelsensor) except: pass try: flow = float(data[4]) flowsensor = GaugeSensor(gauge.id + "-flow") flowsensor.name = u"Flow" # TODO flowsensor.unit = flowsensor.forecast = forecast lastvalue = GaugeMeasure() lastvalue.level = flow lastvalue.date = lastdate # TODO lastvalue.alarm = flowsensor.lastvalue = lastvalue flowsensor.history = NotLoaded flowsensor.gaugeid = gauge.id sensors.append(flowsensor) except: pass gauge.sensors = sensors yield gauge
def get_rivers_list(self): for pegel in self.document.getroot().xpath(".//a[@onmouseout='pegelaus()']"): div = pegel.getparent() img = div.find('.//img').attrib['src'].split('/')[1] data = pegel.attrib['onmouseover'] \ .strip('pegelein(').strip(')').replace(",'", ",").split("',") gauge = Gauge(int(data[7])) gauge.name = unicode(data[0].strip("'")) gauge.city = gauge.name.split(' ')[0] gauge.object = unicode(data[1]) sensors = [] try: lastdate = date(*reversed([int(x) for x in data[2].split(' ')[0].split(".")])) lasttime = time(*[int(x) for x in data[2].split(' ')[1].split(":")]) lastdate = datetime.combine(lastdate, lasttime) except: lastdate = NotAvailable bildforecast = data[5] if bildforecast == "pf_gerade.png": forecast = u"stable" elif bildforecast == "pf_unten.png": forecast = u"Go down" elif bildforecast == "pf_oben.png": forecast = u"Go up" else: forecast = NotAvailable try: level = float(data[3]) except: level = NotAvailable levelsensor = GaugeSensor(gauge.id + "-level") levelsensor.name = u"Level" levelsensor.unit = u"cm" levelsensor.forecast = forecast lastvalue = GaugeMeasure() lastvalue.level = level lastvalue.date = lastdate try: lastvalue.alarm = self.alarmlevel[img] except KeyError: lastvalue.alarm = u"" levelsensor.lastvalue = lastvalue levelsensor.history = NotLoaded levelsensor.gaugeid = gauge.id sensors.append(levelsensor) try: flow = float(data[4]) except: flow = NotAvailable flowsensor = GaugeSensor(gauge.id + "-flow") flowsensor.name = u"Flow" flowsensor.unit = u"m3/s" flowsensor.forecast = forecast lastvalue = GaugeMeasure() lastvalue.level = flow lastvalue.date = lastdate try: lastvalue.alarm = self.alarmlevel[img] except KeyError: lastvalue.alarm = u"" flowsensor.lastvalue = lastvalue flowsensor.history = NotLoaded flowsensor.gaugeid = gauge.id sensors.append(flowsensor) gauge.sensors = sensors yield gauge