reformat code;
This commit is contained in:
parent
8748611fa6
commit
4b6052f502
@ -5,12 +5,15 @@ import json
|
|||||||
import time
|
import time
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
def print_response(response):
|
def print_response(response):
|
||||||
print("Code: " + str(response.status_code) + "; Body: " + str(response.json()))
|
print("Code: " + str(response.status_code) + "; Body: " + str(response.json()))
|
||||||
|
|
||||||
|
|
||||||
def unwrap_file(file_str: str) -> bytes:
|
def unwrap_file(file_str: str) -> bytes:
|
||||||
return base64.b64decode(file_str)
|
return base64.b64decode(file_str)
|
||||||
|
|
||||||
|
|
||||||
def send_image(identifier, image_path, metadata_payload):
|
def send_image(identifier, image_path, metadata_payload):
|
||||||
print("Sending image with identifier " + identifier)
|
print("Sending image with identifier " + identifier)
|
||||||
if not os.path.isfile(image_path):
|
if not os.path.isfile(image_path):
|
||||||
@ -41,10 +44,10 @@ def send_image(identifier, image_path, metadata_payload):
|
|||||||
|
|
||||||
|
|
||||||
def get_image(identifier):
|
def get_image(identifier):
|
||||||
print("Getting image with identifier "+identifier)
|
print("Getting image with identifier " + identifier)
|
||||||
|
|
||||||
baseurl = "http://127.0.0.1:8000"
|
baseurl = "http://127.0.0.1:8000"
|
||||||
get_url = "/image/get/"+identifier
|
get_url = "/image/get/" + identifier
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.get(baseurl + get_url)
|
response = requests.get(baseurl + get_url)
|
||||||
@ -53,9 +56,10 @@ def get_image(identifier):
|
|||||||
print(payload['metadata'])
|
print(payload['metadata'])
|
||||||
b64encoded_image = unwrap_file(payload['image_data'])
|
b64encoded_image = unwrap_file(payload['image_data'])
|
||||||
goal_folder_name = "images_fetch"
|
goal_folder_name = "images_fetch"
|
||||||
path = "../../"+goal_folder_name+"/"+payload['metadata']['filename']
|
path = "../../" + goal_folder_name + "/" + payload['metadata']['filename']
|
||||||
|
|
||||||
goal_folder_path = os.path.sep.join(os.path.abspath(__file__).split(os.path.sep)[:-3]) + os.path.sep + goal_folder_name
|
goal_folder_path = os.path.sep.join(
|
||||||
|
os.path.abspath(__file__).split(os.path.sep)[:-3]) + os.path.sep + goal_folder_name
|
||||||
|
|
||||||
if not os.path.isdir(goal_folder_path):
|
if not os.path.isdir(goal_folder_path):
|
||||||
os.mkdir(goal_folder_path)
|
os.mkdir(goal_folder_path)
|
||||||
@ -70,10 +74,10 @@ def get_image(identifier):
|
|||||||
|
|
||||||
|
|
||||||
def delete_image(identifier):
|
def delete_image(identifier):
|
||||||
print("Deleting image with identifier "+identifier)
|
print("Deleting image with identifier " + identifier)
|
||||||
|
|
||||||
baseurl = "http://127.0.0.1:8000"
|
baseurl = "http://127.0.0.1:8000"
|
||||||
get_url = "/image/delete/"+identifier
|
get_url = "/image/delete/" + identifier
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.delete(baseurl + get_url)
|
response = requests.delete(baseurl + get_url)
|
||||||
@ -84,7 +88,6 @@ def delete_image(identifier):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_all():
|
def get_all():
|
||||||
print("Getting all images")
|
print("Getting all images")
|
||||||
|
|
||||||
@ -98,6 +101,7 @@ def get_all():
|
|||||||
print("Error sending request")
|
print("Error sending request")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
metadata = None
|
metadata = None
|
||||||
index = 0
|
index = 0
|
||||||
metadata_folder = "." + os.path.sep
|
metadata_folder = "." + os.path.sep
|
||||||
@ -106,10 +110,10 @@ metadata_path = metadata_folder + os.path.sep + metadata_file
|
|||||||
|
|
||||||
image_folder = "." + os.path.sep + "images"
|
image_folder = "." + os.path.sep + "images"
|
||||||
|
|
||||||
|
|
||||||
if (os.path.isfile(metadata_path)):
|
if (os.path.isfile(metadata_path)):
|
||||||
print("Loading metadata to memory from " + metadata_path)
|
print("Loading metadata to memory from " + metadata_path)
|
||||||
metadata = sorted(json.load(open(metadata_file, "r")), key=lambda k: datetime.strptime(k['datetime'], '%d-%b-%Y (%H:%M:%S.%f)'))
|
metadata = sorted(json.load(open(metadata_file, "r")),
|
||||||
|
key=lambda k: datetime.strptime(k['datetime'], '%d-%b-%Y (%H:%M:%S.%f)'))
|
||||||
else:
|
else:
|
||||||
print("Default metadata_file not found.")
|
print("Default metadata_file not found.")
|
||||||
|
|
||||||
@ -118,7 +122,7 @@ if (os.path.isdir(image_folder)):
|
|||||||
else:
|
else:
|
||||||
print("Default image folder not found.")
|
print("Default image folder not found.")
|
||||||
|
|
||||||
#main input loop
|
# main input loop
|
||||||
command = "dummy"
|
command = "dummy"
|
||||||
while (command.lower() not in ["exit", "quit", "end"]):
|
while (command.lower() not in ["exit", "quit", "end"]):
|
||||||
|
|
||||||
@ -184,7 +188,8 @@ while (command.lower() not in ["exit", "quit", "end"]):
|
|||||||
print("Error: No file on path " + path)
|
print("Error: No file on path " + path)
|
||||||
continue
|
continue
|
||||||
print("Loading metadata to memory from " + path)
|
print("Loading metadata to memory from " + path)
|
||||||
metadata = sorted(json.load(open(metadata_file, "r")), key=lambda k: datetime.strptime(k['datetime'], '%d-%b-%Y (%H:%M:%S.%f)'))
|
metadata = sorted(json.load(open(metadata_file, "r")),
|
||||||
|
key=lambda k: datetime.strptime(k['datetime'], '%d-%b-%Y (%H:%M:%S.%f)'))
|
||||||
index = 0
|
index = 0
|
||||||
|
|
||||||
if command.lower() == "imagefolder":
|
if command.lower() == "imagefolder":
|
||||||
@ -232,7 +237,6 @@ while (command.lower() not in ["exit", "quit", "end"]):
|
|||||||
get_image(filename[:-4])
|
get_image(filename[:-4])
|
||||||
index = index + 1
|
index = index + 1
|
||||||
|
|
||||||
|
|
||||||
if command.lower() == "fetchall":
|
if command.lower() == "fetchall":
|
||||||
if metadata is None:
|
if metadata is None:
|
||||||
print("No metadata loaded")
|
print("No metadata loaded")
|
||||||
@ -269,7 +273,7 @@ while (command.lower() not in ["exit", "quit", "end"]):
|
|||||||
print("Error: amount is no number")
|
print("Error: amount is no number")
|
||||||
continue
|
continue
|
||||||
amount = int(attributes[0])
|
amount = int(attributes[0])
|
||||||
for i in range(0,amount):
|
for i in range(0, amount):
|
||||||
meta_payload = metadata[index]
|
meta_payload = metadata[index]
|
||||||
filename = meta_payload['filename']
|
filename = meta_payload['filename']
|
||||||
send_image(filename[:-4], image_folder + os.path.sep + filename, meta_payload)
|
send_image(filename[:-4], image_folder + os.path.sep + filename, meta_payload)
|
||||||
@ -310,7 +314,6 @@ while (command.lower() not in ["exit", "quit", "end"]):
|
|||||||
continue
|
continue
|
||||||
index = ind
|
index = ind
|
||||||
|
|
||||||
|
|
||||||
if command.lower() == "select":
|
if command.lower() == "select":
|
||||||
if metadata is None:
|
if metadata is None:
|
||||||
print("Please first select metadata")
|
print("Please first select metadata")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user