aboutsummaryrefslogtreecommitdiff
blob: 722dbb75f53201626d6c2e9a7c717c5772a3f824 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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]
Powered by cgit v1.2.3 (git 2.41.0)