wiki.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:Abb1t 作者: k-freeman 项目源码 文件源码
def run(self):
        while True:
            msg = self.queue_in.get()  # get() is blocking
            match = re.search(r'^(?:/|!)wiki (.*)$', msg.get_text().lower())
            if match:
                reply = ""
                try:
                    related_entries = wikipedia.search(match.group(1))
                    w = wikipedia.page(match.group(1))
                    reply1 = "*{}*\n".format(w.title)
                    reply2 = "{}\n".format(w.summary)
                    reply3 = "\n*related topics*:\n- {}".format("\n- ".join(related_entries))

                    if len(reply1+reply2+reply3)>4096:
                        reply = reply1 + reply2[:4092-len(reply1)-len(reply3)]+"...\n" + reply3 # shortening to 4096 characters
                    else:
                        reply = reply1+reply2+reply3
                except wikipedia.DisambiguationError as e:
                    related_entries = str(e).split(":",1)[1].split("\n")[1:]
                    reply = "This was too inspecific. Choose one from these:\n- {}".format("\n- ".join(related_entries))
                except:
                    reply = "No matches returned for this request."
                if reply:
                    self.bot.sendMessage(msg.get_chat_id(), reply, parse_mode="Markdown")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号