Example #1
0
def fetch_snapshot_url(token):
    headers = {
        'Authorization': "Bearer {0}".format(token),
    }
    req = urllib2.Request(nest_api_url, None, headers)
    response = urllib2.urlopen(req)
    data = json.loads(response.read())

    # Verify the account has devices
    if 'devices' not in data:
        raise APIError(error_result("Nest account has no devices"))
    devices = data["devices"]

    # verify the account has cameras
    if 'cameras' not in devices:
        raise APIError(error_result("Nest account has no cameras"))
    cameras = devices["cameras"]

    # Verify the account has 1 Nest Cam
    if len(cameras.keys()) < 1:
        raise APIError(error_result("Nest account has no cameras"))

    camera_id = cameras.keys()[0]
    camera = cameras[camera_id]

    # Verify the Nest Cam has a Snapshot URL field
    if 'snapshot_url' not in camera:
        raise APIError(error_result("Camera has no snapshot URL"))
    snapshot_url = camera["snapshot_url"]

    return snapshot_url
Example #2
0
def apierr_from_msg(code, err_msg="Error"):
    # get additional message (not from the API), for next steps or more details.
    help_msg = get_error_msg_help(code, '')

    return APIError(
        code, error_result("{0}: {1}. {2}".format(str(code), err_msg,
                                                  help_msg)))
Example #3
0
def classify_remote_image(image_url):
    # Attempt to Download
    try:
        image = download_image(image_url)
    except IOError:
        return error_result("Camera's Snapshot URL could not be downloaded")

    # Attempt to Classify
    try:
        results = run_inference_on_image(image)
    except:
        return error_result("Could not classify the image")

    return {
        "image_url": image_url,
        "results": results
    }
Example #4
0
def server_error(e):
    return jsonify(error_result('500 - Server error'))
Example #5
0
def page_not_found(e):
    return jsonify(error_result('404 - Page not found'))