|
|
@ -6,9 +6,6 @@ from typing import Optional, TypedDict
|
|
|
|
import aiohttp
|
|
|
|
import aiohttp
|
|
|
|
from config import Config
|
|
|
|
from config import Config
|
|
|
|
|
|
|
|
|
|
|
|
mw_api = Config.get("mw.api_endpoint", "https://www.isekai.cn/api.php")
|
|
|
|
|
|
|
|
request_proxy = Config.get("request.proxy", type=str, empty_is_none=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MediaWikiApiException(Exception):
|
|
|
|
class MediaWikiApiException(Exception):
|
|
|
|
def __init__(self, info: str, code: Optional[str] = None) -> None:
|
|
|
|
def __init__(self, info: str, code: Optional[str] = None) -> None:
|
|
|
|
super().__init__(info)
|
|
|
|
super().__init__(info)
|
|
|
@ -55,6 +52,7 @@ class MediaWikiApi:
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def create():
|
|
|
|
def create():
|
|
|
|
|
|
|
|
mw_api = Config.get("mw.api_endpoint", "https://www.isekai.cn/api.php")
|
|
|
|
if MediaWikiApi.instance is None:
|
|
|
|
if MediaWikiApi.instance is None:
|
|
|
|
MediaWikiApi.instance = MediaWikiApi(mw_api)
|
|
|
|
MediaWikiApi.instance = MediaWikiApi(mw_api)
|
|
|
|
|
|
|
|
|
|
|
@ -62,6 +60,7 @@ class MediaWikiApi:
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, api_url: str):
|
|
|
|
def __init__(self, api_url: str):
|
|
|
|
self.api_url = api_url
|
|
|
|
self.api_url = api_url
|
|
|
|
|
|
|
|
self.request_proxy = Config.get("request.proxy", type=str, empty_is_none=True)
|
|
|
|
|
|
|
|
|
|
|
|
self.cookie_jar = aiohttp.CookieJar(unsafe=True)
|
|
|
|
self.cookie_jar = aiohttp.CookieJar(unsafe=True)
|
|
|
|
self.login_identity = None
|
|
|
|
self.login_identity = None
|
|
|
@ -77,7 +76,7 @@ class MediaWikiApi:
|
|
|
|
"titles": title,
|
|
|
|
"titles": title,
|
|
|
|
"inprop": "url"
|
|
|
|
"inprop": "url"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async with session.get(self.api_url, params=params, proxy=request_proxy) as resp:
|
|
|
|
async with session.get(self.api_url, params=params, proxy=self.request_proxy) as resp:
|
|
|
|
data = await resp.json()
|
|
|
|
data = await resp.json()
|
|
|
|
if "error" in data:
|
|
|
|
if "error" in data:
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
@ -99,7 +98,7 @@ class MediaWikiApi:
|
|
|
|
"disabletoc": "true",
|
|
|
|
"disabletoc": "true",
|
|
|
|
"disablelimitreport": "true",
|
|
|
|
"disablelimitreport": "true",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async with session.get(self.api_url, params=params, proxy=request_proxy) as resp:
|
|
|
|
async with session.get(self.api_url, params=params, proxy=self.request_proxy) as resp:
|
|
|
|
data = await resp.json()
|
|
|
|
data = await resp.json()
|
|
|
|
if "error" in data:
|
|
|
|
if "error" in data:
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
@ -115,7 +114,7 @@ class MediaWikiApi:
|
|
|
|
"meta": "siteinfo|userinfo",
|
|
|
|
"meta": "siteinfo|userinfo",
|
|
|
|
"siprop": "general"
|
|
|
|
"siprop": "general"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async with session.get(self.api_url, params=params, proxy=request_proxy) as resp:
|
|
|
|
async with session.get(self.api_url, params=params, proxy=self.request_proxy) as resp:
|
|
|
|
data = await resp.json()
|
|
|
|
data = await resp.json()
|
|
|
|
if "error" in data:
|
|
|
|
if "error" in data:
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
@ -148,7 +147,7 @@ class MediaWikiApi:
|
|
|
|
if start_title is not None:
|
|
|
|
if start_title is not None:
|
|
|
|
params["apfrom"] = start_title
|
|
|
|
params["apfrom"] = start_title
|
|
|
|
|
|
|
|
|
|
|
|
async with session.get(self.api_url, params=params, proxy=request_proxy) as resp:
|
|
|
|
async with session.get(self.api_url, params=params, proxy=self.request_proxy) as resp:
|
|
|
|
data = await resp.json()
|
|
|
|
data = await resp.json()
|
|
|
|
if "error" in data:
|
|
|
|
if "error" in data:
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
@ -172,7 +171,7 @@ class MediaWikiApi:
|
|
|
|
"formatversion": "2",
|
|
|
|
"formatversion": "2",
|
|
|
|
"meta": "userinfo"
|
|
|
|
"meta": "userinfo"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async with session.get(self.api_url, params=params, proxy=request_proxy) as resp:
|
|
|
|
async with session.get(self.api_url, params=params, proxy=self.request_proxy) as resp:
|
|
|
|
data = await resp.json()
|
|
|
|
data = await resp.json()
|
|
|
|
if "error" in data:
|
|
|
|
if "error" in data:
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
@ -188,7 +187,7 @@ class MediaWikiApi:
|
|
|
|
"meta": "tokens",
|
|
|
|
"meta": "tokens",
|
|
|
|
"type": token_type
|
|
|
|
"type": token_type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async with session.get(self.api_url, params=params, proxy=request_proxy) as resp:
|
|
|
|
async with session.get(self.api_url, params=params, proxy=self.request_proxy) as resp:
|
|
|
|
data = await resp.json()
|
|
|
|
data = await resp.json()
|
|
|
|
if "error" in data:
|
|
|
|
if "error" in data:
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
@ -210,7 +209,7 @@ class MediaWikiApi:
|
|
|
|
"lgpassword": password,
|
|
|
|
"lgpassword": password,
|
|
|
|
"lgtoken": token,
|
|
|
|
"lgtoken": token,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async with session.post(self.api_url, data=post_data, proxy=request_proxy) as resp:
|
|
|
|
async with session.post(self.api_url, data=post_data, proxy=self.request_proxy) as resp:
|
|
|
|
data = await resp.json()
|
|
|
|
data = await resp.json()
|
|
|
|
if "error" in data:
|
|
|
|
if "error" in data:
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
@ -242,7 +241,7 @@ class MediaWikiApi:
|
|
|
|
"namespace": 0,
|
|
|
|
"namespace": 0,
|
|
|
|
"format": "json",
|
|
|
|
"format": "json",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async with session.get(self.api_url, params=params, proxy=request_proxy) as resp:
|
|
|
|
async with session.get(self.api_url, params=params, proxy=self.request_proxy) as resp:
|
|
|
|
data = await resp.json()
|
|
|
|
data = await resp.json()
|
|
|
|
return data[1]
|
|
|
|
return data[1]
|
|
|
|
|
|
|
|
|
|
|
@ -257,7 +256,7 @@ class MediaWikiApi:
|
|
|
|
"format": "json",
|
|
|
|
"format": "json",
|
|
|
|
"formatversion": "2",
|
|
|
|
"formatversion": "2",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async with session.get(self.api_url, params=params, proxy=request_proxy) as resp:
|
|
|
|
async with session.get(self.api_url, params=params, proxy=self.request_proxy) as resp:
|
|
|
|
data = await resp.json()
|
|
|
|
data = await resp.json()
|
|
|
|
if "error" in data:
|
|
|
|
if "error" in data:
|
|
|
|
if data["error"]["code"] == "user-not-found":
|
|
|
|
if data["error"]["code"] == "user-not-found":
|
|
|
@ -284,7 +283,7 @@ class MediaWikiApi:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Filter out None values
|
|
|
|
# Filter out None values
|
|
|
|
post_data = {k: v for k, v in post_data.items() if v is not None}
|
|
|
|
post_data = {k: v for k, v in post_data.items() if v is not None}
|
|
|
|
async with session.post(self.api_url, data=post_data, proxy=request_proxy) as resp:
|
|
|
|
async with session.post(self.api_url, data=post_data, proxy=self.request_proxy) as resp:
|
|
|
|
data = await resp.json()
|
|
|
|
data = await resp.json()
|
|
|
|
if "error" in data:
|
|
|
|
if "error" in data:
|
|
|
|
print(data)
|
|
|
|
print(data)
|
|
|
@ -310,7 +309,7 @@ class MediaWikiApi:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Filter out None values
|
|
|
|
# Filter out None values
|
|
|
|
post_data = {k: v for k, v in post_data.items() if v is not None}
|
|
|
|
post_data = {k: v for k, v in post_data.items() if v is not None}
|
|
|
|
async with session.post(self.api_url, data=post_data, proxy=request_proxy) as resp:
|
|
|
|
async with session.post(self.api_url, data=post_data, proxy=self.request_proxy) as resp:
|
|
|
|
data = await resp.json()
|
|
|
|
data = await resp.json()
|
|
|
|
if "error" in data:
|
|
|
|
if "error" in data:
|
|
|
|
if data["error"]["code"] == "noenoughpoints":
|
|
|
|
if data["error"]["code"] == "noenoughpoints":
|
|
|
@ -338,7 +337,7 @@ class MediaWikiApi:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Filter out None values
|
|
|
|
# Filter out None values
|
|
|
|
post_data = {k: v for k, v in post_data.items() if v is not None}
|
|
|
|
post_data = {k: v for k, v in post_data.items() if v is not None}
|
|
|
|
async with session.post(self.api_url, data=post_data, proxy=request_proxy) as resp:
|
|
|
|
async with session.post(self.api_url, data=post_data, proxy=self.request_proxy) as resp:
|
|
|
|
data = await resp.json()
|
|
|
|
data = await resp.json()
|
|
|
|
if "error" in data:
|
|
|
|
if "error" in data:
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
@ -363,7 +362,7 @@ class MediaWikiApi:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Filter out None values
|
|
|
|
# Filter out None values
|
|
|
|
post_data = {k: v for k, v in post_data.items() if v is not None}
|
|
|
|
post_data = {k: v for k, v in post_data.items() if v is not None}
|
|
|
|
async with session.post(self.api_url, data=post_data, proxy=request_proxy) as resp:
|
|
|
|
async with session.post(self.api_url, data=post_data, proxy=self.request_proxy) as resp:
|
|
|
|
data = await resp.json()
|
|
|
|
data = await resp.json()
|
|
|
|
if "error" in data:
|
|
|
|
if "error" in data:
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|
raise MediaWikiApiException(data["error"]["info"], data["error"]["code"])
|
|
|
|