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
|
import base64
import json
import os
import re
import urllib.parse
import urllib.request
GONDUL_USERNAME = os.getenv("GONDUL_USERNAME", "")
GONDUL_PASSWORD = os.getenv("GONDUL_PASSWORD", "")
GONDUL_API = os.getenv("GONDUL_API", "https://tg18.gondul.gathering.org/api")
GONDUL_SWITCHES_ENDPOINT = os.getenv(
"GONDUL_SWITCHES_ENDPOINT", "/public/switches")
def _generate_credentials(username, password):
return base64.standard_b64encode(
(username + ":" + password)
.encode("utf-8")).decode("utf-8")
def _do_switches_request(
api=GONDUL_API,
endpoint=GONDUL_SWITCHES_ENDPOINT,
credentials=_generate_credentials(GONDUL_USERNAME, GONDUL_PASSWORD)):
switches_url = api + endpoint
# Build request
request = urllib.request.Request(switches_url)
request.add_header("Authorization", "Basic " + credentials)
resp = urllib.request.urlopen(request, timeout=5)
assert resp.status == 200, "HTTP return was not 200 OK"
# Read response
body = resp.read().decode("utf-8")
data = json.loads(body)
assert "switches" in data, "Missing switches object from HTTP response"
switches = data.get("switches")
print("Found {} switches in Gondul".format(len(switches)))
return switches
def _match_switches(switches, match="^e(.*)"):
pattern = re.compile(match)
included_switches = []
for switch in switches:
include = re.search(pattern, switch)
if include:
included_switches.append(switch)
print("'{}' matches {} switches.".format(match, len(included_switches)))
return included_switches
def _sort_switches(switches):
# The lambda returns two values to compare on;
# * The switch number (e77-4) - picks out the number 77
# * The number of the switch in relation to other switches on the same row
# E.g. "e77-4" will return 4
return sorted(switches, key=lambda x: (int(x[1:].split("-")[0]), x.split("-")[1]))
def fetch_gondul_switches(match="^e(.*)"):
# credentials = _generate_credentials()
return _sort_switches(_match_switches(_do_switches_request()))
|