You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
10 KiB
162 lines
10 KiB
import logging
|
|
import sys
|
|
import O365
|
|
import traceback
|
|
import pause
|
|
import dialog
|
|
import argparse
|
|
from dialog import Dialog
|
|
from o365_util import O365_User
|
|
|
|
if __name__ == '__main__':
|
|
# Announce an argument parser
|
|
parser = argparse.ArgumentParser(description="Automated API calling program.")
|
|
parser.add_argument("-i", "--interactive", action="store_true", help="Config the arguments in interactive mode")
|
|
parser.add_argument("--business-hour", action="store_true", help="Only runs the routine in business hours, "
|
|
"that is 9AM to 5PM from Monday to Friday.")
|
|
parser.add_argument("--interval", type=int, help="Set the interval of routine. "
|
|
"(Conflicts with the min and max arguments!)")
|
|
parser.add_argument("--min", type=int, help="Set the minimum interval of routine. "
|
|
"(Conflicts with the interval arguments!)")
|
|
parser.add_argument("--max", type=int, help="Set the maximum interval of routine. "
|
|
"(Conflicts with the interval arguments!)")
|
|
parser.add_argument("-t", "--temp-file-threshold", type=int, default=5, help="The maximum amount of files "
|
|
"stored in your onedrive drive.")
|
|
parser.add_argument("--client-id", type=str, help="The client id from graph API.")
|
|
parser.add_argument("--secret", type=str, help="The client secret from graph API.")
|
|
if not len(sys.argv) == 1:
|
|
parsed_arguments = parser.parse_args(sys.argv[1:])
|
|
try:
|
|
# Check interactive mode first
|
|
if parsed_arguments.interactive:
|
|
d = Dialog()
|
|
d.set_background_title("Office E5 Continuer")
|
|
d.infobox("Welcome to use this program!", width=54)
|
|
pause.seconds(2)
|
|
# Setting the default value
|
|
credentials = [("" if not parsed_arguments.client_id else parsed_arguments.client_id),
|
|
("" if not parsed_arguments.secret else parsed_arguments.secret)]
|
|
routine_mode = ""
|
|
interval: int = 0
|
|
business_hour: bool = False
|
|
threshold: int = 5
|
|
while True:
|
|
(choice0, credentials) = d.form("Enter the Client ID and secret below:",
|
|
width=65,
|
|
elements=[
|
|
("Client ID", 2, 5,
|
|
credentials[0],
|
|
2, 16, 41, 0),
|
|
("Secret", 4, 5,
|
|
credentials[1],
|
|
4, 16, 41, 0)
|
|
],
|
|
title="Setting the API",
|
|
form_height=5)
|
|
if choice0 == d.CANCEL:
|
|
exit(2)
|
|
(choice1, routine_mode) = d.menu("Choose the routine mode listed below.",
|
|
width=54,
|
|
choices=[("Fixed", "Fixed interval between each routine."),
|
|
("Ranged", "Randomly picks an interval in a range.")],
|
|
title="Routine Mode")
|
|
if choice1 == d.CANCEL:
|
|
exit(2)
|
|
else:
|
|
if routine_mode == "Fixed":
|
|
(choice2, interval) = d.inputbox("Enter the interval during each routine, in seconds",
|
|
width=54,
|
|
init=str(
|
|
600 if not parsed_arguments.interval
|
|
else parsed_arguments.interval),
|
|
title="Setting the interval")
|
|
interval = int(interval)
|
|
else:
|
|
(choice2, interval) = d.form("Enter the minimum and the maximum interval, in seconds",
|
|
width=54,
|
|
elements=[
|
|
("Min", 1, 15,
|
|
str(600 if not parsed_arguments.min else
|
|
parsed_arguments.min),
|
|
1, 23, 10, 0),
|
|
("Max", 2, 15,
|
|
str(1200 if not parsed_arguments.max else
|
|
parsed_arguments.max),
|
|
2, 23, 10, 0)
|
|
],
|
|
title="Setting the interval")
|
|
interval = (int(interval[0]), int(interval[1]))
|
|
if choice2 == "cancel":
|
|
exit(2)
|
|
business_hour = True if d.yesno("Do you wanna enable business hours mode?\n"
|
|
"If you enabled this, the API will be only called from 9 to 5, "
|
|
"from Monday to Friday.",
|
|
width=54) == d.OK else False
|
|
(choice4, threshold) = d.inputbox("Enter the maximum file amount stored in the OneDrive.",
|
|
width=54,
|
|
init=str(5 if not parsed_arguments.temp_file_threshold
|
|
else parsed_arguments.temp_file_threshold),
|
|
title="Temporary file threshold")
|
|
threshold = int(threshold)
|
|
if choice4 == "Cancel":
|
|
exit(2)
|
|
# Conclusion
|
|
if d.yesno("You've selected:\n"
|
|
"Client ID: {0}\n"
|
|
"Secret: {1}***{2}\n"
|
|
"Routine Mode: {3}\n"
|
|
"Interval: {4} s\n"
|
|
"Business Mode: {5}\n"
|
|
"Threshold: {6}\n\n"
|
|
"Is this OK?".format(credentials[0], credentials[1][0:5],
|
|
credentials[1][-5:],
|
|
routine_mode,
|
|
str(interval) if isinstance(interval, int) else (
|
|
"{0} - {1}".format(str(interval[0]),
|
|
str(interval[1]))),
|
|
"Yes" if business_hour else "No",
|
|
str(threshold)
|
|
),
|
|
width=54,
|
|
height=15,
|
|
title="Final Check") == d.OK:
|
|
break
|
|
else:
|
|
continue
|
|
(client_id, secret) = credentials
|
|
account = O365.Account(credentials=(client_id, secret),
|
|
scopes=['basic', 'message_all', 'onedrive_all'])
|
|
user_obj = O365_User(account)
|
|
user_obj.infinite_routine(interval, business_hour, 30, threshold, interactive=True)
|
|
else:
|
|
if parsed_arguments.client_id is None or parsed_arguments.secret is None:
|
|
raise ValueError("Both of the client ID and secret VALUE (not uuid) should be provided!")
|
|
if parsed_arguments.interval and (parsed_arguments.min or parsed_arguments.max):
|
|
raise ValueError("Intervals and the range cannot be set at the same time!")
|
|
if (not (parsed_arguments.min and parsed_arguments.max)) and (not parsed_arguments.interval):
|
|
raise ValueError("The minimum and the maximum should be set at the same time. "
|
|
"If you wanna use a fixed interval, use --interval instead.")
|
|
account = O365.Account(credentials=(parsed_arguments.client_id, parsed_arguments.secret),
|
|
scopes=['basic', 'message_all', 'onedrive_all'])
|
|
user_obj = O365_User(account)
|
|
user_obj.infinite_routine(int(parsed_arguments.interval) if parsed_arguments.interval else
|
|
(int(parsed_arguments.min), int(parsed_arguments.max)),
|
|
parsed_arguments.business_hour,
|
|
30,
|
|
int(parsed_arguments.temp_file_threshold))
|
|
except dialog.PythonDialogBug:
|
|
logging.error("Cannot use dialogs here, try to change to a terminal or disable interactive mode. Make sure "
|
|
"that UNIX dialog have been installed in your machine.")
|
|
except ValueError as e:
|
|
if parsed_arguments.interactive:
|
|
d = Dialog()
|
|
d.msgbox(str(e), width=54, title="Error")
|
|
logging.error("{0}: {1}".format(type(e).__name__, e))
|
|
logging.error(traceback.format_exc())
|
|
except Exception as e:
|
|
logging.error("{0}: {1}".format(type(e).__name__, e))
|
|
logging.error(traceback.format_exc())
|
|
|
|
else:
|
|
parser.print_help()
|