Last active
November 18, 2016 10:52
-
-
Save jboynyc/1195f5e753a3063c7dfc8e97e3ad8f01 to your computer and use it in GitHub Desktop.
simple Mapillary API wrapper (for Python 3.4+)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
import requests | |
from yaml import load as yaml_load | |
from functools import partialmethod | |
from urllib.parse import urlencode, quote | |
class Mapillary(object): | |
def __init__(self, token='token.yaml'): | |
self._base_url = 'https://a.mapillary.com/v2' | |
self._img_url = 'https://d1cuyjsrcm0gby.cloudfront.net/{}/thumb-{}.jpg' | |
try: | |
self._client_id = self._load_token(token)['client_id'] | |
except FileNotFoundError: | |
self._client_id = token | |
@property | |
def status(self): | |
path = '/status/api' | |
return requests.get(self._base_url+path).ok | |
@property | |
def authorized(self): | |
path = '/me' | |
r = requests.get(self._base_url+path) | |
if r.ok: | |
return True | |
elif r.status_code == 401: | |
return False | |
else: | |
raise ConnectionError(r.status_code) | |
def image_url(self, image_key, thumbnail_size=640): | |
assert thumbnail_size in (320, 640, 1024, 2048) | |
return self._img_url.format(image_key, thumbnail_size) | |
def _load_token(self, token_file): | |
with open(token_file, 'r') as f: | |
return yaml_load(f) | |
def _endpoint(self, path, **kwargs): | |
params = dict(client_id=self._client_id) | |
params.update(kwargs) | |
r = requests.get('?'.join([self._base_url+path, urlencode(params)])) | |
if r.ok: | |
return r.json() | |
else: | |
raise ConnectionError(r.status_code) | |
def _retrieve_by_key(self, key, path, **kwargs): | |
params = dict(client_id=self._client_id) | |
params.update(kwargs) | |
formatted_path = path.format(quote(key)) | |
r = requests.get('?'.join([self._base_url+formatted_path, urlencode(params)])) | |
if r.ok: | |
return r.json() | |
else: | |
raise ConnectionError(r.status_code) | |
random_image = partialmethod(_endpoint, path='/search/im/randomselected') | |
random_image_geojson = partialmethod(_endpoint, path='/search/im/geojson/randomimage') | |
random_image_with_object = partialmethod(_endpoint, path='/search/im/or/random') | |
box_search = partialmethod(_endpoint, path='/search/im') | |
point_search = partialmethod(_endpoint, path='/search/im/close') | |
comment = partialmethod(_retrieve_by_key, path='/cm/{}') | |
image = partialmethod(_retrieve_by_key, path='/im/{}') | |
sequence = partialmethod(_retrieve_by_key, path='/s/{}') | |
user = partialmethod(_retrieve_by_key, path='/u/{}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
client_id: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' | |
client_secret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment