|
|
@ -125,7 +125,7 @@ class MediaWikiApi:
|
|
|
|
|
|
|
|
|
|
|
|
return ret
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
async def is_logged_in(self,):
|
|
|
|
async def is_logged_in(self):
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
params = {
|
|
|
|
params = {
|
|
|
|
"action": "query",
|
|
|
|
"action": "query",
|
|
|
@ -157,6 +157,10 @@ class MediaWikiApi:
|
|
|
|
return data["query"]["tokens"][token_type + "token"]
|
|
|
|
return data["query"]["tokens"][token_type + "token"]
|
|
|
|
|
|
|
|
|
|
|
|
async def robot_login(self, username: str, password: str):
|
|
|
|
async def robot_login(self, username: str, password: str):
|
|
|
|
|
|
|
|
if await self.is_logged_in():
|
|
|
|
|
|
|
|
self.login_time = time.time()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
token = await self.get_token("login")
|
|
|
|
token = await self.get_token("login")
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
post_data = {
|
|
|
|
post_data = {
|
|
|
@ -173,7 +177,7 @@ class MediaWikiApi:
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
|
|
|
|
|
|
|
|
if "result" not in data["login"] or data["login"]["result"] != "Success":
|
|
|
|
if "result" not in data["login"] or data["login"]["result"] != "Success":
|
|
|
|
raise MediaWikiApiException("Login failed")
|
|
|
|
raise MediaWikiApiException("Login failed", data["login"]["result"])
|
|
|
|
|
|
|
|
|
|
|
|
self.login_time = time.time()
|
|
|
|
self.login_time = time.time()
|
|
|
|
self.login_identity = {
|
|
|
|
self.login_identity = {
|
|
|
@ -191,12 +195,24 @@ class MediaWikiApi:
|
|
|
|
print("刷新MW机器人账号登录状态")
|
|
|
|
print("刷新MW机器人账号登录状态")
|
|
|
|
return await self.robot_login(self.login_identity["username"], self.login_identity["password"])
|
|
|
|
return await self.robot_login(self.login_identity["username"], self.login_identity["password"])
|
|
|
|
|
|
|
|
|
|
|
|
async def chat_complete_user_info(self, user_id: int):
|
|
|
|
async def search_title(self, keyword: str) -> list[str]:
|
|
|
|
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
|
|
|
|
params = {
|
|
|
|
|
|
|
|
"action": "opensearch",
|
|
|
|
|
|
|
|
"search": keyword,
|
|
|
|
|
|
|
|
"namespace": 0,
|
|
|
|
|
|
|
|
"format": "json",
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async with session.get(self.api_url, params=params, proxy=config.REQUEST_PROXY) as resp:
|
|
|
|
|
|
|
|
data = await resp.json()
|
|
|
|
|
|
|
|
return data[1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def ai_toolbox_get_user_info(self, user_id: int):
|
|
|
|
await self.refresh_login()
|
|
|
|
await self.refresh_login()
|
|
|
|
|
|
|
|
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
params = {
|
|
|
|
params = {
|
|
|
|
"action": "chatcompletebot",
|
|
|
|
"action": "aitoolboxbot",
|
|
|
|
"method": "userinfo",
|
|
|
|
"method": "userinfo",
|
|
|
|
"userid": user_id,
|
|
|
|
"userid": user_id,
|
|
|
|
"format": "json",
|
|
|
|
"format": "json",
|
|
|
@ -210,14 +226,14 @@ class MediaWikiApi:
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
|
|
|
|
|
|
|
|
return data["chatcompletebot"]["userinfo"]
|
|
|
|
return data["aitoolboxbot"]["userinfo"]
|
|
|
|
|
|
|
|
|
|
|
|
async def chat_complete_get_point_cost(self, user_id: int, user_action: str, tokens: Optional[int] = None, extractlines: Optional[int] = None) -> ChatCompleteGetPointUsageResponse:
|
|
|
|
async def ai_toolbox_get_point_cost(self, user_id: int, user_action: str, tokens: Optional[int] = None, extractlines: Optional[int] = None) -> ChatCompleteGetPointUsageResponse:
|
|
|
|
await self.refresh_login()
|
|
|
|
await self.refresh_login()
|
|
|
|
|
|
|
|
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
post_data = {
|
|
|
|
post_data = {
|
|
|
|
"action": "chatcompletebot",
|
|
|
|
"action": "aitoolboxbot",
|
|
|
|
"method": "reportusage",
|
|
|
|
"method": "reportusage",
|
|
|
|
"step": "check",
|
|
|
|
"step": "check",
|
|
|
|
"userid": int(user_id) if user_id is not None else None,
|
|
|
|
"userid": int(user_id) if user_id is not None else None,
|
|
|
@ -235,15 +251,15 @@ class MediaWikiApi:
|
|
|
|
print(data)
|
|
|
|
print(data)
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
|
|
|
|
|
|
|
|
point_cost = int(data["chatcompletebot"]["reportusage"]["pointcost"] or 0)
|
|
|
|
point_cost = int(data["aitoolboxbot"]["reportusage"]["pointcost"] or 0)
|
|
|
|
return ChatCompleteGetPointUsageResponse(point_cost=point_cost)
|
|
|
|
return ChatCompleteGetPointUsageResponse(point_cost=point_cost)
|
|
|
|
|
|
|
|
|
|
|
|
async def chat_complete_start_transaction(self, user_id: int, user_action: str, tokens: Optional[int] = None, extractlines: Optional[int] = None) -> ChatCompleteReportUsageResponse:
|
|
|
|
async def ai_toolbox_start_transaction(self, user_id: int, user_action: str, tokens: Optional[int] = None, extractlines: Optional[int] = None) -> ChatCompleteReportUsageResponse:
|
|
|
|
await self.refresh_login()
|
|
|
|
await self.refresh_login()
|
|
|
|
|
|
|
|
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
post_data = {
|
|
|
|
post_data = {
|
|
|
|
"action": "chatcompletebot",
|
|
|
|
"action": "aitoolboxbot",
|
|
|
|
"method": "reportusage",
|
|
|
|
"method": "reportusage",
|
|
|
|
"step": "start",
|
|
|
|
"step": "start",
|
|
|
|
"userid": int(user_id) if user_id is not None else None,
|
|
|
|
"userid": int(user_id) if user_id is not None else None,
|
|
|
@ -263,17 +279,17 @@ class MediaWikiApi:
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
|
|
|
|
|
|
|
|
point_cost = int(data["chatcompletebot"]["reportusage"]["pointcost"] or 0)
|
|
|
|
point_cost = int(data["aitoolboxbot"]["reportusage"]["pointcost"] or 0)
|
|
|
|
return ChatCompleteReportUsageResponse(point_cost=point_cost,
|
|
|
|
return ChatCompleteReportUsageResponse(point_cost=point_cost,
|
|
|
|
transaction_id=data["chatcompletebot"]["reportusage"]["transactionid"])
|
|
|
|
transaction_id=data["aitoolboxbot"]["reportusage"]["transactionid"])
|
|
|
|
|
|
|
|
|
|
|
|
async def chat_complete_end_transaction(self, transaction_id: str, tokens: Optional[int] = None):
|
|
|
|
async def ai_toolbox_end_transaction(self, transaction_id: str, tokens: Optional[int] = None):
|
|
|
|
await self.refresh_login()
|
|
|
|
await self.refresh_login()
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
post_data = {
|
|
|
|
post_data = {
|
|
|
|
"action": "chatcompletebot",
|
|
|
|
"action": "aitoolboxbot",
|
|
|
|
"method": "reportusage",
|
|
|
|
"method": "reportusage",
|
|
|
|
"step": "end",
|
|
|
|
"step": "end",
|
|
|
|
"transactionid": transaction_id,
|
|
|
|
"transactionid": transaction_id,
|
|
|
@ -288,17 +304,17 @@ class MediaWikiApi:
|
|
|
|
if "error" in data:
|
|
|
|
if "error" in data:
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
|
|
|
|
|
|
|
|
return data["chatcompletebot"]["reportusage"]["success"]
|
|
|
|
return data["aitoolboxbot"]["reportusage"]["success"]
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
print(e, file=sys.stderr)
|
|
|
|
print(e, file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
async def chat_complete_cancel_transaction(self, transaction_id: str, error: Optional[str] = None):
|
|
|
|
async def ai_toolbox_cancel_transaction(self, transaction_id: str, error: Optional[str] = None):
|
|
|
|
await self.refresh_login()
|
|
|
|
await self.refresh_login()
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
|
|
|
|
post_data = {
|
|
|
|
post_data = {
|
|
|
|
"action": "chatcompletebot",
|
|
|
|
"action": "aitoolboxbot",
|
|
|
|
"method": "reportusage",
|
|
|
|
"method": "reportusage",
|
|
|
|
"step": "cancel",
|
|
|
|
"step": "cancel",
|
|
|
|
"transactionid": transaction_id,
|
|
|
|
"transactionid": transaction_id,
|
|
|
@ -313,6 +329,6 @@ class MediaWikiApi:
|
|
|
|
if "error" in data:
|
|
|
|
if "error" in data:
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
|
|
|
|
|
|
|
|
return data["chatcompletebot"]["reportusage"]["success"]
|
|
|
|
return data["aitoolboxbot"]["reportusage"]["success"]
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
print(e, file=sys.stderr)
|
|
|
|
print(e, file=sys.stderr)
|