Skip to content

Commit b6c4c2a

Browse files
authored
Use the new tracks feed. (#45)
The tracks feed means we no longer need our own list/mapping of tracks and track colors, reducing one more manual step. There's a temporary workaround here for a bug in the feed. Signed-off-by: Phil Dibowitz <phil@ipom.com>
1 parent 03db625 commit b6c4c2a

File tree

1 file changed

+50
-56
lines changed

1 file changed

+50
-56
lines changed

guidebook/sync_guidebook.py

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
except ImportError:
5656
import xdg
5757

58-
DBASE_DEFAULT = "https://www.socallinuxexpo.org/scale/23x/app"
58+
EVENTS_FEED = "https://www.socallinuxexpo.org/scale/23x/app"
59+
TRACKS_FEED = "https://www.socallinuxexpo.org/api/tracks/23x"
5960
GUIDE_NAME = "SCaLE 23x"
6061

6162

@@ -153,7 +154,7 @@ def send_to_datadog(self, logger, dryrun=False):
153154

154155
class OurJSON:
155156
rooms = set()
156-
tracks = set()
157+
tracks = {}
157158
sessions_by_name = {}
158159
sessions_by_nid = {}
159160

@@ -162,28 +163,42 @@ class OurJSON:
162163
"rooms": "Location",
163164
}
164165

165-
def __init__(self, path, logger):
166+
def __init__(self, event_feed, track_feed, logger):
166167
self.logger = logger
168+
169+
event_data = self._get_feed_data(event_feed)
170+
track_data = self._get_feed_data(track_feed)
171+
172+
self.sessions_by_name, self.sessions_by_nid = self._load_event_json(
173+
event_data
174+
)
175+
self.tracks = self._load_tracks_json(track_data)
176+
177+
def _get_feed_data(self, path):
178+
self.logger.info("Loading JSON feed from %s" % path)
167179
if path.startswith("http://") or path.startswith("https://"):
168180
response = requests.get(path)
169-
blob = response.text
170-
else:
171-
blob = open(path, "r").read()
172-
self.sessions_by_name, self.sessions_by_nid = self.load_json(blob)
181+
return response.text
182+
return open(path, "r").read()
173183

174-
def load_json(self, raw):
175-
self.logger.info("Loading JSON file")
184+
def _load_tracks_json(self, raw):
185+
raw = json.loads(raw)
186+
tracks = {}
187+
for track in raw:
188+
# temporary until we fix the feed
189+
name = track["name"].replace("&amp;", "&")
190+
tracks[name] = track["color"]
191+
return tracks
192+
193+
def _load_event_json(self, raw):
176194
raw = json.loads(raw)
177195
data_by_name = {}
178196
data_by_nid = {}
179197
for session in raw:
180198
# handle leading/trailing spaces in names
181199
name = session["Name"].strip()
182200
session["Name"] = name
183-
track = session[self.FIELD_MAPPING["tracks"]].strip()
184201
room = session[self.FIELD_MAPPING["rooms"]].strip()
185-
if track != "":
186-
self.tracks.add(track)
187202
if room != "":
188203
self.rooms.add(room)
189204
clean_session = {k: v.strip() for k, v in session.items()}
@@ -204,38 +219,6 @@ class GuideBook:
204219
"publish": "https://builder.guidebook.com/api/guides/{guide}/publish/",
205220
}
206221

207-
COLOR_MAP = {
208-
"Applied Science": "#dddddd",
209-
"AstriCon": "#8b4789",
210-
"BoFs": "#ffbc00",
211-
"Career Day": "#dddddd", # Open Source Career Day
212-
"Cloud Native": "#638dce", # Cloud Native Days
213-
"Developer": "#d65c09",
214-
"DevOpsDay LA": "#565448",
215-
"Embedded Linux": "#004a4a",
216-
"Entertainment": "#ff6f91",
217-
"Fedora Hatch Day": "#294172",
218-
"FOSS @ HOME": "#998876",
219-
"General": "#97a67a",
220-
"HAM Radio": "#96beef",
221-
"Higher Education": "#fff8dc", # Open Source in Higher Education
222-
"Kernel & Low Level Systems": "#ffa200",
223-
"Keynote": "#d31111",
224-
"Kwaai Summit": "#4b2e83",
225-
"LibreGraphics": "#e10098",
226-
"MySQL": "#0aaca0",
227-
"Next Generation": "#96f74b", # The Next Generation
228-
"Observability": "#ffbc00",
229-
"Open Government": "#6c6c6c",
230-
"Open Source AI": "#ffd672",
231-
"PlanetNix": "#2d5d3f",
232-
"PostgreSQL": "#0aaca0",
233-
"Security": "#000000",
234-
"SunSecCon": "#e63946",
235-
"Systems & Infrastructure": "#c4c249",
236-
"Workshops": "#774022",
237-
}
238-
239222
ROOM_TO_MAP_REGION = {
240223
"Ballroom A": {"h": 0.04, "w": 0.056, "x": 0.668, "y": 0.477},
241224
"Ballroom B": {"h": 0.04, "w": 0.056, "x": 0.668, "y": 0.519},
@@ -406,7 +389,7 @@ def add_thing(self, thing, name, data, update, tid):
406389
sys.exit(1)
407390
return response
408391

409-
def add_track(self, track, update, tid):
392+
def add_track(self, track, color, update, tid):
410393
"""
411394
Track-specific wrapper around add_thing()
412395
"""
@@ -416,34 +399,38 @@ def add_track(self, track, update, tid):
416399
"guide": self.guide,
417400
"name": track,
418401
# NOTE WELL: Guidebook cannot handle lower-case letters
419-
"color": self.COLOR_MAP[track].upper(),
402+
"color": color,
420403
}
421-
self.tracks[track] = self.add_thing("tracks", track, data, update, tid)
404+
newinfo = self.add_thing("tracks", track, data, update, tid)
405+
if not self.dryrun:
406+
self.tracks[track] = newinfo
422407
operation = "updated" if update else "added"
423408
self.stats.increment("tracks", operation)
424409

425410
def setup_tracks(self, tracks):
426411
"""
427412
Add all tracks passed in if missing.
428413
"""
429-
for track in tracks:
414+
for track, color in tracks.items():
415+
# Guidebook only deals in upper-case colors, so we must match
416+
color = color.upper()
430417
update = False
431418
tid = None
432419
if track in self.tracks:
433420
orig = self.tracks[track]
434421
# the only "info" about a track is the color (the name is
435422
# our primary key), so if the color is correct, it's up to date.
436-
if orig["color"].upper() == self.COLOR_MAP[track].upper():
423+
if orig["color"] == color:
437424
self.logger.debug(
438425
"Track '%s' exists in Guidebook and has correct color"
439426
" %s. No update needed.",
440427
track,
441-
self.COLOR_MAP[track].upper(),
428+
color,
442429
)
443430
continue
444431
update = True
445432
tid = self.tracks[track]["id"]
446-
self.add_track(track, update, tid)
433+
self.add_track(track, color, update, tid)
447434

448435
def add_room(self, room, update, rid):
449436
"""
@@ -966,12 +953,17 @@ def get_tokens(logger):
966953
help="Delete all tracks, rooms, and sessions",
967954
)
968955
@click.option(
969-
"--json",
970-
"feed",
956+
"--event-feed",
971957
metavar="FILE_OR_URL",
972-
default=DBASE_DEFAULT,
958+
default=EVENTS_FEED,
973959
help="JSON file or http(s) URL to JSON data.",
974960
)
961+
@click.option(
962+
"--track-feed",
963+
metavar="FILE_OR_URL",
964+
default=TRACKS_FEED,
965+
help="JSON file or http(s) URL to track data.",
966+
)
975967
@click.option(
976968
"--dryrun/--no-dryrun",
977969
"-n",
@@ -984,7 +976,9 @@ def get_tokens(logger):
984976
help="Max number of sessions to delete when syncing. Zero will not"
985977
" delete any sessions. Ignored if --delete-all is used.",
986978
)
987-
def main(debug, update, delete_all, feed, dryrun, max_deletes):
979+
def main(
980+
debug, update, delete_all, event_feed, track_feed, dryrun, max_deletes
981+
):
988982
"""
989983
Sync the schedule data from our website to Guidebook.
990984
@@ -1019,7 +1013,7 @@ def main(debug, update, delete_all, feed, dryrun, max_deletes):
10191013
print("into a schedule to lose all of that work.")
10201014
click.confirm("ARE YOU FUCKING SURE?!", abort=True)
10211015
else:
1022-
ourdata = OurJSON(feed, logger)
1016+
ourdata = OurJSON(event_feed, track_feed, logger)
10231017

10241018
ourguide = GuideBook(
10251019
logger, update, dryrun, max_deletes, key, stats_tracker, x_key=x_key

0 commit comments

Comments
 (0)