import requests, time, re, rsa, json, base64
from urllib import parse

s = requests.Session()

username = ""
password = ""

if(username == "" or password == ""):
    username = input("账号：")
    password = input("密码：")

def main():
    login(username, password)
    rand = str(round(time.time()*1000))
    surl = f'https://api.cloud.189.cn/mkt/userSign.action?rand={rand}&clientType=TELEANDROID&version=8.6.3&model=SM-G930K'
    url  = f'https://m.cloud.189.cn/v2/drawPrizeMarketDetails.action?taskId=TASK_SIGNIN&activityId=ACT_SIGNIN'
    url2 = f'https://m.cloud.189.cn/v2/drawPrizeMarketDetails.action?taskId=TASK_SIGNIN_PHOTOS&activityId=ACT_SIGNIN'
    headers = {
        'User-Agent':'Mozilla/5.0 (Linux; Android 5.1.1; SM-G930K Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.136 Mobile Safari/537.36 Ecloud/8.6.3 Android/22 clientId/355325117317828 clientModel/SM-G930K imsi/460071114317824 clientChannelId/qq proVersion/1.0.6',
        "Referer" : "https://m.cloud.189.cn/zhuanti/2016/sign/index.jsp?albumBackupOpened=1",
        "Host" : "m.cloud.189.cn",
        "Accept-Encoding" : "gzip, deflate",
    }
    response = s.get(surl,headers=headers)
    netdiskBonus = response.json()['netdiskBonus']
    if(response.json()['isSign'] == "false"):
        print(f"未签到，签到获得{netdiskBonus}M空间")
    else:
        print(f"已经签到过了，签到获得{netdiskBonus}M空间")
    headers = {
        'User-Agent':'Mozilla/5.0 (Linux; Android 5.1.1; SM-G930K Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.136 Mobile Safari/537.36 Ecloud/8.6.3 Android/22 clientId/355325117317828 clientModel/SM-G930K imsi/460071114317824 clientChannelId/qq proVersion/1.0.6',
        "Referer" : "https://m.cloud.189.cn/zhuanti/2016/sign/index.jsp?albumBackupOpened=1",
        "Host" : "m.cloud.189.cn",
        "Accept-Encoding" : "gzip, deflate",
    }
    response = s.get(url,headers=headers)
    try:
      if ("errorCode" in response.text):
          print(response.json()['errorCode'])
      elif (response.json().has_key('description')):
          description = response.json()['description']
          print(f"抽奖获得{description}")
    except:
      print(f"抽奖1完成，解析时失败")
          
    try:
      response2 = s.get(url2,headers=headers)
      if ("errorCode" in response2.text):
          print(response.json()['errorCode'])
      elif (response2.json().has_key('description')):
          description = response2.json()['description']
          print(f"抽奖2获得{description}")
    except:
      print(f"抽奖2完成，解析时失败")

BI_RM = list("0123456789abcdefghijklmnopqrstuvwxyz")
def int2char(a):
    return BI_RM[a]

b64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
def b64tohex(a):
    d = ""
    e = 0
    c = 0
    for i in range(len(a)):
        if list(a)[i] != "=":
            v = b64map.index(list(a)[i])
            if 0 == e:
                e = 1
                d += int2char(v >> 2)
                c = 3 & v
            elif 1 == e:
                e = 2
                d += int2char(c << 2 | v >> 4)
                c = 15 & v
            elif 2 == e:
                e = 3
                d += int2char(c)
                d += int2char(v >> 2)
                c = 3 & v
            else:
                e = 0
                d += int2char(c << 2 | v >> 4)
                d += int2char(15 & v)
    if e == 1:
        d += int2char(c << 2)
    return d


def rsa_encode(j_rsakey, string):
    rsa_key = f"-----BEGIN PUBLIC KEY-----\n{j_rsakey}\n-----END PUBLIC KEY-----"
    pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(rsa_key.encode())
    result = b64tohex((base64.b64encode(rsa.encrypt(f'{string}'.encode(), pubkey))).decode())
    return result

def calculate_md5_sign(params):
    return hashlib.md5('&'.join(sorted(params.split('&'))).encode('utf-8')).hexdigest()

def login(username, password):
    url = "https://cloud.189.cn/udb/udb_login.jsp?pageId=1&redirectURL=/main.action"
    r = s.get(url)
    captchaToken = re.findall(r"captchaToken' value='(.+?)'", r.text)[0]
    lt = re.findall(r'lt = "(.+?)"', r.text)[0]
    returnUrl = re.findall(r"returnUrl = '(.+?)'", r.text)[0]
    paramId = re.findall(r'paramId = "(.+?)"', r.text)[0]
    j_rsakey = re.findall(r'j_rsaKey" value="(\S+)"', r.text, re.M)[0]
    s.headers.update({"lt": lt})

    username = rsa_encode(j_rsakey, username)
    password = rsa_encode(j_rsakey, password)
    url = "https://open.e.189.cn/api/logbox/oauth2/loginSubmit.do"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/76.0',
        'Referer': 'https://open.e.189.cn/',
        }
    data = {
        "appKey": "cloud",
        "accountType": '01',
        "userName": f"{{RSA}}{username}",
        "password": f"{{RSA}}{password}",
        "validateCode": "",
        "captchaToken": captchaToken,
        "returnUrl": returnUrl,
        "mailSuffix": "@189.cn",
        "paramId": paramId
        }
    r = s.post(url, data=data, headers=headers, timeout=5)
    if(r.json()['result'] == 0):
        print(r.json()['msg'])
    else:
        print(r.json()['msg'])
    redirect_url = r.json()['toUrl']
    r = s.get(redirect_url)
    return s
    

if __name__ == "__main__":
    main()

[((4, 4, 4, 22), 'requests.Session', 'requests.Session', ({}, {}), '()', False, 'import requests, time, re, rsa, json, base64\n'), ((103, 19, 103, 69), 're.findall', 're.findall', ({(103, 30, 103, 60): '"""captchaToken\' value=\'(.+?)\'"""', (103, 62, 103, 68): 'r.text'}, {}), '("captchaToken\' value=\'(.+?)\'", r.text)', False, 'import requests, time, re, rsa, json, base64\n'), ((104, 9, 104, 44), 're.findall', 're.findall', ({(104, 20, 104, 35): '"""lt = "(.+?)\\""""', (104, 37, 104, 43): 'r.text'}, {}), '(\'lt = "(.+?)"\', r.text)', False, 'import requests, time, re, rsa, json, base64\n'), ((105, 16, 105, 58), 're.findall', 're.findall', ({(105, 27, 105, 49): '"""returnUrl = \'(.+?)\'"""', (105, 51, 105, 57): 'r.text'}, {}), '("returnUrl = \'(.+?)\'", r.text)', False, 'import requests, time, re, rsa, json, base64\n'), ((106, 14, 106, 54), 're.findall', 're.findall', ({(106, 25, 106, 45): '"""paramId = "(.+?)\\""""', (106, 47, 106, 53): 'r.text'}, {}), '(\'paramId = "(.+?)"\', r.text)', False, 'import requests, time, re, rsa, json, base64\n'), ((107, 15, 107, 67), 're.findall', 're.findall', ({(107, 26, 107, 52): '"""j_rsaKey" value="(\\\\S+)\\""""', (107, 54, 107, 60): 'r.text', (107, 62, 107, 66): 're.M'}, {}), '(\'j_rsaKey" value="(\\\\S+)"\', r.text, re.M)', False, 'import requests, time, re, rsa, json, base64\n'), ((15, 21, 15, 32), 'time.time', 'time.time', ({}, {}), '()', False, 'import requests, time, re, rsa, json, base64\n')]