import requests
import json,uuid,urllib.parse
import user_agent
class hotmail:
    @staticmethod
    def getTokens(email):
     while True:
        headers = {
            "Connection": "keep-alive",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent":str(user_agent.generate_user_agent()),
            "return-client-request-id": "false",
            "client-request-id":str(uuid.uuid4()),
            "x-ms-sso-ignore-sso": "1",
            "correlation-id": str(uuid.uuid4()),
            "x-client-ver": "1.1.0+9e54a0d1",
            "x-client-os": "28",
            "x-client-sku": "MSAL.xplat.android",
            "x-client-src-sku": "MSAL.xplat.android",
            "X-Requested-With": "com.microsoft.outlooklite",}

        params = {
            "client_info": "1",
            "haschrome": "1",
            "login_hint": email,
            "mkt": "en",
            "response_type": "code",
            "client_id": "e9b154d0-7658-433b-bb25-6b8e0a8a7c59",
            "scope": "profile openid offline_access https://outlook.office.com/M365.Access",
            "redirect_uri": "msauth://com.microsoft.outlooklite/fcg80qvoM1YMKJZibjBwQcDfOno%3D"
        }
        url=f"https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?{urllib.parse.urlencode(params)}"
        try:
            res = requests.get(url,headers=headers)	
            cok = res.cookies.get_dict()
            return res.text.split('"urlPost":"')[1].split('",')[0],res.text.split('name=\\"PPFT\\" id=\\"i0327\\" value=\\"')[1].split('\\"')[0],res.url.split('haschrome=1')[0],cok['MSPRequ'],cok['uaid'],cok['RefreshTokenSso'],cok['MSPOK'],cok['OParams']
        except:continue
    @staticmethod
    def login(email,Password):
        host ,h1 ,h2 ,h3, h4 ,h6 ,h7 ,h8  = hotmail.getTokens(email=email)
        if not host and not h1 and not h2 and not h3 and not h4 and not h6 and not h7 and not h8:
            print(None)
            return
        print(host)
        payload  = {
            "i13": "1",
            "login": email,
            "loginfmt": email,
            "type": "11",
            "LoginOptions": "1",
            "lrt": "",
            "lrtPartition": "",
            "hisRegion": "",
            "hisScaleUnit": "",
            "passwd": Password,
            "ps": "2",
            "psRNGCDefaultType": "",
            "psRNGCEntropy": "",
            "psRNGCSLK": "",
            "canary": "",
            "ctx": "",
            "hpgrequestid": "",
            "PPFT": h1,
            "PPSX": "PassportR",
            "NewUser": "1",
            "FoundMSAs": "",
            "fspost": "0",
            "i21": "0",
            "CookieDisclosure": "0",
            "IsFidoSupported": "0",
            "isSignupPost": "0",
            "isRecoveryAttemptPost": "0",
            "i19": "9960"}
        headers = {
            "Host": "login.live.com",
            "Connection": "keep-alive",
            "Content-Length": str(len(payload)),
            "Cache-Control": "max-age=0",
            "Upgrade-Insecure-Requests": "1",
            "Origin": "https://login.live.com",
            "Content-Type": "application/x-www-form-urlencoded",
            "User-Agent":str(user_agent.generate_user_agent()),
            "Sec-Fetch-Site": "same-origin",
            "Sec-Fetch-Mode": "navigate",
            "Sec-Fetch-User": "?1",
            "Sec-Fetch-Dest": "document",
            "Referer": f"{h2}haschrome=1",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "en-US,en;q=0.9",
            "Cookie": f"MSPRequ={h3};uaid={h3}; RefreshTokenSso={h6}; MSPOK={h7}; OParams={h8}; MicrosoftApplicationsTelemetryDeviceId={str(uuid.uuid4())}"}
        response = requests.post(host,data=payload,headers=headers,allow_redirects=False)	
        c=(response.cookies.get_dict())
        if "JSH" in c and "JSHP" in c and "ANON" in c and "WLSSC" in c :
            result = {
            "success": True,
            "login_state": True,
            "Cookie":c,

        }
            print(result)
        else:
            html = response.text
            key = "ServerData ="
            idx = html.find(key)
            if idx == -1:
                raise Exception("ServerData key not found")
            start = html.find("{", idx)
            count = 0
            end = start
            for i in range(start, len(html)):
                if html[i] == "{":
                    count += 1
                elif html[i] == "}":
                    count -= 1
                    if count == 0:
                        end = i + 1
                        break
            server_data_raw = html[start:end]
            server_data = json.loads(server_data_raw)
            result = {
            "success": not server_data.get("fHasError", False),
            "error_code": server_data.get("sErrorCode"),
            "error_text": server_data.get("sErrTxt"),
            "email": server_data.get("sPOST_Username"),
            "login_state": server_data.get("sPOST_PaginatedLoginState")
        }
            print(result)
hotmail.login(input("Email : "),input("Paswerd : "))