88 lines
2.9 KiB
Python
88 lines
2.9 KiB
Python
import requests
|
|
import re
|
|
import json
|
|
import time
|
|
import random
|
|
class bobbySession(requests.Session):
|
|
def __init__(self,account:str,password:str) -> None:
|
|
super().__init__()
|
|
self.account = account
|
|
self.password = password
|
|
try:
|
|
self.Refresh()
|
|
except:
|
|
raise SystemError("无法连接到bobby皮肤站")
|
|
|
|
def Refresh(self):
|
|
self.__XSRF_COOKIE_Init()
|
|
self.__Login(self.account,self.password)
|
|
def __XSRF_COOKIE_Init(self) :
|
|
url = r"https://www.bobbyskin.com/auth/login"
|
|
html = self.get(url)
|
|
# 正则查找
|
|
pattern = r'<meta name="csrf-token"([^<^>^]*)>'
|
|
txt = html.text
|
|
pattern2 = r'(?<=content=)"[^"]*"'
|
|
res1 = re.search(pattern,txt)
|
|
if res1 is None:
|
|
raise ValueError("cant find xsrf token")
|
|
res2 = re.search(pattern2,res1[0])
|
|
if res2 is None :
|
|
raise ValueError("cant find xsrf token")
|
|
self.headers['X-Csrf-Token'] = res2[0].replace('"',"")
|
|
return
|
|
|
|
# 登录初始化
|
|
def __Login(self,account:str,password:str):
|
|
body = {
|
|
"identification" : account,
|
|
"keep" : "true",
|
|
"password" : password
|
|
}
|
|
resp = self.post(r"https://www.bobbyskin.com/auth/login",json=body)
|
|
|
|
|
|
# 生成邀请码
|
|
def __GenerateInvite (self) :
|
|
xsrf = self.headers.get("X-Csrf-Token")
|
|
if xsrf is None :
|
|
raise requests.RequestException("XSRF expired or unavailable")
|
|
body = {
|
|
"_token" : xsrf,
|
|
"description" : time.strftime("%Y%m%d%H%M%S")
|
|
}
|
|
res = self.post(r"https://www.bobbyskin.com/admin/invitation-codes/generate",data=body)
|
|
# print(res.text,res.status_code)
|
|
|
|
# 查找可用邀请码
|
|
def __getInvite(self)->list[str]:
|
|
url = "https://www.bobbyskin.com/admin/invitation-codes"
|
|
tablePattern = r'<tbody>((?!</tbody>)[\S\s])*</tbody>'
|
|
codePattern = r'(?<=<td>)([a-zA-Z0-9])+(?=</td>)'
|
|
|
|
html = self.get(url).text
|
|
table = re.search(tablePattern,html)
|
|
if table is None :
|
|
raise ValueError("cant parse html table")
|
|
codes =re.finditer(codePattern,table[0])
|
|
# print (table[0])
|
|
res:list[str] = []
|
|
for i in codes :
|
|
res.append(i.group())
|
|
return res
|
|
def getInviteCode(self,step=0)->str:
|
|
if step == 5:
|
|
raise SystemError("验证码查找失败")
|
|
try :
|
|
codes = self.__getInvite()
|
|
if codes.__len__() == 0:
|
|
self.__GenerateInvite()
|
|
return self.getInviteCode(step+1)
|
|
rand =random.randrange(0,codes.__len__())
|
|
return codes[rand]
|
|
except:
|
|
self.Refresh()
|
|
return self.getInviteCode(step+1)
|
|
|
|
|