-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
110 lines (83 loc) · 2.99 KB
/
main.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
import os
import re
import requests
from art import tprint
from dotenv import load_dotenv
from tqdm import tqdm
from bs4 import BeautifulSoup
load_dotenv()
api_key = os.getenv("API_KEY")
group = os.getenv("GROUP")
username = os.getenv("USERNAME")
password = os.getenv("PASSWORD")
BASE_URL = "https://db.scout.ch"
ATTRIBUTE_TO_CHECK = "ahv_number"
s = requests.Session()
def get_groupname(group_id):
group = s.get(f"{BASE_URL}/groups/{group_id}.json").json()
return group["groups"][0]["name"]
def get_all_sub_groups(group_id, depth=0):
group = s.get(f"{BASE_URL}/groups/{group_id}.json").json()
group_name = group["groups"][0]["name"]
try:
children = group["groups"][0]["links"]["children"]
except KeyError:
children = []
subgroups = []
for child in (
tqdm(children, desc=f"Fetching subgroups of {group_name}", unit="group")
if depth == 0
else children
):
subgroups.extend(get_all_sub_groups(child, depth + 1))
subgroups.append(group_id)
return list(set(subgroups))
def get_people_id_from_group(group_id):
people = s.get(f"{BASE_URL}/groups/{group_id}/people.json").json()["people"]
return [person["id"] for person in people]
def get_people_details(group_id, person_id):
person = s.get(f"{BASE_URL}/groups/{group_id}/people/{person_id}.json").json()
return person["people"][0]
def name_formatter(nickname, firstname, lastname):
if nickname:
return f"{nickname} / {firstname} {lastname}"
return f"{firstname} {lastname}"
tprint("AHV-Checker")
# Login procedure
login_page = s.get("https://db.scout.ch/de/users/sign_in")
soup = BeautifulSoup(login_page.content, "html.parser")
hidden_tag = soup.find(attrs={"name": "authenticity_token"})
auth_token = hidden_tag.attrs["value"]
login = s.post(
"https://db.scout.ch/de/users/sign_in",
{
"authenticity_token": auth_token,
"person[email]": username,
"person[password]": password,
},
)
groups = get_all_sub_groups(group)
people_ids = []
for group in tqdm(groups, desc=f"Fetching all people ids", unit="group"):
people_ids.extend([(group, p_id) for p_id in get_people_id_from_group(group)])
people_ids = list(set(people_ids)) # Ensure only unique
people = []
for person_id, group_id in tqdm(
people_ids, desc=f"Fetching all peoples info", unit="lookups"
):
people.append(get_people_details(person_id, group_id))
people.sort(key=lambda p: p["last_name"].replace("von ", "") + p["first_name"])
ahv_regex = re.compile("\d{3}.\d{4}.\d{4}.\d{2}")
people_without_ahv = [
person
for person in people
if not (person[ATTRIBUTE_TO_CHECK] and ahv_regex.match(person[ATTRIBUTE_TO_CHECK]))
]
print(f"Mit AHV-Nr: {len(people) - len(people_without_ahv)}")
print(f"Ohne AHV-Nr: {len(people_without_ahv)}")
print(f"Total: {len(people)}")
print()
for p in people_without_ahv:
f_name = name_formatter(p["nickname"], p["first_name"], p["last_name"])
url = p["href"].replace(".json", "")
print(f"{f_name}: {url}")