sbt-idp/cope2n-api/scripts/script.py

70 lines
2.5 KiB
Python
Raw Normal View History

2024-02-06 03:14:44 +00:00
import os
import time
import requests
from datetime import datetime
# Get the proxy URL from the environment variable
interval = 60*60*1 # 1 minute
2024-02-23 08:50:52 +00:00
update_cost = int(60*2)
2024-02-06 03:14:44 +00:00
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',
2024-02-29 06:12:50 +00:00
'password': '7Eg4AbWIXDnufgn'
# 'password': 'abc'
2024-02-06 03:14:44 +00:00
}
# Define the command to call the update API
update_url = f'{proxy_url}/api/ctel/make_report/'
2024-02-07 05:39:24 +00:00
update_data = {
'is_daily_report': True,
2024-02-06 03:14:44 +00:00
'report_overview_duration': '',
'subsidiary': None
}
"report_overview_duration"
2024-02-28 11:45:10 +00:00
# def update_report(login_token, report_overview_duration=["30d", "7d"], subsidiary=["all", "SEAU", "SESP", "SME", "SEPCO", "TSE", "SEIN"]):
def update_report(login_token, report_overview_duration=["7d", "30d"], subsidiary=["SEAO", "SEAU", "SESP", "SME", "SEPCO", "TSE", "SEIN"]):
2024-02-06 03:14:44 +00:00
headers = {'Authorization': login_token}
for dur in report_overview_duration:
for sub in subsidiary:
2024-02-07 05:39:24 +00:00
update_data["report_overview_duration"] = dur
update_data["subsidiary"] = sub
update_response = requests.post(update_url, data=update_data, headers=headers)
2024-02-06 03:14:44 +00:00
print("[INFO]: update_response at {} by {} - {} with status {}".format(datetime.now(), dur, sub, update_response.status_code))
update_response.raise_for_status()
time.sleep(update_cost)
# Define the interval in seconds between API calls
# time.sleep(60)
while True:
# Call the login API and retrieve the login token
if not login_token:
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()))
# Call the update API
try:
update_report(login_token)
except Exception as e:
print(f"[ERROR]: {e}")
print(f"[ERROR]: Failed to update_response, retrying...")
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()))
update_report(login_token)
# Wait for the specified interval
time.sleep(interval)