From fda33866814e95830466787ae9cea2e006386e85 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Mon, 20 Feb 2023 14:45:17 -0800 Subject: attach top popular photo to mastodon media post --- foursquare/poi.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 foursquare/poi.py (limited to 'foursquare/poi.py') diff --git a/foursquare/poi.py b/foursquare/poi.py new file mode 100644 index 0000000..722dbb7 --- /dev/null +++ b/foursquare/poi.py @@ -0,0 +1,53 @@ +import json + +import requests + +from config import FSQ_API_KEY +from dbstore.dbm_store import store_loc + +POI_API_ENDPOINT = "https://api.foursquare.com/v3/places/nearby?ll={}%2C{}" +POI_PHOTO_ENDPOINT = "https://api.foursquare.com/v3/places/{}/photos?sort=POPULAR&limit=10" +OSM_ENDPOINT = "https://www.openstreetmap.org/?mlat={}&mlon={}&zoom=15&layers=M" +headers = { + "accept": "application/json", + "Authorization": FSQ_API_KEY +} + + +def query_poi(latitude, longitude): + locations = list() + + url = POI_API_ENDPOINT.format(latitude, longitude) + + response = requests.get(url, headers=headers) + + for poi in json.loads(response.text)["results"]: + loc = { + "fsq_id": poi["fsq_id"], + "name": poi["name"], + "locality": poi["location"]["locality"], + "region": poi["location"]["region"], + "latitude": poi["geocodes"]["main"]["latitude"], + "longitude": poi["geocodes"]["main"]["longitude"], + "osm_url": OSM_ENDPOINT.format(poi["geocodes"]["main"]["latitude"], poi["geocodes"]["main"]["longitude"]) + } + locations.append(loc) + store_loc(loc) + + return locations + + +def get_poi_top_photo(fsq_id): + url = POI_PHOTO_ENDPOINT.format(fsq_id) + response = requests.get(url, headers=headers) + + poi_photo_urls = [] + for poi in json.loads(response.text): + prefix = poi["prefix"] + suffix = poi["suffix"] + poi_photo_urls.append(prefix + "original" + suffix) + + if len(poi_photo_urls) == 0: + return None + + return poi_photo_urls[0] -- cgit v1.2.3