Skip to content
Snippets Groups Projects
Commit 9c76d60c authored by Köllőd Csaba's avatar Köllőd Csaba
Browse files

Update downloadFilesOfNeuroFlowDevice.py

parent d98bd0f8
No related branches found
No related tags found
No related merge requests found
import json
from getpass import getpass from getpass import getpass
from pathlib import Path
import json
import os
import requests import requests
...@@ -12,7 +12,8 @@ class tcolors: ...@@ -12,7 +12,8 @@ class tcolors:
ENDC = '\033[0m' ENDC = '\033[0m'
def download_data(): def download_data(path='.'):
path = Path(path)
print("\n" + tcolors.HEADER + "================================" + tcolors.ENDC) print("\n" + tcolors.HEADER + "================================" + tcolors.ENDC)
print(tcolors.HEADER + "NeuroFlow Lab Kutatói Alkalmazás" + tcolors.ENDC) print(tcolors.HEADER + "NeuroFlow Lab Kutatói Alkalmazás" + tcolors.ENDC)
print(tcolors.HEADER + "================================" + tcolors.ENDC + "\n") print(tcolors.HEADER + "================================" + tcolors.ENDC + "\n")
...@@ -20,6 +21,7 @@ def download_data(): ...@@ -20,6 +21,7 @@ def download_data():
email = input("Kutató email címe: ") email = input("Kutató email címe: ")
password = getpass("Kutató jelszava: ") password = getpass("Kutató jelszava: ")
deviceID = input("Eszköz egyedi azonosítója: ") deviceID = input("Eszköz egyedi azonosítója: ")
path = path.joinpath(deviceID)
headers = {'Content-Type': 'application/json'} headers = {'Content-Type': 'application/json'}
payload = {'email': email, 'password': password, 'deviceID': deviceID} payload = {'email': email, 'password': password, 'deviceID': deviceID}
...@@ -28,14 +30,14 @@ def download_data(): ...@@ -28,14 +30,14 @@ def download_data():
if r.status_code == 200: if r.status_code == 200:
listOfFiles = r.json()['files'] listOfFiles = r.json()['files']
print("\nLe fogok tölteni " + str(len(listOfFiles)) + " darab fájl-t a '" + deviceID + "' nevű könyvtárba...") print(f'\nLe fogok tölteni {len(listOfFiles)} darab fájl-t a {path} könyvtárba...')
if not os.path.exists(deviceID): if not path.exists():
os.makedirs(deviceID) path.mkdir(parents=True, exist_ok=True)
print(tcolors.OK + "Létrehoztam a '" + deviceID + "' nevű könytárat." + tcolors.ENDC) print(tcolors.OK + "Létrehoztam a '" + str(path) + "' könytárat." + tcolors.ENDC)
print("") print("")
for file in listOfFiles: for i, file in enumerate(listOfFiles):
print("Indítom a '" + file + "' nevű fájl letöltését...") print("Indítom a '" + file + "' nevű fájl letöltését...")
headers = {'Content-Type': 'application/json'} headers = {'Content-Type': 'application/json'}
...@@ -44,11 +46,11 @@ def download_data(): ...@@ -44,11 +46,11 @@ def download_data():
headers=headers, data=json.dumps(payload)) headers=headers, data=json.dumps(payload))
if r.status_code == 200: if r.status_code == 200:
path = deviceID + '/' + file save_path = path.joinpath(file)
with open(path, "wb") as binary_file: with open(save_path, "wb") as binary_file:
binary_file.write(r.content) binary_file.write(r.content)
print(tcolors.OK + "LETÖLTVE" + tcolors.ENDC) print(tcolors.OK + "LETÖLTVE" + tcolors.ENDC, f'{100. * i / len(listOfFiles):.2f} %')
else: else:
print(tcolors.FAIL + "HIBA" + tcolors.ENDC) print(tcolors.FAIL + "HIBA" + tcolors.ENDC)
...@@ -59,6 +61,7 @@ def download_data(): ...@@ -59,6 +61,7 @@ def download_data():
print("\n" + tcolors.FAIL + "Hibás email cím vagy jelszó!" + tcolors.ENDC + "\n") print("\n" + tcolors.FAIL + "Hibás email cím vagy jelszó!" + tcolors.ENDC + "\n")
elif r.status_code == 446: elif r.status_code == 446:
print("\n" + tcolors.FAIL + "Hibás eszköz azonosító!" + tcolors.ENDC + "\n") print("\n" + tcolors.FAIL + "Hibás eszköz azonosító!" + tcolors.ENDC + "\n")
return path
if __name__ == '__main__': if __name__ == '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment