Skip to content

Instantly share code, notes, and snippets.

@hudvin

hudvin/demo.py

Last active Jan 14, 2020
Embed
What would you like to do?
khumbu api
import imghdr
import io
import json
import shutil
import requests
valid_credentials = {
"email": "your@username.com",
"password": "your_password",
}
ROOT_URL = "https://api-facade.khumbu.im"
JWT_CREATE_URL = ROOT_URL + '/auth/jwt/create'
JWT_VERIFY_URL = ROOT_URL + '/auth/jwt/verify'
JWT_REFRESH_URL = ROOT_URL + '/auth/jwt/refresh'
AUTH_USERS_URL = ROOT_URL + '/auth/users/'
def get_tokens():
resp = requests.post(url=JWT_CREATE_URL, json=valid_credentials)
return resp.json()
def get_auth_headers():
return {"Authorization": "Bearer %s" % get_tokens()["access"]}
# max size - 5mb
UPLOADER_URL_ID = ROOT_URL + '/image_uploader/%s'
def send_one_file(collection_id, filepath):
files = {'file_1': open(filepath, 'rb')}
url = UPLOADER_URL_ID % collection_id
resp = requests.post(url, files=files, headers=get_auth_headers())
return resp
# print(send_one_file("5e038e520b63fbea98d4407d", "/home/kontiki/Downloads/mqdefault.jpg"))
########## search =======================
SEARCH_URL = ROOT_URL + "/search?collection_id=%s&page=%s&per_page=%s&query=%s"
IMAGE_URL = ROOT_URL + "/get_image?imageinfo_id=%s&type=%s"
def search(collection_id, query, page, per_page):
url = SEARCH_URL % (collection_id, page, per_page, json.dumps(query))
resp = requests.get(url)
return resp.json()
def get_image(imageinfo_id, image_size):
image_url = IMAGE_URL % (imageinfo_id, image_size)
print("getting image %s" % image_url)
resp = requests.get(image_url)
image_obj = io.BytesIO(resp.content)
ext = imghdr.what(image_obj)
image_obj.seek(0)
with open(imageinfo_id + "." + ext, 'wb') as out_file:
shutil.copyfileobj(image_obj, out_file)
search_results = search("5dff72e66483e25b40e0222e", {"text": "tree"}, 1, 10)
for idx, result in enumerate(search_results):
iamgeinfo_id = result["id"]
print(idx, iamgeinfo_id)
# orignal. preview, small
get_image(iamgeinfo_id, "small")
all_filters = {"text": "cat",
"faces.count": [0, 25],
"nsfw_score": [0, 1],
"height": [50, 10000],
"width": [50, 10000],
"aperture": [1, 32],
"exposure": [0.00025, 20],
"focal_length": [5, 600],
"iso": [25, 12800],
"date_year": [2000, 2020],
"date_month": [1, 12],
"date_weekday": [1, 7],
"date_hour": [0, 23]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.