forked from momorientes/pretix_ggroups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretix_ggroups.py
64 lines (49 loc) · 1.91 KB
/
pretix_ggroups.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
#!/usr/bin/env python3
import os
import re
import requests
api_base = "https://pretix.eu/api/v1"
# the google groups mail address
group_mail = "[email protected]"
# pretix event organizer
organizer = "denog"
# pretix event name
event = "denog13"
def main():
results = api_get_all_pages(
f"{api_base}/organizers/{organizer}/events/{event}/orders/"
)
attendee_emails = []
for order in results:
for position in order["positions"]:
if (
position["attendee_email"]
and position["attendee_email"] not in attendee_emails
):
if not position["attendee_email"].endswith("gmail.com"):
attendee_emails.append(position["attendee_email"])
# gmail doesn't allow to subscribe userse with plussed emails to be subscribed to groups, so we remove the plussed part
else:
mail = re.sub(r"\+[^@]+", "", position["attendee_email"])
attendee_emails.append(mail)
print("Group Email [Required],Member Email,Member Type,Member Role")
for mail in attendee_emails:
print(f"{group_mail},{mail},,")
def api_get_all_pages(url: str, results: list = []) -> list:
"""Recursive function to get all pages of an API endpoint
Args:
url: the full URL to get
results: a list of all existing results to return (for recursion)
Returns: All pages results as list
"""
headers = {"Authorization": f"Token {os.environ.get('PRETIX_API_TOKEN')}"}
data = requests.get(url, headers=headers).json()
if data["results"]:
results += data["results"]
if data["next"]: # handle pagination by recursing
api_get_all_pages(data["next"], results=results)
return results
if __name__ == "__main__":
if not os.environ.get("PRETIX_API_TOKEN"):
raise EnvironmentError("No PREITX_API_TOKEN provided")
main()