def parseOdvXML(self, xmldata): if xmldata.itdOdvPlace['state'] == 'empty': return classes.EmptyTripPoint() if 'list' in [xmldata.itdOdvPlace['state'].encode('utf-8'), xmldata.itdOdvName['state'].encode('utf-8')]: result = classes.TripPointCompatibleUnclear() result.place=[elem.getText().encode('utf-8').strip() for elem in xmldata.itdOdvPlace('odvPlaceElem')] result.name=[elem.getText().encode('utf-8').strip() for elem in xmldata.itdOdvName('odvNameElem')] else: odvtype = xmldata['type'] if odvtype == 'any': odvtype = xmldata.itdOdvName.odvNameElem['anyType'] if odvtype == 'stop': result = classes.Station() result.place=xmldata.itdOdvPlace.odvPlaceElem.getText().encode('utf-8').strip() result.name=xmldata.itdOdvName.odvNameElem.getText().encode('utf-8').strip() result.stopid=xmldata.itdOdvName.odvNameElem['stopID'] if xmldata.itdOdvName.odvNameElem.has_attr('stopID') else xmldata.itdOdvName.odvNameElem['id'] if xmldata.itdOdvName.odvNameElem.has_attr('x'): result.coords = tools.coords(xmldata.itdOdvName.odvNameElem['x'], xmldata.itdOdvName.odvNameElem['y']) elif odvtype == 'address': result = classes.Address() result.place=xmldata.itdOdvPlace.odvPlaceElem.getText().encode('utf-8').strip() result.name=xmldata.itdOdvName.odvNameElem.getText().encode('utf-8').strip() result.streetname=xmldata.itdOdvName.odvNameElem['streetName'] result.housenumber=xmldata.itdOdvName.odvNameElem['houseNumber'] if xmldata.itdOdvName.odvNameElem.has_attr('x'): result.coords = tools.coords(xmldata.itdOdvName.odvNameElem['x'], xmldata.itdOdvName.odvNameElem['y']) else: raise exceptions.NotImplementedException('unknown type of odv: "%s"' % xmldata['type']) return result
def parseStopJSON(self, jsondata, result): result.name = jsondata['nameWO'].encode('utf8').strip() result.place = jsondata['name'].encode('utf8').strip() result.place = ' '.join(result.place.split(' ')[0:0-len(result.name.split(' '))]) result.stopid = jsondata['ref']['id'] result.platformname = jsondata['platformName'].encode('utf8').replace('Gleis', '').replace('Bstg.', '').strip() if 'arrDateTime' in jsondata['ref']: result.arrival = datetime.datetime.strptime(jsondata['ref']['arrDateTime'].replace('24:', '00:'), '%Y%m%d %H:%M') if 'arrDelay' in jsondata['ref'] and int(jsondata['ref']['arrDelay']) >= 0: result.arrival_delay = int(jsondata['ref']['arrDelay']) result.arrival_live = result.arrival+datetime.timedelta(minutes=result.arrival_delay) else: result.arrival_delay = None result.arrival_live = None if 'depDateTime' in jsondata['ref']: result.departure = datetime.datetime.strptime(jsondata['ref']['depDateTime'].replace('24:', '00:'), '%Y%m%d %H:%M') if 'depDelay' in jsondata['ref'] and int(jsondata['ref']['depDelay']) >= 0: result.departure_delay = int(jsondata['ref']['depDelay']) result.departure_live = result.departure+datetime.timedelta(minutes=result.departure_delay) else: result.departure_delay = None result.departure_live = None if 'arrDateTime' not in jsondata['ref'] and 'depDateTime' not in jsondata['ref']: result.nostop = True c = self.parseJSONcoords(jsondata['ref']['coords']) if c is not None: result.coords = tools.coords(c[0], c[1]) return result
def parsePointXML(self, xmldata, result): result.place = xmldata['locality'].encode('utf-8').strip() result.name = xmldata['nameWO'].encode('utf-8').strip() if xmldata['locality'] != '' or xmldata['nameWO'] != '' else xmldata['name'].encode('utf-8').strip() result.stopid = xmldata['stopID'] result.platformname = xmldata['platformName'].encode('utf-8').replace('Gleis', '').replace('Bstg.', '').strip() if xmldata('x'): result.coords = tools.coords(xmldata['x'], xmldata['y']) times = [] if xmldata('itdDateTimeTarget'): times.append(self.parseDateTimeXML(xmldata.itdDateTimeTarget)) if xmldata('itdDateTime'): times.append(self.parseDateTimeXML(xmldata.itdDateTime)) if len(times) > 0: setattr(result, '%s' % xmldata['usage'], times[0]) if len(times) == 2: setattr(result, '%s_live' % xmldata['usage'], times[1]) setattr(result, '%s_delay' % xmldata['usage'], int((times[1]-times[0]).total_seconds())/60) return result
def parsePointJSON(self, jsondata, result): if result.place is None or result.place == '': result.place = jsondata['place'].encode('utf8').strip() if result.name is None or result.name == '': result.name = (jsondata['name'] if not jsondata['name'].startswith(jsondata['place']) else jsondata['name'][len(jsondata['place']):]).encode('utf8').strip() result.stopid = jsondata['ref']['id'] if result.platformname is None: result.platformname = jsondata['ref']['platform'].encode('utf8').strip() c = self.parseJSONcoords(jsondata['ref']['coords']) if c is not None: result.coords = tools.coords(c[0], c[1]) if jsondata['dateTime']['time'] != '24:00': datetimeObject = datetime.datetime.strptime('%s %s' % (jsondata['dateTime']['date'], jsondata['dateTime']['time']), '%d.%m.%Y %H:%M') else: datetimeObject = datetime.datetime.strptime('%s' % (jsondata['dateTime']['date']), '%d.%m.%Y') + datetime.timedelta(1) setattr(result, jsondata['usage'], datetimeObject) return result
def parseTripJSON(self, rawdata, result): jsondata = json.loads(rawdata) # Metadaten for param in jsondata['parameters']: if param['name'] in ('sessionID', 'serverID'): setattr(result, param['name'].lower(), param['value']) setattr(result, 'json%s' % param['name'].lower(), param['value']) # Stationen # Gibts nicht! Naja… # Routen parsen routesJSON = jsondata['trips'] i = 0 for routeJSONfoo in routesJSON: routeJSON = routeJSONfoo['trip'] newroute = result.addroute(i) newroute.duration = self.parseHourMinutes(routeJSON['duration']) partsJSON = routeJSON['legs'] j = 0 for partJSON in partsJSON: newpart = newroute.addpart(j) if newpart.origin is None: newpart.origin = classes.Point() if newpart.destination is None: newpart.destination = classes.Point() newpart.origin = self.parsePointJSON(partJSON['points'][0], newpart.origin) newpart.destination = self.parsePointJSON(partJSON['points'][1], newpart.destination) newpart.mot = self.parseMotJSON(partJSON['mode'], classes.Mot()) newpart.via = [] newpart.via_all = [] if 'path' in partJSON: newpart.path = [tools.coords(*c.split(',')) for c in partJSON['path'].strip().split(' ')] stopsJSON = partJSON['stopSeq'] if 'stopSeq' in partJSON else [] k = 0 for stopJSON in stopsJSON: newstop = self.parseStopJSON(stopJSON, classes.Point()) newpart.via.append(newstop) newstop.k = k k+=1 # So, und jetzt müssen wir dinge kompliziert machen, um bugs der EFA zu Workarounden # Zunächst mal original origin und destination durch die detaillierteren versionen aus der Stopliste ersetzen for k in xrange(len(newpart.via)): via = newpart.via[k] if via.stopid == newpart.origin.stopid and via.get_maybelive_time('departure') == newpart.origin.departure and via.coords == newpart.origin.coords: newpart.origin = via newpart.via.pop(k) break for k in xrange(len(newpart.via)): via = newpart.via[len(newpart.via)-1-k] if via.stopid == newpart.destination.stopid and via.get_maybelive_time('arrival') == newpart.destination.arrival and via.coords == newpart.destination.coords: newpart.destination = via newpart.via.pop(len(newpart.via)-1-k) break # Gefilterte liste ohne durchfahren erstellen newpart.via_all = newpart.via newpart.via = [] for via in newpart.via_all: if not via.nostop: newpart.via.append(via) newpart.duration = int((newpart.destination.arrival - newpart.origin.departure).total_seconds())/60 if 'turnInst' in partJSON: newpart.distance = sum([int(inst['dis']) for inst in partJSON['turnInst']]) j+=1 i+=1 if len(result.routes) > 0: result.origin = result.routes[0].parts[0].origin result.destination = result.routes[0].parts[-1].destination return result