diff options
| author | Dylan Bolger <dylan.bolger00@gmail.com> | 2023-10-15 11:34:02 -0500 |
|---|---|---|
| committer | Dylan Bolger <dylan.bolger00@gmail.com> | 2023-10-15 11:34:02 -0500 |
| commit | b5d1b9378adfd8337b3a949b3565b989e0f07db4 (patch) | |
| tree | 729f7e6ed4261a36c6576e7a9cad09da1fdee4e8 /login.py | |
| download | city-utilities-restful-wrapper-b5d1b9378adfd8337b3a949b3565b989e0f07db4.tar.xz city-utilities-restful-wrapper-b5d1b9378adfd8337b3a949b3565b989e0f07db4.zip | |
Initial commit: RESTful API to grab energy usage
Diffstat (limited to 'login.py')
| -rw-r--r-- | login.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/login.py b/login.py new file mode 100644 index 0000000..67f4f67 --- /dev/null +++ b/login.py @@ -0,0 +1,53 @@ +import requests +import re +from constants import loginPageHeaders, loginRequestJson, genericRequestHeaders, loginPageUri, loginRequestEndpoint + +def login(): + # Grab generated session keys from viewing the webpage + aga, asi, ct = grabRequiredKeys() + # Perform a login request using keys found on the page and JSON data (credentials) + lt = performLoginRequest(aga, asi, ct) + # Return the keys required to make endpoint calls + return { + "lt": lt, + "aga": aga, + "asi": asi, + "ct": ct + } + + +def grabRequiredKeys(): + loginPageResponse = requests.get(loginPageUri, headers=loginPageHeaders) + affinityMatcher = re.compile("CORS=(.+?);") + affinityResults = affinityMatcher.search(loginPageResponse.headers['Set-Cookie']) + appGatewayAffinity = affinityResults.group(1) + + aspNetSessionIdMatcher = re.compile("ASP\.NET\_SessionId=(.+?);") + aspnetResults = aspNetSessionIdMatcher.search(loginPageResponse.headers['Set-Cookie']) + aspNetSessionId = aspnetResults.group(1) + + csrfMatcher = re.compile("id=\"hdnCSRFToken\" value=\"(.+)\"") + csrfResults = csrfMatcher.search(loginPageResponse.text) + csrfToken = csrfResults.group(1) + return appGatewayAffinity, aspNetSessionId, csrfToken + +def performLoginRequest(appGatewayAffinity, aspNetSessionId, csrfToken): + loginRequestCookies = { + 'ApplicationGatewayAffinityCORS': appGatewayAffinity, + 'ApplicationGatewayAffinity': appGatewayAffinity, + 'ASP.NET_SessionId': aspNetSessionId, + } + + genericRequestHeaders['csrftoken'] = csrfToken + + loginResponse = requests.post( + loginRequestEndpoint, + cookies=loginRequestCookies, + headers=genericRequestHeaders, + json=loginRequestJson, + ) + + scpMatcher = re.compile("SCP=(.{36});") + scpSearchResults = scpMatcher.search(loginResponse.headers['Set-Cookie']) + loginToken = scpSearchResults.group(1) + return loginToken |
