zhenxun_bot/plugins/search_buff_skin_price/data_source.py
2021-11-04 16:11:50 +08:00

54 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from utils.user_agent import get_user_agent
import aiohttp
from asyncio.exceptions import TimeoutError
from configs.config import Config
url = "https://buff.163.com/api/market/goods"
async def get_price(dname):
cookie = {"session": Config.get_config("search_buff_skin_price", "COOKIE")}
name_list = []
price_list = []
parameter = {"game": "csgo", "page_num": "1", "search": dname}
try:
async with aiohttp.ClientSession(
cookies=cookie, headers=get_user_agent()
) as session:
async with session.get(
url, proxy=Config.get_config("search_buff_skin_price", "BUFF_PROXY"), params=parameter, timeout=5
) as response:
if response.status == 200:
try:
if str(await response.text()).find("Login Required") != -1:
return "BUFF登录被重置请联系管理员重新登入", 996
data = (await response.json())["data"]
total_page = data["total_page"]
data = data["items"]
for _ in range(total_page):
for i in range(len(data)):
name = data[i]["name"]
price = data[i]["sell_reference_price"]
name_list.append(name)
price_list.append(price)
except Exception:
return "没有查询到...", 998
else:
return "访问失败!", response.status
except TimeoutError:
return "访问超时! 请重试或稍后再试!", 997
result = f"皮肤: {dname}({len(name_list)})\n"
for i in range(len(name_list)):
result += name_list[i] + ": " + price_list[i] + "\n"
return result[:-1], 999
def update_buff_cookie(cookie: str) -> str:
Config.set_config("search_buff_skin_price", "COOKIE", cookie)
return "更新cookie成功"
if __name__ == "__main__":
print(get_price("awp 二西莫夫"))