nonebot2/commands/todayinhistory.py

30 lines
979 B
Python
Raw Normal View History

2016-12-03 22:41:12 +08:00
import requests
from lxml import etree
from command import CommandRegistry
from commands import core
__registry__ = cr = CommandRegistry()
@cr.register('today_in_history', '历史上的今天')
def today_in_history(_, ctx_msg):
resp = requests.get('http://tool.lu/todayonhistory/')
ok = False
if resp.status_code == 200:
2016-12-04 15:57:43 +08:00
core.echo('历史上的今天:', ctx_msg)
2016-12-03 22:41:12 +08:00
html = etree.HTML(resp.text)
li_elems = html.xpath('//ul[@id="tohlis"]/li')
# reply = reduce(lambda x, y: x.text + '\n' + y.text, li_elems)
step = 10
for start in range(0, len(li_elems), step):
reply = ''
for item in li_elems[start:start + step]:
reply += item.text + '\n'
reply = reply.rstrip()
core.echo(reply, ctx_msg)
core.echo('以上~', ctx_msg)
ok = True
if not ok:
core.echo('很抱歉,网络出错了……建议等会儿再试吧~', ctx_msg)