-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventbrite_manager.py
214 lines (177 loc) · 8.72 KB
/
eventbrite_manager.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import argparse
import yaml
from datetime import datetime, timedelta, timezone
import os
import re
from git import Repo
from bs4 import BeautifulSoup
import configparser
from glob import glob
#import interfaces.zoom.ZoomInterface as ZoomInterface
import interfaces.eventbrite.EventbriteInterface as eventbrite
from common import UTC_FMT, valid_date, to_iso8061, ISO_8061_FORMAT, Trainers, get_config
from common import get_trainer_keys
import CQORCcalendar
def actualize_repo(url, local_repo):
"""
Clones or pulls the repo at `url` to `local_repo`.
"""
repo = (
Repo(local_repo)
if os.path.exists(local_repo)
else Repo.clone_from(url, local_repo)
)
# pull in latest changes if any, expects remote to be named `origin`
repo.remotes.origin.pull()
def update_html(description, content, instructor_name):
"""
Updates the HTML description with the content and instructor.
The HTML description is a template with the following tags:
[[SUMMARY]]
[[DESCRIPTION]]
[[PREREQS]]
[[PLAN]]
[[INSTRUCTOR]]
The content is a dictionary with the following keys:
summary
description
prerequisites
plan
"""
soup = BeautifulSoup(description, "html.parser")
# Find elements!
# Remove the summary which is not used
summary = soup.select_one("div")
summary.decompose()
desc = soup.find("p", string="[[DESCRIPTION]]")
prerequis = soup.find("p", string="[[PREREQS]]")
plan = soup.find("p", string="[[PLAN]]")
instructor = soup.find("p", string=re.compile("\\[\\[INSTRUCTOR\\]\\]"))
# Update them
# summary.string = summary.string.replace("[[SUMMARY]]", content["summary"])
if desc:
desc.string = desc.string.replace("[[DESCRIPTION]]", content["description"])
if prerequis:
new_prereqs = soup.new_tag("ul")
for i in content["prerequisites"]:
new_li = soup.new_tag("li")
new_li.string = i
new_prereqs.append(new_li)
prerequis.replace_with(new_prereqs)
if plan:
new_plan = soup.new_tag("ul")
for item in content["plan"]:
if isinstance(item, str):
new_li = soup.new_tag("li")
new_li.string = item
new_plan.append(new_li)
elif isinstance(item, list):
new_li = soup.new_tag("li")
new_li.string = item[0]
new_plan.append(new_li)
new_sub_plan = soup.new_tag("ul")
for subitem in item[1:]:
new_li = soup.new_tag("li")
new_li.string = subitem
new_sub_plan.append(new_li)
new_plan.append(new_sub_plan)
plan.replace_with(new_plan)
# Update instructor
if instructor:
instructor.string = instructor.string.replace("[[INSTRUCTOR]]", instructor_name)
return soup
if __name__ == "__main__":
config = configparser.ConfigParser()
parser = argparse.ArgumentParser(description="Create a new event from a template.")
parser.add_argument("--config_dir", default=".", help="Directory that holds the configuration files")
parser.add_argument("--secrets_dir", default="./secrets", help="Directory that holds the configuration files")
parser.add_argument("--date", metavar=ISO_8061_FORMAT, type=valid_date, help="Generate for the first event on this date")
parser.add_argument("--course_id", help="Handle course specified by course_id")
parser.add_argument("--dry-run", default=False, action='store_true', help="Dry-run")
parser.add_argument("--create", default=False, action='store_true', help="Create event")
parser.add_argument("--update", default=False, action='store_true', help="Update event")
parser.add_argument("--delete", default=False, action='store_true', help="Delete event")
args = parser.parse_args()
print(vars(args))
config = get_config(args)
trainers = Trainers(config['global']['trainers_db'])
#zoom_user = config['zoom']['user']
#zoom = ZoomInterface.ZoomInterface(config['zoom']['account_id'], config['zoom']['client_id'], config['zoom']['client_secret'], config['global']['timezone'], zoom_user)
# no need to actualize the repo if we are not creating or updating the event
if args.create or args.update:
actualize_repo(config["descriptions"]["repo_url"], config["descriptions"]["local_repo"])
eb = eventbrite.EventbriteInterface(config["eventbrite"]["api_key"])
# get the courses from the working calendar in the Google spreadsheets
calendar = CQORCcalendar.Calendar(config, args)
courses = calendar.get_courses()
# keep only courses that start on the date listed
if args.date:
courses = [courses for course in courses if args.date.date().isoformat() in course['sessions'][0]['start_date']]
# keep only the course for the course_id specified
if args.course_id:
courses = [calendar[args.course_id]]
for course in courses:
first_session = course['sessions'][0]
instructor = ','.join([trainers.fullname(key) for key in get_trainer_keys(course, ['instructor'])])
if first_session['code']:
# Read the description from the repo
with open(os.path.join(config["descriptions"]["local_repo"], f"{first_session['code']}-{first_session['language']}.yaml")) as f:
event_description = yaml.safe_load(f)
else:
event_description = None
print("Empty workshop code, skipping updating description")
# handle course on multiple sessions
if len(course['sessions']) > 1:
print(f"{event_description['plan']}")
if not isinstance(event_description['plan'], list) or not len(event_description['plan']) == len(course['sessions']):
print(f"Error: Course is multiple sessions. Expecting a lesson plan that is two-dimensional of length {len(course['sessions'])}.")
for idx, session in enumerate(course['sessions']):
start_date = to_iso8061(session['start_date'])
end_date = to_iso8061(session['end_date'])
event_description['plan'][idx][0] = f"<b>{event_description['plan'][idx][0]} ({start_date.date()}, {start_date.time().__str__()[:5]} - {end_date.time().__str__()[:5]})</b>"
# Create the event
if args.create:
if first_session['eventbrite_id']:
print(f"Error: event already exists with EventBrite ID: {first_session['eventbrite_id']}")
exit(1)
# for multi-session courses, the duration of the webinar must be from the start to the end
start_date = min([to_iso8061(session['start_date']) for session in course['sessions']])
end_date = max([to_iso8061(session['end_date']) for session in course['sessions']])
eventid = eb.create_event_from(
event_id=first_session['template'],
title=first_session['title'],
start_date=start_date,
end_date=end_date,
tz=config["global"]["timezone"],
summary=event_description["summary"] if event_description else "",
)
calendar.set_eventbrite_id(first_session['course_id'], eventid)
calendar.update_spreadsheet()
print(f"Successfully created {first_session['title']}({eventid}) {start_date} {end_date}")
else:
eventid = first_session['eventbrite_id']
# Update the event
if args.update or args.create:
if event_description:
# Update the description
eb.update_event_description(eventid, str(update_html(
eb.get_event_description(eventid)['description'],
event_description,
instructor,
)))
print(f'Successfully updated {eventid} description')
# Update tickets classes
hours = int(config["eventbrite"]["close_hours_before_event"])
eb.update_tickets(eventid, "", (to_iso8061(first_session['start_date']) - timedelta(hours=hours)).astimezone(timezone.utc).strftime(UTC_FMT))
print(f'Successfully updated {eventid} ticket classes')
# Update Zoom webinar
# Note: This merely creates a generic webinar, not a Zoom connection
# if first_session['zoom_id']:
# webinar = zoom.get_webinar(first_session['zoom_id'])
# eb.update_webinar_url(eventid, webinar['join_url'])
# Delete the event
if args.delete:
eb.delete_event(eventid)
calendar.set_eventbrite_id(first_session['course_id'], '')
print(f'Successfully deleted {eventid}')
calendar.update_spreadsheet()