commit
6e5d9d1bd8
@ -0,0 +1,149 @@
|
||||
### Example user template template
|
||||
### Example user template
|
||||
|
||||
# IntelliJ project files
|
||||
.idea
|
||||
*.iml
|
||||
out
|
||||
gen
|
||||
### Python template
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
o365_token.txt
|
||||
@ -0,0 +1,20 @@
|
||||
beautifulsoup4==4.11.1
|
||||
certifi==2022.9.24
|
||||
charset-normalizer==2.1.1
|
||||
func-timeout==4.3.5
|
||||
idna==3.4
|
||||
O365==2.0.21
|
||||
oauthlib==3.2.2
|
||||
pause==0.3
|
||||
python-dateutil==2.8.2
|
||||
pythondialog==3.5.3
|
||||
pytz==2022.6
|
||||
pytz-deprecation-shim==0.1.0.post0
|
||||
requests==2.28.1
|
||||
requests-oauthlib==1.3.1
|
||||
six==1.16.0
|
||||
soupsieve==2.3.2.post1
|
||||
stringcase==1.2.0
|
||||
tzdata==2022.6
|
||||
tzlocal==4.2
|
||||
urllib3==1.26.12
|
||||
@ -0,0 +1,161 @@
|
||||
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 == "cancel":
|
||||
break
|
||||
(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 == "cancel":
|
||||
break
|
||||
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")
|
||||
for item in interval:
|
||||
item = int(item)
|
||||
if choice2 == "cancel":
|
||||
break
|
||||
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")
|
||||
if choice4 == "Cancel":
|
||||
break
|
||||
# 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()
|
||||
Loading…
Reference in new issue