From ac2cbf09526db3d7bf5a537ccd74e8962596ce4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=B6ll=C5=91d=20Csaba?= <kollod.csaba@itk.ppke.hu> Date: Tue, 13 Dec 2022 09:33:23 +0100 Subject: [PATCH] Add file downloader --- mcc-flow/downloadFilesOfNeuroFlowDevice.py | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 mcc-flow/downloadFilesOfNeuroFlowDevice.py diff --git a/mcc-flow/downloadFilesOfNeuroFlowDevice.py b/mcc-flow/downloadFilesOfNeuroFlowDevice.py new file mode 100644 index 0000000..215f702 --- /dev/null +++ b/mcc-flow/downloadFilesOfNeuroFlowDevice.py @@ -0,0 +1,65 @@ +from getpass import getpass + +import json +import os +import requests + + +class tcolors: + HEADER = '\033[93m' + OK = '\033[92m' + FAIL = '\033[91m' + ENDC = '\033[0m' + + +def download_data(): + print("\n" + tcolors.HEADER + "================================" + tcolors.ENDC) + print(tcolors.HEADER + "NeuroFlow Lab Kutatói Alkalmazás" + tcolors.ENDC) + print(tcolors.HEADER + "================================" + tcolors.ENDC + "\n") + + email = input("Kutató email címe: ") + password = getpass("Kutató jelszava: ") + deviceID = input("Eszköz egyedi azonosítója: ") + + headers = {'Content-Type': 'application/json'} + payload = {'email': email, 'password': password, 'deviceID': deviceID} + r = requests.post('https://europe-west3-neuroflowtest.cloudfunctions.net/testListFilesByScript', headers=headers, + data=json.dumps(payload)) + + if r.status_code == 200: + listOfFiles = r.json()['files'] + print("\nLe fogok tölteni " + str(len(listOfFiles)) + " darab fájl-t a '" + deviceID + "' nevű könyvtárba...") + + if not os.path.exists(deviceID): + os.makedirs(deviceID) + print(tcolors.OK + "Létrehoztam a '" + deviceID + "' nevű könytárat." + tcolors.ENDC) + + print("") + for file in listOfFiles: + print("Indítom a '" + file + "' nevű fájl letöltését...") + + headers = {'Content-Type': 'application/json'} + payload = {'email': email, 'password': password, 'deviceID': deviceID, 'fileName': file} + r = requests.post('https://europe-west3-neuroflowtest.cloudfunctions.net/testDownloadByScript', + headers=headers, data=json.dumps(payload)) + + if r.status_code == 200: + path = deviceID + '/' + file + with open(path, "wb") as binary_file: + binary_file.write(r.content) + + print(tcolors.OK + "LETÖLTVE" + tcolors.ENDC) + else: + print(tcolors.FAIL + "HIBA" + tcolors.ENDC) + + print( + "\nA '" + deviceID + "' eszközhöz tartozó fájlok letöltésre kerültek a '" + deviceID + "' nevű könyvtárba.\n") + + elif r.status_code in (401, 444, 445): + print("\n" + tcolors.FAIL + "Hibás email cím vagy jelszó!" + tcolors.ENDC + "\n") + elif r.status_code == 446: + print("\n" + tcolors.FAIL + "Hibás eszköz azonosító!" + tcolors.ENDC + "\n") + + +if __name__ == '__main__': + download_data() -- GitLab