def home(): # First, we'll perform the select of the latest checkin try: cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) cur.execute('SELECT latitude, longitude, display_name, timestamp FROM "checkins" ORDER BY id DESC') row = cur.fetchone() cur.close() attrs = ['years', 'months', 'days', 'hours', 'minutes', 'seconds'] human_readable = lambda delta: ['%d %s' % (getattr(delta, attr), getattr(delta, attr) > 1 and attr or attr[:-1]) for attr in attrs if getattr(delta, attr)] timeSince = human_readable(relativedelta(datetime.now(), datetime.fromtimestamp(row['timestamp']))) # And return this data, and all lookups to the script if request.query.get("accuracy", '') != '' and request.query.get('token', '') == os.getenv('APP_TOKEN', 'testtoken'): locationData = Nominatim() display_name = locationData.reverse(row['latitude'], row['longitude'], request.query.get("accuracy"))['display_name'] else: display_name = row['display_name'] return dict( name=display_name, timeSince="({} ago)".format(', '.join(timeSince)), time=datetime.fromtimestamp(row['timestamp']).strftime('%d/%m/%Y %H:%M:%S') ) except Exception as e: conn.rollback() pass
def home(): # First, we'll perform the select of the latest checkin try: cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) cur.execute('SELECT latitude, longitude, display_name, timestamp FROM "checkins" ORDER BY id DESC') row = cur.fetchone() cur.close() attrs = ['years', 'months', 'days', 'hours', 'minutes', 'seconds'] human_readable = lambda delta: ['%d %s' % (getattr(delta, attr), getattr(delta, attr) > 1 and attr or attr[:-1]) for attr in attrs if getattr(delta, attr)] timeSince = human_readable(relativedelta(datetime.now(), datetime.fromtimestamp(row['timestamp']))) # And return this data, and all lookups to the script if request.query.get("accuracy", '') != '' and request.query.get('token', '') == os.getenv('APP_TOKEN', 'testtoken'): locationData = Nominatim() display_name = locationData.reverse(row['latitude'], row['longitude'], request.query.get("accuracy"))['display_name'] if not os.getenv('W3W_TOKEN', "") == "": words = w3w.reverse(lat=row['latitude'], lng=row['longitude'])['words'] else: display_name = row['display_name'] words = '' return dict( name=display_name, timeSince="({} ago)".format(', '.join(timeSince)), threeWords=words, time=datetime.fromtimestamp(row['timestamp']).strftime('%d/%m/%Y %H:%M:%S') ) except Exception as e: print e.message conn.rollback() pass
def submit(lat, lon, deviceID=''): try: locationData = Nominatim() locationData = locationData.reverse(lat, lon, 12) timeData = int(time.time()) # Get the last check-in cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) cur.execute('SELECT latitude, longitude FROM "checkins" ORDER BY id DESC') row = cur.fetchone() cur.close() # If we have moved more than 100m, then we'll accept the check-in try: distance = LatLon(lat, lon).distance(LatLon(row['latitude'],row['longitude'])) if distance < 0.1: message = "Not updating as latitude and longitude have not been modified by more than 100m: {}, {}".format(lat, lon) log.info(message) return message except TypeError: pass # Insert the requisite check-in record cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) cur.execute( 'INSERT INTO "checkins" (latitude, longitude, display_name, timestamp) VALUES (%s, %s, %s, %s)', (lat, lon, locationData['display_name'], timeData) ) log.info("Updated location to: {}, {}".format(lat, lon)) conn.commit() cur.close() except ValueError: # Something was wrong with the latitude or logitude (probably invalid data) message = "You provided invalid data: {}, {}".format(lat, lon) log.info(message) return message return True