共计 1303 个字符,预计需要花费 4 分钟才能阅读完成。
背景
- 之所以写了个监控脚本,主要是我的博客在国内备了案,而且开放了评论,听说万一评论中有反动言论被举报的话要被请喝茶,一般建议关掉评论,但我觉得关掉评论会失去很多乐趣, 所以开发了这个监控脚本,这个监控脚本核心代码只有几行,参考自 leancloud 的官方 API 文档。我的博客是基于 melody 主题的 hexo 博客,评论插件选择了 valine, 不像 disqus,gitalk 等需要登录,valine 大大降低了评论的门槛,虽然有被滥用的可能。
-
另外, 虽然 valine 官方文档中提到了 ” 在子级对存在邮件地址的父级发表评论时发送邮件 ”, 但经过我的测试并没有邮件发送,此外我需要的不仅仅是子级对父级评论会发邮件而是只要有了新评论都会收到邮件通知,显然 valine 满足不了我了,于是就开发了这个脚本。
环境
- Mac OSX 10.13.6 /ubuntu 18.04.3
- Python 3.6.4+
- leancloud 2.5.0
- APScheduler 3.6.3
使用说明
- 拉完仓库代码后安装脚本依赖的 python 第三方库
$ git clone https://github.com/supersu097/leancloud_notifier.git
$ cd leancloud_notifier && sudo pip3 install -r requirement.txt
修改配置
$ vim core/config.py
sender ='' # 提供 smtp 服务的邮箱,我采用的是 126 的
receiver='' # 接收邮件通知的邮箱
pwd = '' # 提供 smtp 服务的邮箱密码
leancloud_appid='' # leancloud 的应用 appid,可在控制面板的 设置 - 应用 keys 中找到
leancloud_appkey='' # leancloud 的应用 appkey,可在控制面板的 设置 - 应用 keys 中找到
interval=300 # 每隔多少秒执行一下检测是否有新评论,这里默认给了 300 秒
- 使用
$ python3 notifier.py &
运行之后把终端关掉就行了,或者你也可以在 screen 中直接 python3 notifier.py 这样运行。
核心代码
@property
def get_query_obj(self):
leancloud.init(config.leancloud_appid, config.leancloud_appkey)
query = leancloud.Query('Comment')
# 获取最新发布的
query.add_descending('createdAt')
query.limit(1)
return query
first_recent_comment = self.get_query_obj.find()[0]
comment_content = first_recent_comment.get('comment')
comment_id = first_recent_comment.get('objectId')
特色
- 本脚本集成了 python 强大的第三方定时任务库 apscheduler,无需设置 cronjob,不知道为啥,我在 Ubuntu 上从来就没成功跑过一次 cronjob。
正文完
发表至: 其它
2020-01-18