aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--bot.py15
-rw-r--r--foursquare/poi.py (renamed from foursquare/query_poi.py)25
3 files changed, 34 insertions, 8 deletions
diff --git a/.gitignore b/.gitignore
index eb701d6..f52378c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,4 @@
1foursquare/location_example.json 1foursquare/*_example.json
2config.ini 2config.ini
3.idea/ 3.idea/
4fsq_poi.db 4fsq_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
26from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters 26from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters
27 27
28from config import BOT_TOKEN 28from config import BOT_TOKEN
29from foursquare.query_poi import query_poi 29from foursquare.poi import query_poi, get_poi_top_photo
30from dbstore.dbm_store import get_loc 30from dbstore.dbm_store import get_loc
31from toot import mastodon_client 31from toot import mastodon_client
32import urllib.request
32 33
33# Enable logging 34# Enable logging
34logging.basicConfig( 35logging.basicConfig(
@@ -66,12 +67,20 @@ async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
66 67
67 await query.answer() 68 await query.answer()
68 poi = get_loc(query.data) 69 poi = get_loc(query.data)
70 photo_url = get_poi_top_photo(query.data)
71 media_id = None
72 if photo_url is not None:
73 with urllib.request.urlopen(photo_url) as response:
74 data = response.read()
75 media = mastodon_client.media_post(data, mime_type="image/jpeg")
76 media_id = [media["id"]]
69 77
70 status = mastodon_client.status_post( 78 status = mastodon_client.status_post(
71 f"I'm at {poi['name']} in {poi['locality']}, {poi['region']}, {poi['osm_url']}", 79 f"I'm at {poi['name']} in {poi['locality']}, {poi['region']}, {poi['osm_url']}",
72 visibility="private") 80 visibility="private",
81 media_ids=media_id)
73 82
74 await query.edit_message_text(text=f"Selected place: {poi['name']}\nPosted to Mastodon: {status['url']}", 83 await query.edit_message_text(text=f"Selected place: {poi['name']}, `{query.data}`\nPosted to Mastodon: {status['url']}",
75 parse_mode=telegram.constants.ParseMode.MARKDOWN) 84 parse_mode=telegram.constants.ParseMode.MARKDOWN)
76 85
77 86
diff --git a/foursquare/query_poi.py b/foursquare/poi.py
index e2d5625..722dbb7 100644
--- a/foursquare/query_poi.py
+++ b/foursquare/poi.py
@@ -6,17 +6,18 @@ from config import FSQ_API_KEY
6from dbstore.dbm_store import store_loc 6from dbstore.dbm_store import store_loc
7 7
8POI_API_ENDPOINT = "https://api.foursquare.com/v3/places/nearby?ll={}%2C{}" 8POI_API_ENDPOINT = "https://api.foursquare.com/v3/places/nearby?ll={}%2C{}"
9POI_PHOTO_ENDPOINT = "https://api.foursquare.com/v3/places/{}/photos?sort=POPULAR&limit=10"
9OSM_ENDPOINT = "https://www.openstreetmap.org/?mlat={}&mlon={}&zoom=15&layers=M" 10OSM_ENDPOINT = "https://www.openstreetmap.org/?mlat={}&mlon={}&zoom=15&layers=M"
11headers = {
12 "accept": "application/json",
13 "Authorization": FSQ_API_KEY
14}
10 15
11 16
12def query_poi(latitude, longitude): 17def query_poi(latitude, longitude):
13 locations = list() 18 locations = list()
14 19
15 url = POI_API_ENDPOINT.format(latitude, longitude) 20 url = POI_API_ENDPOINT.format(latitude, longitude)
16 headers = {
17 "accept": "application/json",
18 "Authorization": FSQ_API_KEY
19 }
20 21
21 response = requests.get(url, headers=headers) 22 response = requests.get(url, headers=headers)
22 23
@@ -34,3 +35,19 @@ def query_poi(latitude, longitude):
34 store_loc(loc) 35 store_loc(loc)
35 36
36 return locations 37 return locations
38
39
40def get_poi_top_photo(fsq_id):
41 url = POI_PHOTO_ENDPOINT.format(fsq_id)
42 response = requests.get(url, headers=headers)
43
44 poi_photo_urls = []
45 for poi in json.loads(response.text):
46 prefix = poi["prefix"]
47 suffix = poi["suffix"]
48 poi_photo_urls.append(prefix + "original" + suffix)
49
50 if len(poi_photo_urls) == 0:
51 return None
52
53 return poi_photo_urls[0]
Powered by cgit v1.2.3 (git 2.41.0)