2016-09-20 22:18:10 +00:00
|
|
|
"""
|
2016-10-25 20:04:24 +00:00
|
|
|
An interface to node-OpenDroneMap's API
|
|
|
|
https://github.com/pierotofy/node-OpenDroneMap/blob/master/docs/index.adoc
|
2016-09-20 22:18:10 +00:00
|
|
|
"""
|
2016-10-25 16:19:14 +00:00
|
|
|
import requests
|
2016-10-25 20:04:24 +00:00
|
|
|
import mimetypes
|
|
|
|
import json
|
|
|
|
import os
|
2016-10-26 22:09:59 +00:00
|
|
|
from urlparse import urlunparse
|
2016-09-21 20:54:22 +00:00
|
|
|
|
2016-09-20 22:18:10 +00:00
|
|
|
class ApiClient:
|
|
|
|
def __init__(self, host, port):
|
2016-09-21 20:54:22 +00:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
|
2016-10-25 16:19:14 +00:00
|
|
|
def url(self, url):
|
2016-10-26 22:09:59 +00:00
|
|
|
netloc = self.host if self.port == 80 else "{}:{}".format(self.host, self.port)
|
|
|
|
|
|
|
|
# TODO: https support
|
|
|
|
return urlunparse(('http', netloc, url, '', '', ''))
|
2016-10-25 16:19:14 +00:00
|
|
|
|
2016-09-21 20:54:22 +00:00
|
|
|
def info(self):
|
2016-10-25 16:19:14 +00:00
|
|
|
return requests.get(self.url('/info')).json()
|
2016-09-21 22:07:19 +00:00
|
|
|
|
|
|
|
def options(self):
|
2016-10-25 16:19:14 +00:00
|
|
|
return requests.get(self.url('/options')).json()
|
2016-10-25 14:47:49 +00:00
|
|
|
|
2016-10-26 16:54:46 +00:00
|
|
|
def task_info(self, uuid):
|
|
|
|
return requests.get(self.url('/task/{}/info').format(uuid)).json()
|
|
|
|
|
2016-11-01 21:12:13 +00:00
|
|
|
def task_output(self, uuid, line = 0):
|
|
|
|
return requests.get(self.url('/task/{}/output?line={}').format(uuid, line)).json()
|
|
|
|
|
2016-11-02 22:32:24 +00:00
|
|
|
def task_cancel(self, uuid):
|
|
|
|
return requests.post(self.url('/task/cancel'), data={'uuid': uuid}).json()
|
|
|
|
|
2016-11-04 18:19:18 +00:00
|
|
|
def task_remove(self, uuid):
|
|
|
|
return requests.post(self.url('/task/remove'), data={'uuid': uuid}).json()
|
|
|
|
|
|
|
|
def task_restart(self, uuid):
|
|
|
|
return requests.post(self.url('/task/restart'), data={'uuid': uuid}).json()
|
2016-11-01 21:12:13 +00:00
|
|
|
|
2016-11-05 20:23:54 +00:00
|
|
|
def task_download(self, uuid, asset):
|
|
|
|
res = requests.get(self.url('/task/{}/download/{}').format(uuid, asset))
|
|
|
|
if "Content-Type" in res.headers and "application/json" in res.headers['Content-Type']:
|
|
|
|
return res.json()
|
|
|
|
else:
|
|
|
|
return res.content
|
|
|
|
|
2016-10-25 20:04:24 +00:00
|
|
|
def new_task(self, images, name=None, options=[]):
|
|
|
|
"""
|
|
|
|
Starts processing of a new task
|
|
|
|
:param images: list of path images
|
|
|
|
:param name: name of the task
|
|
|
|
:param options: options to be used for processing ([{'name': optionName, 'value': optionValue}, ...])
|
|
|
|
:return: UUID or error
|
|
|
|
"""
|
2016-10-26 20:56:32 +00:00
|
|
|
|
2016-10-25 20:04:24 +00:00
|
|
|
files = [('images',
|
|
|
|
(os.path.basename(image), open(image, 'rb'), (mimetypes.guess_type(image)[0] or "image/jpg"))
|
|
|
|
) for image in images]
|
|
|
|
return requests.post(self.url("/task/new"),
|
|
|
|
files=files,
|
|
|
|
data={'name': name, 'options': json.dumps(options)}).json()
|