Skip to content
This repository has been archived by the owner on Aug 29, 2021. It is now read-only.

Commit

Permalink
login automatically when a request is made
Browse files Browse the repository at this point in the history
  • Loading branch information
NCPlayz committed Oct 18, 2020
1 parent 55e4700 commit cb6aa6e
Showing 1 changed file with 15 additions and 33 deletions.
48 changes: 15 additions & 33 deletions classcharts/student/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ def __init__(self, code, date_of_birth):
self.account_disabled = False
self.announcements = 0

async def _request(self, verb, endpoint, *, params={}, data={}, headers={}):
async def _request(self, verb, endpoint, *, params={}, data={}, headers={}, with_credentials=True):
root = 'https://www.classcharts.com/'

if not self.session:
await self.login()

if with_credentials:
headers['Authorization'] = 'Basic {}'.format(self._session_id)

async with self.session.request(verb, root + endpoint, params=params, data=data, headers=headers) as response:
try:
data = await response.json()
Expand All @@ -44,7 +50,7 @@ async def login(self):
'recaptcha-token': 'no-token-available'
})

await self._request('POST', 'student/login', data=form)
await self._request('POST', 'student/login', data=form, with_credentials=False)

for cookie in self.session.cookie_jar:
if cookie.key == 'student_session_credentials':
Expand All @@ -54,20 +60,14 @@ async def login(self):
await self.ping()

async def logout(self):
headers = {
'Authorization': 'Basic {}'.format(self._session_id)
}
await self._request("POST", "apiv2student/logout", headers=headers)
await self._request("POST", "apiv2student/logout")
await self.session.close()

async def ping(self):
form = {
'include_data': "true"
}
headers = {
'Authorization': 'Basic {}'.format(self._session_id)
}
data = await self._request('POST', 'apiv2student/ping', data=form, headers=headers)
data = await self._request('POST', 'apiv2student/ping', data=form)

self._session_id = data['meta']['session_id']
user = data['data']['user']
Expand All @@ -91,11 +91,8 @@ async def activity(self, *, after: datetime = None, before: datetime = None):
'from': after.strftime("%Y-%m-%d"),
'to': before.strftime("%Y-%m-%d")
}
headers = {
'Authorization': 'Basic {}'.format(self._session_id)
}

data = await self._request('POST', 'apiv2student/activity/{}'.format(self.id), params=params, headers=headers)
data = await self._request('POST', 'apiv2student/activity/{}'.format(self.id), params=params)

activity = data['data']

Expand All @@ -122,11 +119,8 @@ async def homeworks(self, *, display_date: DisplayDate = DisplayDate.due, after:
'from': after.strftime("%Y-%m-%d"),
'to': before.strftime("%Y-%m-%d")
}
headers = {
'Authorization': 'Basic {}'.format(self._session_id)
}

data = await self._request('POST', 'apiv2student/homeworks/{}'.format(self.id), params=params, headers=headers)
data = await self._request('POST', 'apiv2student/homeworks/{}'.format(self.id), params=params)

homeworks = data['data']

Expand All @@ -147,12 +141,8 @@ async def detentions(self, *, after: datetime = None, before: datetime = None):
'from': after.strftime("%Y-%m-%d"),
'to': before.strftime("%Y-%m-%d")
}
headers = {
'Authorization': 'Basic {}'.format(self._session_id)
}

data = await self._request('POST', 'apiv2student/detentions/{}'.format(self.id), params=params, headers=headers)

data = await self._request('POST', 'apiv2student/detentions/{}'.format(self.id), params=params)
detentions = data['data']

ret = []
Expand All @@ -172,11 +162,7 @@ async def timetable(self, day: datetime = None):
if day:
params["date"] = day.strftime("%Y-%m-%d")

headers = {
'Authorization': 'Basic {}'.format(self._session_id)
}

data = await self._request('POST', 'apiv2student/timetable/{}'.format(self.id), params=params, headers=headers)
data = await self._request('POST', 'apiv2student/timetable/{}'.format(self.id), params=params)

return Timetable(data)

Expand All @@ -195,9 +181,5 @@ async def attendance(self, *, after: datetime = None, before: datetime = None):
if before:
params["before"] = before.strftime("%Y-%m-%d")

headers = {
'Authorization': 'Basic {}'.format(self._session_id)
}

data = await self._request('POST', 'apiv2student/attendance/{}'.format(self.id), params=params, headers=headers)
data = await self._request('POST', 'apiv2student/attendance/{}'.format(self.id), params=params)
return Attendance(data)

0 comments on commit cb6aa6e

Please sign in to comment.