def get(self): db = Database(connect=1) message = self.request.get("message") session_id = self.request.get("id") if session_id and db.sessionExists(session_id): if not message and session_id: start_poll = time() # get timestamp of poll started new_messages = [] while 1: try: db = Database(connect=1) # reconnecting every time is necessary chat = db.deserialize(db.getData("chat", session_id)) for message in chat: if message: message_timestamp = float(message.split(":")[0]) if message_timestamp > start_poll: new_messages.append(message) if new_messages: self.response.out.write(new_messages) break else: sleep(2) except DeadlineExceededError: break elif message and db.isIDValid(session_id): current_chat = db.deserialize(db.getData("chat", session_id)) current_chat.append(str(time()) + ":" + quote_plus(message)) if len(current_chat >= 10): current_chat.pop() db.setData("chat", db.serialize(current_chat), session_id)
def get(self): def isIDValid(session_id): valid = False if session_id != None: if match("^[A-Za-z0-9_-]*$", str(session_id)) and len(session_id) == 40: valid = True else: self.response.out.write('id not valid') return valid if __name__ == "__main__": sys.stderr = sys.stdout db = Database() data = {"id": False, "creator": False, "admins": False, "users": False, "queue": False, "chat": False } sent_field = self.request.get("id") if db.isIDValid(sent_field): data["id"] = sent_field for field in data.keys(): sent_field = self.request.get(field) if sent_field and field != "id" and int(sent_field) == 1: data[field] = True if data["id"]: #query db for data if id is valid db.connect() for field in data.keys(): if data[field] and field != "id": query_data = db.getData(field, data["id"]) self.response.out.write(field + ":" + query_data) db.close()