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 --- .gitignore | 2 +- bot.py | 15 +++++++++++--- foursquare/poi.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ foursquare/query_poi.py | 36 --------------------------------- 4 files changed, 66 insertions(+), 40 deletions(-) create mode 100644 foursquare/poi.py delete mode 100644 foursquare/query_poi.py diff --git a/.gitignore b/.gitignore index eb701d6..f52378c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -foursquare/location_example.json +foursquare/*_example.json config.ini .idea/ fsq_poi.db diff --git a/bot.py b/bot.py index 7679d7f..467c37e 100644 --- a/bot.py +++ b/bot.py @@ -26,9 +26,10 @@ from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters from config import BOT_TOKEN -from foursquare.query_poi import query_poi +from foursquare.poi import query_poi, get_poi_top_photo from dbstore.dbm_store import get_loc from toot import mastodon_client +import urllib.request # Enable logging logging.basicConfig( @@ -66,12 +67,20 @@ async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await query.answer() poi = get_loc(query.data) + photo_url = get_poi_top_photo(query.data) + media_id = None + if photo_url is not None: + with urllib.request.urlopen(photo_url) as response: + data = response.read() + media = mastodon_client.media_post(data, mime_type="image/jpeg") + media_id = [media["id"]] status = mastodon_client.status_post( f"I'm at {poi['name']} in {poi['locality']}, {poi['region']}, {poi['osm_url']}", - visibility="private") + visibility="private", + media_ids=media_id) - await query.edit_message_text(text=f"Selected place: {poi['name']}\nPosted to Mastodon: {status['url']}", + await query.edit_message_text(text=f"Selected place: {poi['name']}, `{query.data}`\nPosted to Mastodon: {status['url']}", parse_mode=telegram.constants.ParseMode.MARKDOWN) 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] diff --git a/foursquare/query_poi.py b/foursquare/query_poi.py deleted file mode 100644 index e2d5625..0000000 --- a/foursquare/query_poi.py +++ /dev/null @@ -1,36 +0,0 @@ -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{}" -OSM_ENDPOINT = "https://www.openstreetmap.org/?mlat={}&mlon={}&zoom=15&layers=M" - - -def query_poi(latitude, longitude): - locations = list() - - url = POI_API_ENDPOINT.format(latitude, longitude) - headers = { - "accept": "application/json", - "Authorization": FSQ_API_KEY - } - - 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 -- cgit v1.2.3