subscribe.py 文件源码

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

项目:yui 作者: item4 项目源码 文件源码
def run(bot, sess):
    subs = sess.query(SiteSub).all()
    for sub in subs:  # type: SiteSub
        async with aiohttp.ClientSession() as session:
            try:
                async with session.get(sub.url) as res:
                    new_body: str = await res.text()
                    if sub.body != new_body:
                        old_body_lines = sub.body.splitlines(keepends=True)
                        new_body_lines = new_body.splitlines(keepends=True)
                        d = Differ()
                        diff = [
                            SPACE_RE.sub(' ', x).rstrip() for x in d.compare(
                                old_body_lines,
                                new_body_lines
                            )
                            if x[0] not in [' ', '?']
                        ]
                        await bot.say(
                            sub.user,
                            '`{}` ?? ??? ?????!\n```\n{}\n```'.format(
                                sub.url,
                                '\n'.join(diff),
                            )
                        )
                        sub.body = new_body
                        with sess.begin():
                            sess.add(sub)
            except aiohttp.client_exceptions.ClientConnectorError:
                await bot.say(
                    sub.user,
                    f'`{sub.url}` ? ??? ? ???!'
                )
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号