Try 3 times to get UID; Fix wrong json

This commit is contained in:
Qin Fandong 2020-10-30 18:49:27 +08:00
parent f551a8ca8e
commit 031fe15260

View File

@ -16,21 +16,21 @@ logging.basicConfig(
class ConfMeta(type):
@property
def index_url(cls):
def index_url(self):
return 'https://webstatic.mihoyo.com/bbs/event/signin-ys/index.html'
@property
def ua(cls):
def ua(self):
return 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0_1 like Mac OS X) ' \
'AppleWebKit/605.1.15 (KHTML, like Gecko) miHoYoBBS/2.1.0'
class Conf(metaclass = ConfMeta):
class Conf(metaclass=ConfMeta):
pass
class UID(object):
def __init__(self, cookie:str = None):
def __init__(self, cookie:str=None):
if type(cookie) is not str:
raise TypeError("%s want a %s but got %s" %(
self.__class__, type(__name__), type(cookie)))
@ -53,7 +53,6 @@ class UID(object):
}
def get_uid(self):
# XXX Loop this code for try N times ?
try:
jdict = json.loads(
requests.Session().get(
@ -69,21 +68,40 @@ class UID(object):
class Sign(object):
def __init__(self, cookie:str = None):
def __init__(self, cookie:str=None):
if type(cookie) is not str:
raise TypeError("%s want a %s but got %s" %(
self.__class__, type(__name__), type(cookie)))
self._url = 'https://api-takumi.mihoyo.com/event/bbs_sign_reward/sign'
uid = UID(cookie)
errstr = None
for i in range(1, 4):
try:
self._uid = uid.get_uid()
except HTTPError as e:
logging.error("HTTP error when get UID, retry %s times ..." %(i))
logging.error("error is %s" %(e))
errstr = str(e)
continue
except KeyError as e:
logging.error("Wrong response to get UID, retry %s times ..." %(i))
logging.error("response is %s" %(e))
errstr = str(e)
continue
except Exception as e:
logging.error("Unknown error %s, die" %(e))
errstr = str(e)
raise
else:
break
try:
self._uid = UID(cookie).get_uid()
except KeyError as e:
logging.error("get uid failed, request is %s" %(e))
raise
except Exception as e:
logging.error("Unknown error %s" %(e))
raise
self._uid
except AttributeError:
raise Exception(errstr)
self._cookie = cookie
@ -103,7 +121,7 @@ class Sign(object):
}
def run(self):
logging.info('UID is %s' %(str(self._uid).replace(str(self._uid)[4:8],'****',1)))
logging.info('UID is %s' %(str(self._uid).replace(str(self._uid)[3:6],'***',1)))
data = {
'act_id': 'e202009291139501',
@ -114,19 +132,20 @@ class Sign(object):
try:
jdict = json.loads(requests.Session().post(
self._url, headers = self.get_header(),
data = json.dumps(data)).text)
data = json.dumps(data, ensure_ascii=False)).text)
except Exception as e:
raise
return jdict
def makeResult(result:str, data = None):
def makeResult(result:str, data=None):
return json.dumps(
{
'result': result,
'message': data
},
sort_keys=False, indent=2
{
'result': result,
'message': data
},
sort_keys=False, indent=2, ensure_ascii=False
)
@ -138,7 +157,7 @@ if __name__ == "__main__":
try:
jdict = Sign(input().strip()).run()
jstr = str(jdict)
jstr = json.dumps(jdict, ensure_ascii=False)
code = jdict['retcode']
except Exception as e:
jstr = str(e)
@ -155,4 +174,5 @@ if __name__ == "__main__":
if code in [0, -5003]:
result = makeResult('Success', jstr)
logging.info(result.encode('utf-8').decode('unicode_escape'))
logging.info(result)