3分钟上手Miniflux阅读统计导出:从数据到可视化全指南
你是否曾想知道自己在信息海洋中花费了多少时间?作为轻量级News Feed阅读器,Miniflux不仅帮你聚合内容,还悄悄记录着你的阅读行为数据。本文将带你用3个步骤从internal/model/entry.go中提取阅读统计,生成个性化阅读报告。
数据来源解析:Miniflux如何计算阅读时间
Miniflux的阅读时间统计功能源自internal/reader/readingtime/readingtime.go模块,该模块通过分析文章内容自动估算阅读时长。核心算法会区分CJK(中日韩)文字与其他语言,采用不同的计算标准:
// 代码片段来自[internal/reader/readingtime/readingtime.go](https://link.gitcode.com/i/dc6ebc998b9668519f60bd8758f785a5)
func EstimateReadingTime(content string, defaultReadingSpeed, cjkReadingSpeed int) int {
sanitizedContent := sanitizer.StripTags(content)
truncationPoint := min(len(sanitizedContent), 50)
if isCJK(sanitizedContent[:truncationPoint]) {
return int(math.Ceil(float64(utf8.RuneCountInString(sanitizedContent)) / float64(cjkReadingSpeed)))
}
return int(math.Ceil(float64(len(strings.Fields(sanitizedContent))) / float64(defaultReadingSpeed)))
}
这些计算结果最终会存储在Entry模型的ReadingTime字段中(internal/model/entry.go第36行),为统计导出提供基础数据。
导出工具开发:从数据库到CSV
要导出阅读统计,我们需要创建一个简单的Go脚本,通过Miniflux的内部API查询数据。以下是实现核心功能的代码框架:
package main
import (
"encoding/csv"
"log"
"os"
"time"
"miniflux.app/v2/internal/storage"
"miniflux.app/v2/internal/model"
)
func main() {
// 初始化数据库连接
store := storage.NewConnectionPool("postgres://user:password@localhost/miniflux?sslmode=disable")
// 查询指定时间范围内的阅读数据
entries, err := store.Entries(&storage.EntryQuery{
UserID: 1, // 替换为实际用户ID
FromDate: time.Now().AddDate(0, -1, 0), // 过去30天
ToDate: time.Now(),
Status: model.EntryStatusRead,
OrderBy: "published_at",
Direction: "asc",
})
// 导出为CSV文件
file, _ := os.Create("reading_stats.csv")
defer file.Close()
writer := csv.NewWriter(file)
writer.Write([]string{"日期", "标题", "来源", "阅读时间(分钟)", "状态"})
for _, entry := range entries {
writer.Write([]string{
entry.Date.Format("2006-01-02"),
entry.Title,
entry.Feed.Title,
strconv.Itoa(entry.ReadingTime),
entry.Status,
})
}
writer.Flush()
}
报告可视化:从CSV到信息图表
导出CSV后,可使用Python的Matplotlib或Go的gonum/plot生成阅读统计图表。以下是一个简单的Python可视化脚本示例:
import pandas as pd
import matplotlib.pyplot as plt
# 读取CSV数据
df = pd.read_csv('reading_stats.csv')
# 按来源统计阅读时间
feed_stats = df.groupby('来源')['阅读时间(分钟)'].sum().sort_values(ascending=False)
# 生成饼图
plt.figure(figsize=(10, 6))
feed_stats.plot(kind='pie', autopct='%1.1f%%')
plt.title('阅读时间分布(按来源)')
plt.ylabel('')
plt.savefig('reading_distribution.png')
自动化方案:定时导出与报告发送
对于进阶用户,可以利用项目中的contrib/ansible/目录下的自动化工具,设置定时任务自动生成并发送报告。通过Ansible Playbook配置:
# 示例Playbook片段,完整配置参见[contrib/ansible/playbooks/](https://link.gitcode.com/i/ef7d829a8fbc23a61826cab54d130756)
- name: 运行阅读统计导出脚本
hosts: localhost
tasks:
- name: 执行导出脚本
command: go run stats_exporter.go
args:
chdir: /data/web/disk1/git_repo/gh_mirrors/v21/v2
- name: 发送邮件报告
mail:
to: user@example.com
subject: Miniflux阅读周报
body: 附件是您本周的阅读统计报告
attach: /data/web/disk1/git_repo/gh_mirrors/v21/v2/reading_stats.csv
扩展功能:自定义统计维度
通过修改internal/storage/entry_query_builder.go,可以添加更多统计维度,如按标签统计、阅读时段分析等。例如添加标签过滤条件:
// 在EntryQueryBuilder中添加标签过滤
func (q *EntryQueryBuilder) WithTag(tag string) *EntryQueryBuilder {
q.builder.Where("e.id IN (SELECT et.entry_id FROM entry_tags et WHERE et.tag = ?)", tag)
return q
}
部署与使用
完整的导出工具已集成到项目的contrib/目录下,通过以下步骤即可使用:
- 克隆仓库:
git clone https://gitcode.com/gh_mirrors/v21/v2 - 进入工具目录:
cd v2/contrib/stats-exporter - 配置数据库连接:
cp config.example.json config.json - 运行导出命令:
make run
详细使用说明参见contrib/README.md,更多高级配置可参考internal/config/目录下的配置文件模板。
通过这些工具和方法,你可以全面掌握自己的阅读习惯,优化信息消费结构。如需进一步定制报告格式,可修改internal/ui/目录下的模板文件,添加自定义统计图表展示。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



