refactor: code update

This commit is contained in:
y1ndan 2020-12-27 19:45:21 +08:00
parent c682c5186b
commit 14f000d9d7

View File

@ -18,8 +18,19 @@ logging.basicConfig(
class ConfMeta(type):
@property
def index_url(self):
return 'https://webstatic.mihoyo.com/bbs/event/signin-ys/index.html'
def ref_url(self):
return 'https://webstatic.mihoyo.com/bbs/event/signin-ys/index.html?' \
'bbs_auth_required={}&act_id={}&utm_source={}&utm_medium={}&' \
'utm_campaign={}'.format('true', self.act_id, 'bbs', 'mys', 'icon')
@property
def role_url(self):
return 'https://api-takumi.mihoyo.com/binding/api/' \
'getUserGameRolesByCookie?game_biz={}'.format('hk4e_cn')
@property
def sign_url(self):
return 'https://api-takumi.mihoyo.com/event/bbs_sign_reward/sign'
@property
def app_version(self):
@ -27,9 +38,12 @@ class ConfMeta(type):
@property
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/%s' \
%(self.app_version)
return 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0_1 like Mac OS X) Apple' \
'WebKit/605.1.15 (KHTML, like Gecko) miHoYoBBS/{}'.format(self.app_version)
@property
def act_id(self):
return 'e202009291139501'
class Conf(metaclass=ConfMeta):
@ -39,22 +53,14 @@ class Conf(metaclass=ConfMeta):
class Roles(object):
def __init__(self, cookie:str=None):
if type(cookie) is not str:
raise TypeError("%s want a %s but got %s" %(
raise TypeError('%s want a %s but got %s' %(
self.__class__, type(__name__), type(cookie)))
self._cookie = cookie
self._url = "https://api-takumi.mihoyo.com/binding/api/" \
"getUserGameRolesByCookie?game_biz=%s" %('hk4e_cn')
def get_header(self):
actid = 'e202009291139501'
ref = "%s?bbs_auth_required=%s&act_id=%s&utm_source=%s" \
"&utm_medium=%s&utm_campaign=%s" %(
Conf.index_url, 'true', actid, 'bbs', 'mys', 'icon')
self._cookie = cookie
return {
'User-Agent': Conf.ua,
'Referer': ref,
'Referer': Conf.ref_url,
'Accept-Encoding': 'gzip, deflate, br',
'Cookie': self._cookie
}
@ -63,7 +69,7 @@ class Roles(object):
try:
jdict = json.loads(
requests.Session().get(
self._url, headers = self.get_header()).text)
Conf.role_url, headers = self.get_header()).text)
except Exception as e:
logging.error(e)
raise HTTPError
@ -78,19 +84,19 @@ class Roles(object):
try:
self._rolesInfo = self.get_roles()
except HTTPError as e:
logging.error("HTTP error when get user game roles, " \
"retry %s time(s) ..." %(i))
logging.error("error is %s" %(e))
logging.error('HTTP error when get user game roles, ' \
'retry %s time(s) ...' %(i))
logging.error('error is %s' %(e))
errstr = str(e)
continue
except KeyError as e:
logging.error("Wrong response to get user game roles, " \
"retry %s time(s) ..." %(i))
logging.error("response is %s" %(e))
logging.error('Wrong response to get user game roles, ' \
'retry %s time(s) ...' %(i))
logging.error('response is %s' %(e))
errstr = str(e)
continue
except Exception as e:
logging.error("Unknown error %s, die" %(e))
logging.error('Unknown error %s, die' %(e))
errstr = str(e)
raise
else:
@ -100,38 +106,31 @@ class Roles(object):
self._rolesInfo
except AttributeError:
raise Exception(errstr)
return self._rolesInfo
class Sign(object):
def __init__(self, cookie:str=None):
if type(cookie) is not str:
raise TypeError("%s want a %s but got %s" %(
raise TypeError('%s want a %s but got %s' %(
self.__class__, type(__name__), type(cookie)))
self._cookie = cookie
self._url = 'https://api-takumi.mihoyo.com/event/bbs_sign_reward/sign'
# Provided by Steesha
def md5(self, text):
md5 = hashlib.md5()
md5.update(text.encode())
return (md5.hexdigest())
return md5.hexdigest()
def get_DS(self):
n = self.md5(Conf.app_version)
i = str(int(time.time()))
r = ''.join(random.sample(string.ascii_lowercase + string.digits, 6))
c = self.md5("salt=" + n + "&t="+ i + "&r=" + r)
return i + "," + r + "," + c
c = self.md5('salt=' + n + '&t='+ i + '&r=' + r)
return '{},{},{}'.format(i, r, c)
def get_header(self):
actid = 'e202009291139501'
ref = "%s?bbs_auth_required=%s&act_id=%s&utm_source=%s" \
"&utm_medium=%s&utm_campaign=%s" %(
Conf.index_url, 'true', actid, 'bbs', 'mys', 'icon')
self._cookie = cookie
return {
'x-rpc-device_id': str(uuid.uuid3(
uuid.NAMESPACE_URL, self._cookie)).replace('-','').upper(),
@ -142,7 +141,7 @@ class Sign(object):
'x-rpc-client_type': '5',
'Accept-Encoding': 'gzip, deflate, br',
'User-Agent': Conf.ua,
'Referer': ref,
'Referer': Conf.ref_url,
'x-rpc-app_version': Conf.app_version,
'DS': self.get_DS(),
'Cookie': self._cookie
@ -156,7 +155,7 @@ class Sign(object):
self._uid = rolesList[i]['game_uid']
data = {
'act_id': 'e202009291139501',
'act_id': Conf.act_id,
'region': self._region,
'uid': self._uid
}
@ -165,7 +164,7 @@ class Sign(object):
i+1, str(self._uid).replace(str(self._uid)[3:6],'***',1), self._region_name))
try:
jdict = json.loads(requests.Session().post(
self._url, headers = self.get_header(),
Conf.sign_url, headers = self.get_header(),
data = json.dumps(data, ensure_ascii=False)).text)
except Exception as e:
raise
@ -199,13 +198,13 @@ def notify(key, massage):
except Exception as e:
logging.error(e)
raise HTTPError
return jdict
else:
logging.info('未配置SCKEY,正在跳过推送...')
if __name__ == "__main__":
if __name__ == '__main__':
secret = input().strip().split('#')
secret.append('')
cookie=secret[0]