def updateBusInfo(stopcode, busnumber, pt_timestamp, eta_minutes, dest, requestid): """ Given information about a bus, insert a new businfo object in the datastore """ newInfo = BusInfo(stopcode=stopcode, busNumber=busnumber, pt_timestamp=pt_timestamp, eta_minutes=eta_minutes, dest=dest, requestid=requestid) newInfo.put() logging.debug("Added new bus to datastore: %s" % newInfo)
def getUpdateBus(request): """ Checks the database to see if there's information about a specific bus stop """ entries = BusInfo.all().filter("stopcode = ", request.stopcode).filter("last_modified > ", request.created_date) return entries
def parseCarrisMail(stopcode, mailbody): """ Given a mail body received in an email from carris, search in the html for bus information """ # Delete all previous information about this stopcode, # this one is fresher for b in BusInfo.all().filter("stopcode = ", stopcode): b.delete() res = [] soup = BeautifulSoup(mailbody) # If there's an error, just mark the stop code as invalid invalidcodep = soup.find('p', 'error-title') # Check if the reply is a "No buses found" notfoundb = soup.find('b', 'thINFO') if invalidcodep is not None: hasResults = BUSREQUEST_RETURNED_INVALID elif notfoundb is not None and notfoundb.contents[0] == "Não foram encontrados Resultados.": hasResults = BUSREQUEST_RETURNED_WO_RESULTS else: hasResults = BUSREQUEST_RETURNED_W_RESULTS # Update all requests for this stop that aren't older than REQUEST_TIMEOUT # Note that all requests will be updated, even if they already received an update. # This way, we'll provide freshier info. requests = BusRequest.all().\ filter("last_modified > ", datetime.now() - timedelta(seconds=REQUEST_TIMEOUT)).\ filter("stopcode = ", stopcode) for r in requests: r.status_code = hasResults r.put() # If we received results, write them to db if hasResults == BUSREQUEST_RETURNED_W_RESULTS: # Iterate over all the rows starting in the 2nd one (1:) for tag in soup.find('div',id='RESULT_LAYER').findAll('tr')[1:]: ths = tag.findAll('th') busnr = ths[0].contents[0].strip() dest = ths[1].contents[0].strip() pt_timestamp = ths[2].contents[0].strip() eta_minutes = int(ths[3].contents[0].strip().rstrip('m')) newbus = BusInfo(stopcode=stopcode, busNumber=busnr, pt_timestamp=pt_timestamp, eta_minutes=eta_minutes, dest=dest) newbus.put() res.append(newbus) return res