pl 增加api超时提示以及代码逻辑优化 (#193)

This commit is contained in:
lowking 2021-11-02 23:43:39 +08:00 committed by GitHub
parent 2c070164e3
commit 2da0ef0959
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

View File

@ -482,7 +482,7 @@
}, },
{ {
"name": "pl", "name": "pl",
"version": "1.02", "version": "1.03",
"section": "chat", "section": "chat",
"maintainer": "lowking", "maintainer": "lowking",
"size": "4.0 kb", "size": "4.0 kb",

18
pl.py
View File

@ -26,36 +26,42 @@ async def pl(context):
await context.edit(f"请先安装依赖:\n`{executable} -m pip install bs4`\n随后,请重启 pagermaid。") await context.edit(f"请先安装依赖:\n`{executable} -m pip install bs4`\n随后,请重启 pagermaid。")
return return
action = context.arguments.split() action = context.arguments.split()
status = False
if len(action) == 1: if len(action) == 1:
await context.edit("查询中 . . .") await context.edit("查询中 . . .")
status = False
st = action[0] st = action[0]
st = st.encode('gb2312') st = st.encode('gb2312')
m = {'tj_so': st, } m = {'tj_so': st, }
s = parse.urlencode(m) s = parse.urlencode(m)
for _ in range(3): # 最多重试3 for _ in range(2): # 最多重试2
try: try:
plhtml = get(f"http://www.gd2063.com/pl/?{s}") try:
plhtml = get(f"http://www.gd2063.com/pl/?{s}", timeout=5)
except:
await context.edit("api请求异常请访问\nhttp://www.gd2063.com/\n查看是否正常")
status = True
break
htmlStr = plhtml.content.decode("gbk") htmlStr = plhtml.content.decode("gbk")
soup = BeautifulSoup(htmlStr, 'html.parser') soup = BeautifulSoup(htmlStr, 'html.parser')
arr = soup.find_all(name='a', attrs={"class": "heise"}, limit=10) arr = soup.find_all(name='a', attrs={"class": "heise"}, limit=10)
result = "" result = ""
for a in arr: for a in arr:
if (a.text != None): if a.text is not None and a.text != "":
txt = a.text.replace("嘌呤含量", "") txt = a.text.replace("嘌呤含量", "")
result = f"{result}{txt}\n" result = f"{result}{txt}\n"
status = True
if result == "": if result == "":
await context.edit("没有查到结果") await context.edit("没有查到结果")
else: else:
await context.edit(result) await context.edit(result)
status = True
break break
except: except:
await sleep(5)
pass pass
if not status: if not status:
await context.edit(f"呜呜呜,试了3次都没查到呢") await context.edit(f"呜呜呜,试了2次都没查到呢")
else: else:
await context.edit(f"乱写什么东西呀!格式如下:\n" await context.edit(f"乱写什么东西呀!格式如下:\n"
f"【-pl 食物名】查询食物嘌呤含量") f"【-pl 食物名】查询食物嘌呤含量")