diff --git a/mcc-flow/__main__.py b/mcc-flow/__main__.py index 93d784fc5a913a00152add55dd534fef0dfb0155..fbe80145f4e71c42bfb6f644585981910fe09f16 100644 --- a/mcc-flow/__main__.py +++ b/mcc-flow/__main__.py @@ -1,4 +1,4 @@ -from downloadFilesOfNeuroFlowDevice import download_data +from file_downloader import download_data from merge_records import process_data_files_in_dir from gui import show_message, select_folder_in_explorer @@ -13,7 +13,7 @@ def main(): ) show_message('Give credentials in terminal!', TITLE) path = download_data(path) - out_file = input('Type output file format ( edf / fif ) ') + out_file = input('Select output file format ( edf / fif ) ') process_data_files_in_dir(path, out_file) print(f'Processed files are available in {path}') diff --git a/mcc-flow/downloadFilesOfNeuroFlowDevice.py b/mcc-flow/downloadFilesOfNeuroFlowDevice.py deleted file mode 100644 index 9545161c1cfcdef833cdba5a68a717fc0a989e85..0000000000000000000000000000000000000000 --- a/mcc-flow/downloadFilesOfNeuroFlowDevice.py +++ /dev/null @@ -1,68 +0,0 @@ -import json -from getpass import getpass -from pathlib import Path - -import requests - - -class tcolors: - HEADER = '\033[93m' - OK = '\033[92m' - FAIL = '\033[91m' - ENDC = '\033[0m' - - -def download_data(path='.'): - path = Path(path) - 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: ") - path = path.joinpath(deviceID) - - 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(f'\nLe fogok tölteni {len(listOfFiles)} darab fájl-t a {path} könyvtárba...') - - if not path.exists(): - path.mkdir(parents=True, exist_ok=True) - print(tcolors.OK + "Létrehoztam a '" + str(path) + "' könytárat." + tcolors.ENDC) - - print("") - for i, file in enumerate(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: - save_path = path.joinpath(file) - with open(save_path, "wb") as binary_file: - binary_file.write(r.content) - - print(tcolors.OK + "LETÖLTVE" + tcolors.ENDC, f'{100. * i / len(listOfFiles):.2f} %') - 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") - return path - - -if __name__ == '__main__': - download_data() diff --git a/mcc-flow/file_downloader.py b/mcc-flow/file_downloader.py new file mode 100644 index 0000000000000000000000000000000000000000..5fa0bfad8e77127586ca4580bbf0f40ea55a443f --- /dev/null +++ b/mcc-flow/file_downloader.py @@ -0,0 +1,69 @@ +import json +from getpass import getpass +from pathlib import Path + +import requests + + +class Colors: + HEADER = '\033[93m' + OK = '\033[92m' + FAIL = '\033[91m' + END = '\033[0m' + + +def download_data(path='.'): + path = Path(path) + print(f'\n{Colors.HEADER}================================{Colors.END}') + print(f'{Colors.HEADER}NeuroFlow Lab File Downloader{Colors.END}') + print(f'{Colors.HEADER}================================{Colors.END}\n') + + email = input("email address: ") + password = getpass("password: ") + device_id = input("NeuroFlow device ID: ") + path = path.joinpath(device_id) + + headers = {'Content-Type': 'application/json'} + payload = {'email': email, 'password': password, 'deviceID': device_id} + r = requests.post('https://europe-west3-neuroflowtest.cloudfunctions.net/testListFilesByScript', headers=headers, + data=json.dumps(payload)) + + if r.status_code == 200: + list_of_files = r.json()['files'] + print(f'\n{len(list_of_files)} files will be downloaded to {path}') + + if not path.exists(): + path.mkdir(parents=True, exist_ok=True) + print(f'{Colors.OK}{path} was created{Colors.END}') + + print("") + for i, file in enumerate(list_of_files): + print(f'Downloading file {file}') + + headers = {'Content-Type': 'application/json'} + payload = {'email': email, 'password': password, 'deviceID': device_id, 'fileName': file} + r = requests.post('https://europe-west3-neuroflowtest.cloudfunctions.net/testDownloadByScript', + headers=headers, data=json.dumps(payload)) + + if r.status_code == 200: + save_path = path.joinpath(file) + with open(save_path, "wb") as binary_file: + binary_file.write(r.content) + + print(f'{Colors.OK}DONE{Colors.END} {100. * (i + 1) / len(list_of_files):.2f} %') + else: + print(f'{Colors.FAIL}ERROR{Colors.END}') + + print(f'Files, corresponding to device {device_id} are stored in {device_id} folder.\n') + return path + + elif r.status_code in (401, 444, 445): + print(f'\n{Colors.FAIL}Wrong email or password!{Colors.END}\n') + elif r.status_code == 446: + print(f'\n{Colors.FAIL}Wrong device ID!{Colors.END}\n') + + exit() + + +if __name__ == '__main__': + download_data()