From 5402e3a2e9df24a883e2e20dfae6c92f11b8d499 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Wed, 22 Feb 2023 23:57:11 -0800 Subject: bot: change create_poi to create_or_update_poi --- callback.py | 1 + dbstore/peewee_store.py | 7 +++---- foursquare/poi.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/callback.py b/callback.py index aca8a90..23b814b 100644 --- a/callback.py +++ b/callback.py @@ -184,6 +184,7 @@ async def _process_location_selection(context: ContextTypes.DEFAULT_TYPE) -> int poi_name = context.user_data.get("poi_name") if context.user_data.get("fsq_id") is not None: poi = get_poi_by_fsq_id(context.user_data.get("fsq_id")) + poi_name = poi["name"] content = generate_toot_text(poi["name"], poi["locality"], poi["region"], poi["latitude"], poi["longitude"]) else: content = generate_toot_text(poi_name, "", "", context.user_data.get("latitude"), context.user_data.get("longitude")) diff --git a/dbstore/peewee_store.py b/dbstore/peewee_store.py index 00897d7..89e4b4b 100644 --- a/dbstore/peewee_store.py +++ b/dbstore/peewee_store.py @@ -52,14 +52,13 @@ def get_poi_by_fsq_id(fsq_id) -> dict: return {} -def create_poi(poi: dict): +def create_or_update_poi(poi: dict) -> int: with db.connection_context(): - poi = Location.create( + return Location.insert( fsq_id=poi["fsq_id"], name=poi["name"], locality=poi["locality"], region=poi["region"], latitude=poi["latitude"], longitude=poi["longitude"], - ) - poi.save() + ).on_conflict_replace().execute() diff --git a/foursquare/poi.py b/foursquare/poi.py index 4ff11fb..ea3509f 100644 --- a/foursquare/poi.py +++ b/foursquare/poi.py @@ -3,7 +3,7 @@ import json import requests from config import FSQ_API_KEY -from dbstore.peewee_store import create_poi +from dbstore.peewee_store import create_or_update_poi POI_API_ENDPOINT = "https://api.foursquare.com/v3/places/nearby?ll={}%2C{}&limit=10" POI_SEARCH_API_ENDPOINT = "https://api.foursquare.com/v3/places/search?query={}&ll={}%2C{}&radius=2000&limit=10" @@ -32,7 +32,7 @@ def query_poi(search, latitude, longitude): "osm_url": OSM_ENDPOINT.format(poi["geocodes"]["main"]["latitude"], poi["geocodes"]["main"]["longitude"]) } locations.append(loc) - create_poi(loc) + create_or_update_poi(loc) return locations -- cgit v1.2.3