Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. import configparser
  2. import copy
  3. import json
  4. import os
  5. import shutil
  6. import sys
  7. import time
  8. import uuid
  9. import zipfile
  10. from pathlib import Path
  11. import requests
  12.  
  13. import flask
  14. import cv2
  15. import numpy as np
  16.  
  17. app = flask.Flask(__name__)
  18.  
  19.  
  20. # Implement API here
  21. #############################
  22.  
  23. download_image_api_url_template = 'https://sandbox.toloka.yandex.ru/api/v1/attachments/{}/download'
  24. token = 'AQAAAAAmIyAzAAIbumyt8ZNFsETZl16aYI24LN4'
  25.  
  26. @app.after_request
  27. def after_request(response):
  28.   response.headers.add('Access-Control-Allow-Origin', '*')
  29.   response.headers.add('Access-Control-Allow-Headers', '*')
  30.   response.headers.add('Access-Control-Allow-Methods', '*')
  31.   return response
  32.  
  33. """
  34. Returns status of detection
  35.  
  36.    Request
  37.    GET
  38.        images: array of images
  39.    
  40.  
  41.    Response
  42.    GET
  43.        result: 1 or 0
  44.        error_message: if result is 0, message with description
  45. """
  46. @app.route('/api/0.1/check_images', methods = ['GET'])
  47. def check_image():
  48.    
  49.     js = flask.request.get_json()
  50.     images_ids = js.get('images')
  51.     print(images_ids)
  52.  
  53.     images = []
  54.     for image in images_ids:
  55.         url = download_image_api_url_template.format(image)
  56.         headers = {'Authorization': 'OAuth {}'.format(token)}
  57.  
  58.         buf = requests.get(url, headers = headers).content
  59.         encoded = np.frombuffer(buf, dtype = np.uint8)
  60.         img = cv2.imdecode(encoded, cv2.IMREAD_UNCHANGED)
  61.         images.append(img)
  62.  
  63.  
  64. ##################################
  65.  
  66.  
  67. def to_json(data):
  68.     return json.dumps(data) + "\n"
  69.  
  70. def resp(code, succeed, error_message, data = {}, mimetype = 'application/json'):
  71.  
  72.     if mimetype == 'application/json':
  73.         body = to_json({'succeed' : succeed,
  74.             'error_message' : error_message,
  75.             **data})
  76.     else:
  77.         body = data
  78.  
  79.     return flask.Response(
  80.         status = code,
  81.         mimetype = mimetype,
  82.         response = body
  83.     )
  84.  
  85. # e.g. failed to parse json
  86. @app.errorhandler(400)
  87. def err(e):
  88.     return resp(err_code_wrong_args, False, 'Failed to parse JSON')
  89.  
  90. @app.errorhandler(404)
  91. def page_not_found(e):
  92.     return resp(err_code_wrong_args, False, 'No such method on server')
  93.  
  94. @app.errorhandler(405)
  95. def page_not_found(e):
  96.     return resp(err_code_wrong_args, False, 'Methods is not allowed')
  97.  
  98. if __name__ == '__main__':
  99.     app.debug = True  # enables auto reload during development
  100.     app.run(host= '0.0.0.0', debug = True, port = 3180)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement