get cookie automatically
This commit is contained in:
parent
ab64ce071e
commit
e0684cb11e
55
getcookie.py
Normal file
55
getcookie.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import os
|
||||||
|
import pyotp
|
||||||
|
from urllib.parse import urlencode
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
import requests
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
class MIBLogin:
|
||||||
|
def __init__(self):
|
||||||
|
self.session = requests.Session()
|
||||||
|
self.base_url = "https://faisanet.mib.com.mv"
|
||||||
|
self.headers = {
|
||||||
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
|
||||||
|
"Accept": "*/*"
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_login_page(self):
|
||||||
|
response = self.session.get(f"{self.base_url}/auth", headers=self.headers)
|
||||||
|
soup = BeautifulSoup(response.text, 'html.parser')
|
||||||
|
return soup.find('input', {'name': 'rTag'})['value']
|
||||||
|
|
||||||
|
def get_auth_type(self, rtag, username, retain=1):
|
||||||
|
data = {'rTag': rtag, 'pgf01': username, 'retain': retain}
|
||||||
|
headers = {**self.headers, 'Content-Type': 'application/x-www-form-urlencoded'}
|
||||||
|
response = self.session.post(f"{self.base_url}/aAuth/getAuthType", headers=headers, data=urlencode(data))
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
def login(self, username, password, retain=1):
|
||||||
|
rtag = self.get_login_page()
|
||||||
|
self.get_auth_type(rtag, username, retain)
|
||||||
|
|
||||||
|
data = {'rTag': rtag, 'pgf01': username, 'pgf02': password, 'retain': retain}
|
||||||
|
headers = {**self.headers, 'Content-Type': 'application/x-www-form-urlencoded'}
|
||||||
|
self.session.post(f"{self.base_url}/aAuth", headers=headers, data=urlencode(data))
|
||||||
|
|
||||||
|
def auth_2fa(self, totp_seed):
|
||||||
|
self.session.get(f"{self.base_url}/auth2FA", headers=self.headers)
|
||||||
|
totp = pyotp.TOTP(totp_seed)
|
||||||
|
|
||||||
|
data = {'otpType': 3, 'otp': totp.now()}
|
||||||
|
headers = {**self.headers, 'Content-Type': 'application/x-www-form-urlencoded'}
|
||||||
|
self.session.post(f"{self.base_url}/aAuth2FA/verifyOTP", headers=headers, data=urlencode(data))
|
||||||
|
|
||||||
|
return {cookie.name: cookie.value for cookie in self.session.cookies if cookie.name in ['IBSID', 'ql_0']}
|
||||||
|
|
||||||
|
def main():
|
||||||
|
load_dotenv()
|
||||||
|
mib = MIBLogin()
|
||||||
|
mib.login(os.getenv('USERNAME'), os.getenv('PASSWORD'))
|
||||||
|
cookies = mib.auth_2fa(os.getenv('TOTP_SEED'))
|
||||||
|
for name in ['IBSID', 'ql_0']:
|
||||||
|
print(f"{name}={cookies.get(name, '')}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
x
Reference in New Issue
Block a user