def get_closest_cities(): lat, lon = float(request.args.get('lat', 0)), float(request.args.get('lon', 0)) if lat == 0 or lon == 0: raise RuntimeError("Invalid request") q = "SELECT * FROM worldcities WHERE (point(%(lon)s, %(lat)s) <@> POINT(longitude, latitude)) / 1.60934 < 100 ORDER BY (point(%(lon)s, %(lat)s) <@> POINT(longitude, latitude)) ASC LIMIT 5" cur = psql.cursor(cursor_factory=psycopg2.extras.RealDictCursor) cur.execute(q, {'lat': lat, 'lon': lon}) c = cur.fetchall() c = [dict(city, id=utils.city_id(city)) for city in c] return jsonify(dict(cities=c))
def send_push(city): messages = {"en": u"High chance on rainbows near {}"} if city['country'] == 'ru': messages["ru"] = u"Высокая вероятность на радугу в районе {}" for lang, message in messages.items(): if lang != 'en': name = city['name'] else: name = city['name_en'] pf = "-" + lang channels = [utils.city_id(city)+pf] #+ [utils.city_id(n)+pf for n in city['nearby']] logger.debug(u"Sending pushes for city: {} ({})".format(name, lang).encode('utf8')) _push(message.format(name), channels)