From 341ba5ce931dedeff511aef71c9a30034242b2fb Mon Sep 17 00:00:00 2001 From: dx-tan Date: Tue, 6 Feb 2024 16:32:09 +0700 Subject: [PATCH] Add: onetime runnig files --- cope2n-api/scripts/re_feedback.py | 66 +++++++++++++++++++++++++++++++ cope2n-api/scripts/script.py | 5 ++- 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 cope2n-api/scripts/re_feedback.py diff --git a/cope2n-api/scripts/re_feedback.py b/cope2n-api/scripts/re_feedback.py new file mode 100644 index 0000000..34f82a7 --- /dev/null +++ b/cope2n-api/scripts/re_feedback.py @@ -0,0 +1,66 @@ +import os +import time +import requests +from datetime import datetime +# from tqdm import tqdm + +ROOT = "/root/TannedCung/sbt-idp/feedback" + +interval = 3 +# Get the proxy URL from the environment variable +proxy_url = os.getenv('PROXY', "localhost") + +# Define the login API URL +login_url = f'{proxy_url}/api/ctel/login/' +login_token = None + +# Define the login credentials +login_credentials = { + 'username': 'sbt', + # 'password': '7Eg4AbWIXDnufgn' + 'password': 'abc' +} + +# Define the command to call the update API +update_url = f'{proxy_url}/api/ctel/images/feedback_file/' + +def login(): + login_token = None + login_response = requests.post(login_url, data=login_credentials) + # login_response.raise_for_status() + if login_response.status_code == 200: + login_token = login_response.json()['token'] + print("[INFO] relogged in at {}".format(datetime.now())) + return login_token + +def feedback(login_token, file_path): + headers = {'Authorization': login_token} + files = { + 'files': (file_path.split("/")[-1], open(file_path, 'rb'), 'text/csv') + } + response = requests.post(update_url, headers=headers, files=files) + + if response.status_code == 200: + print('[INFO]: File uploaded successfully.') + else: + print('[ERROR]: File upload failed.') + +def search_csv_files(directory): + csv_files = [] + for root, dirs, files in os.walk(directory): + for file in files: + if file.endswith(".csv"): + csv_files.append(os.path.join(root, file)) + + csv_files.sort(key=lambda path: int(os.path.basename(os.path.dirname(path)))) + return csv_files + +def main(): + login_token = login() + list_files = search_csv_files(ROOT) + for _file in list_files: + feedback(login_token, _file) + time.sleep(interval) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/cope2n-api/scripts/script.py b/cope2n-api/scripts/script.py index 713c925..7ff2d96 100644 --- a/cope2n-api/scripts/script.py +++ b/cope2n-api/scripts/script.py @@ -5,7 +5,7 @@ from datetime import datetime # Get the proxy URL from the environment variable interval = 60*60*1 # 1 minute -update_cost = 60*3 +update_cost = 60*2 proxy_url = os.getenv('PROXY', "localhost") # Define the login API URL @@ -15,7 +15,8 @@ login_token = None # Define the login credentials login_credentials = { 'username': 'sbt', - 'password': '7Eg4AbWIXDnufgn' + # 'password': '7Eg4AbWIXDnufgn' + 'password': 'abc' } # Define the command to call the update API