-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·86 lines (71 loc) · 2.2 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import argparse
from models import WebinarjamController
from utils import (
get_registrants_from_csv,
write_registrants_to_db,
clear_reports,
configure_logging,
)
from config import *
HEADLESS = True
# set up argparse
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
"-e",
"--event",
default="yesterday",
choices=[
"yesterday",
"all time",
"today",
"this week",
"last week",
"last 7 days",
"this month",
"last month",
"last 30 days",
],
)
args = arg_parser.parse_args()
# set up logging
logger = configure_logging("debug.log")
def main():
try:
logger.info("start")
# delete old reports
clear_reports()
# try to download all reports from the site
# 3 attempts
for _ in range(3):
try:
logger.info("step 1: scraping reports")
app = WebinarjamController(
SITE_LOGIN, SITE_PASSWD, headless=HEADLESS, logger=logger
)
app.get_all_reports(event=args.event) # for all webinars
break
except Exception:
logger.error("failed to download reports", exc_info=True)
else:
logger.error("3 failed attempts achieved, terminating the program")
exit(1)
# get all registrants from the downloaded csv reports (in the reports directory)
logger.info("step 2: getting all registrants from all reports")
registrants = get_registrants_from_csv()
logger.info("step 2: got {} registrant(s)".format(len(registrants)))
# write the registrants to the database
logger.info("step 3: writing the registrants to the database")
write_registrants_to_db(
registrants=registrants,
db_host=DATABASE["host"],
db_name=DATABASE["name"],
db_login=DATABASE["user"],
db_pass=DATABASE["password"],
)
logger.info("profit!")
except KeyboardInterrupt:
logger.info("exited")
except Exception:
logger.critical("critical error occurred", exc_info=True)
if __name__ == "__main__":
main()