· 3 years ago · Dec 13, 2021, 10:50 PM
1"""Aula client"""
2import logging
3import requests
4import time
5import datetime
6import requests
7from bs4 import BeautifulSoup
8import json
9from urllib.parse import urljoin
10from .const import API
11
12_LOGGER = logging.getLogger(__name__)
13
14class Client:
15 def __init__(self, username, password):
16 self._username = username
17 self._password = password
18 self._session = None
19
20 def login(self):
21 self._session = requests.Session()
22 response = self._session.get('https://login.aula.dk/auth/login.php?type=unilogin')
23
24 user_data = { 'username': self._username, 'password': self._password }
25 redirects = 0
26 success = False
27 url = ''
28 while success == False and redirects < 10:
29 html = BeautifulSoup(response.text, 'lxml')
30 url = html.form['action']
31 post_data = {}
32 for input in html.find_all('input'):
33 if(input.has_attr('name') and input.has_attr('value')):
34 post_data[input['name']] = input['value']
35 for key in user_data:
36 if(input.has_attr('name') and input['name'] == key):
37 post_data[key] = user_data[key]
38
39 response = self._session.post(url, data = post_data)
40 if response.url == 'https://www.aula.dk:443/portal/':
41 success = True
42 redirects += 1
43
44 self._profiles = self._session.get(API + "?method=profiles.getProfilesByLogin").json()["data"]["profiles"]
45 self._session.get(API + "?method=profiles.getProfileContext&portalrole=guardian")
46 _LOGGER.debug("LOGIN: " + str(success))
47
48 def update_data(self):
49 is_logged_in = False
50 if self._session:
51 response = self._session.get(API + "?method=profiles.getProfilesByLogin").json()
52 is_logged_in = response["status"]["message"] == "OK"
53
54 _LOGGER.debug("is_logged_ind? " + str(is_logged_in))
55
56 if not is_logged_in:
57 self.login()
58
59 self._institutions = []
60 self._institution_profiles = []
61 self._children = []
62 response = self._session.get(API + "?method=profiles.getProfileContext&portalrole=guardian").json()
63 for institution in response['data']['institutions']:
64 self._institutions.append(institution['institutionCode'])
65 self._institution_profiles.append(institution['institutionProfileId'])
66 for child in institution['children']:
67 self._children.append(child)
68
69 self._daily_overview = {}
70 for i, child in enumerate(self._children):
71 response = self._session.get(API + "?method=presence.getDailyOverview&childIds[]=" + str(child["id"])).json()
72 if len(response["data"]) > 0:
73 self._daily_overview[str(child["id"])] = response["data"][0]
74
75 #TODO: Week plan
76 #total_weeks = 4
77 #now = datetime.datetime.now()
78 #from_date = (now - datetime.timedelta(days = (now).weekday())).strftime("%Y-%m-%d")
79 #to_date = (now + datetime.timedelta(days = (7 * total_weeks) - (now).weekday())).strftime("%Y-%m-%d")
80 #week_plan = requests.get("https://www.aula.dk/api/v11/?method=presence.getPresenceTemplates&filterInstitutionProfileIds[]=" + str(child_id) + "&fromDate=" + from_date + "&toDate=" + to_date, cookies=s.cookies).json()#["data"]["presenceWeekTemplates"][0]
81
82 return True