本文最后更新于:2024年1月14日 晚上

在成功建立本地 Waline 服务后,我们自己掌控了数据库,可以做更多事情,本文记录基于本地数据库的 Waline 为文章添加评论数统计的方法。

背景

  • 需要 独立部署 Waline 并掌握当前数据库
  • 在独立建立的 PVUV 统计 基础上添加内容
  • 由于我的 Waline 部署在了 MongoDB 上,因此本文以 MongoDB 数据库为例

统计思路

  • MongoDB 中,我们已经可以获取评论数据库,那么在访问页面时,后台实时根据访问链接查询评论数据库对应的记录数量返回即可
  • MongoDB 计数 可以统计指定 url 文档数量

修改 Python 代码

  • PVUV 统计 的Python 后台中,类需要在初始化时和数据库建立链接
1
2
3
4
5
6
7
8
9
def __init__(self):
conter_path = Path(__file__).with_name('conter.json')
self.conter_dict = mt.json_load(conter_path)
try:
conn = MongoClient('127.0.0.1', 27017)
db = conn.admin
self.collection = db['Comment']
except:
self.collection = None
  • 在获得 url 后查询评论数返回
1
2
3
4
5
def get_comment_num(self, sub_url):
if self.collection is None:
return 0
else:
return self.collection.count_documents({'url': sub_url})
  • 统计 pvuv 时顺便统计评论数
1
2
3
4
5
6
7
8
def post_pv(self, sub_url):
url = self.root_url + 'stats?start_at=1350679719687&end_at=1990039038644' + '&url=' + sub_url
res = requests.get(url=url, data=json.dumps({}), headers=self.header)
res_dict = json.loads(res.text)
pv = max(1, res_dict['pageviews']['value'] + self.conter_dict.get(sub_url, 0))
uv = max(1, res_dict['uniques']['value'])
comment_num = self.get_comment_num(sub_url)
return pv, uv, comment_num
  • 在 Flask 路由中加入返回 json 的内容
1
2
3
4
5
@app.route("/poststats", methods=['GET','POST'])
def poststats():
url = request.data.decode('utf8')[1:-1]
pv, uv, comment_num = statis_obj.post_pv(url)
return {'pv': pv, 'uv': uv, 'cn': comment_num}
  • 也就是说访问该链接后返回

    1
    {'pv': 3, 'uv': 1, 'cn': 0}

    之类的信息

修改主题

  • 修改 themes\fluid\layout\_partials\post\meta-top.ejs 文件(在 PVUV 修改基础上)

  • 前端代码加入评论数:

    1
    2
    3
    4
    5
    6
    7
    8
        <span id="vvdpost_container_page_pvuv" style="display: none">
    <i class="iconfont icon-eye" aria-hidden="true"></i>
    <%- views_texts[0] %><span id="vvdpost_value_page_pv">0</span><%- views_texts[1] + '&nbsp&nbsp' %>
    <i class="iconfont icon-users" aria-hidden="true"></i>
    <%- views_texts[0] %><span id="vvdpost_value_page_uv">0</span><%- ' 人&nbsp&nbsp' %>
    <i class="iconfont icon-comment" aria-hidden="true"></i>
    <%- views_texts[0] %><span id="vvdpost_value_page_cn">0</span><%- ' 条' %>
    </span>
  • script 代码获取后端返回数据,解析填入前端代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    if (pvCtn) {
    var pv_ele = document.querySelector('#vvdpost_value_page_pv');
    console.log(pv_ele);
    var uv_ele = document.querySelector('#vvdpost_value_page_uv');
    console.log(uv_ele);
    var cn_ele = document.querySelector('#vvdpost_value_page_cn');
    console.log(cn_ele);
    if (uv_ele && uv_ele) {
    pv_ele.innerText = obj.pv;
    uv_ele.innerText = obj.uv;
    cn_ele.innerText = obj.cn;
    pvCtn.style.display = 'inline';
    }
    }
    }

效果展示

参考资料



文章链接:
https://www.zywvvd.com/notes/hexo/theme/fluid/fluid-waline-comment-num/comment-num/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

Fluid -27- 基于本地 Waline 为文章添加评论数统计
https://www.zywvvd.com/notes/hexo/theme/fluid/fluid-waline-comment-num/comment-num/
作者
Yiwei Zhang
发布于
2022年9月2日
许可协议