Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jokas2 committed Feb 22, 2024
0 parents commit 54465b1
Show file tree
Hide file tree
Showing 72 changed files with 14,971 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data/
dist/
node_modules/
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/WechatMsgAnalyzer.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Dragon_yy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
92 changes: 92 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Chat Analysis

WechatMsgAnalyzer 是一个用于分析聊天记录并生成报告的命令行工具。

## 功能特性
- 从 Excel 文件中读取聊天记录数据。
- 分析聊天记录,包括统计发言次数、活跃时间段、关键词频率等。
- 生成 HTML 报告,包含图表和统计数据。

## 使用
1. 使用[WeChatMsg](https://github.com/LC044/WeChatMsg)项目导出微信聊天记录到csv文件
2. 根据聊天记录csv文件生成json
```bash
./WechatMsgAnalyzer_MacOS-1.0 analyze -t test -f test.xlsx -o ./test.json
```
3. 运行serve命令开启后端服务
```bash
./WechatMsgAnalyzer_MacOS-1.0 serve -i 127.0.0.1 -p 9000 -r test.json
```
4. 运行前端项目,需要注意由于我在前端项目中使用了axios请求后端服务,所以需要修改请求地址为后端服务地址
```bash
cd web
npm install
npm run serve
```

## 项目结构
```
/chat-analysis
|-- /cmd
| |-- root.go # 根命令
| |-- analyze.go # 分析并生成报告命令
|
|-- /internal
| |-- /chat
| | |-- analyzer.go # 聊天分析逻辑
| | |-- parser.go # 解析Excel文件内容
| | |-- model.go # 数据模型定义
| |
| |-- /report
| |-- generator.go # 报告生成逻辑
| |-- template.go # HTML报告模板
|
|-- /data
| |-- chat_record.xlsx # Excel聊天记录文件
|
|-- /web
|
|-- go.mod # Go模块文件
|-- go.sum # Go模块依赖版本锁定文件
|-- main.go # 程序主入口
|-- README.md # 项目说明文档
```

## 使用方法
准备聊天记录数据,保存为 Excel 文件。
在命令行中运行 chat-analysis analyze 命令,并指定 Excel 文件路径:
```bash
chat-analysis analyze -f /path/to/your/chat_records.xlsx
```
输入excel包含字段:
- localId: 通常指的是记录在本地数据库中的唯一标识符,用于区分每条消息或通讯记录。
- TalkerId: 可能指的是对话的另一方的标识符,即消息的接收者或发送者的ID。
- Type: 表示消息的类型,这可能包括文本、图片、视频等。
- SubType: 进一步细分消息的类型,例如在文本消息中可能区分是否包含链接或特殊格式。
- IsSender: 标记消息的发送者,通常用布尔值表示(例如,true表示发送者,false表示接收者)。
- CreateTime: 消息创建的时间戳,表明消息发送或接收的具体时间。
- Status: 消息的状态,可能包括已发送、已接收、已读、错误等状态。
- StrContent: 消息的具体内容,以字符串形式表示。
- StrTime: 可能是消息时间的字符串表示形式,用于显示目的。
- Remark: 可能是对消息或通讯记录的额外注释或备注。
- NickName: 通讯双方的昵称或显示名称。
- Sender: 消息发送者的标识符,可能与TalkerId相对应。

程序将会分析聊天记录并生成报告,报告将保存为 HTML 文件。
示例报告
![img.png](img/front.png)

## 技术栈
- Go
- Cobra
- Vue

## 贡献
欢迎提出问题和改进建议!如果你有兴趣贡献代码,请提一个 Pull Request。

## 许可证
GNU 许可证

## 鸣谢
使用了以下开源项目:
- [wechat-report](https://github.com/myth984/wechat-report)
47 changes: 47 additions & 0 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cmd

import (
"fmt"
"github.com/Dragon-yy/WechatMsgAnalyzer/internal/chat"
reportPgk "github.com/Dragon-yy/WechatMsgAnalyzer/internal/report"
"github.com/spf13/cobra"
)

// analyzeCmd represents the analyze command
var analyzeCmd = &cobra.Command{
Use: "analyze",
Short: "Analyze Wechat messages from an Excel file",
Long: `Analyze Wechat messages from an Excel file and generate a report.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("analyze called")
// 获取命令行参数
excelFile := cmd.Flag("file").Value.String()
reportTitle := cmd.Flag("title").Value.String()
outputFile := cmd.Flag("output").Value.String()

// 这里调用internal包的函数
report := chat.AnalyzeChat(reportTitle, excelFile)
//fmt.Println(len(report.TopWords))
//
//fmt.Println(report)
//fmt.Println(report.ActiveTime)
//fmt.Println(report.TopWords)

// 把report json序列化后保存在data/data.json中
reportPgk.GenerateReportData(report, outputFile)

// 生成词云
//reportPgk.GenerateWordCloud(reportTitle, report.TopWords)
},
}

func init() {
analyzeCmd.Flags().StringP("file", "f", "", "The Excel file to be analyzed")
analyzeCmd.MarkFlagRequired("file")
analyzeCmd.Flags().StringP("title", "t", "", "The title of the report")
analyzeCmd.MarkFlagRequired("title")
analyzeCmd.Flags().StringP("output", "o", "data/data.json", "The output file of the report")
analyzeCmd.MarkFlagRequired("output")

RootCmd.AddCommand(analyzeCmd)
}
18 changes: 18 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import "github.com/spf13/cobra"

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "WechatMsgAnalyzer",
Short: "Analyze chat messages from an Excel file",
Long: `Analyze chat messages from an Excel file and generate a report.`,
}

func Execute() error {
return RootCmd.Execute()
}

func init() {
RootCmd.AddCommand(analyzeCmd)
}
84 changes: 84 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cmd

import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
"net/http"
"os"
)

// 创建gin服务,监听端口,返回data.json

var serveCmd = &cobra.Command{
Use: "serve",
Short: "Start the web server",
Long: `Start the web server and serve the report data.`,
Run: func(cmd *cobra.Command, args []string) {
// 获取参数
report := cmd.Flag("report").Value.String()
// 获取port
port := cmd.Flag("port").Value.String()
// 获取IP
ip := cmd.Flag("ip").Value.String()
// 解析报告数据
reportData, err := parseReportData(report)
if err != nil {
fmt.Println("Error parsing report data:", err)
return
}
// 启动服务器
startWebServer(ip, port, reportData)
},
}

func parseReportData(reportPath string) (map[string]interface{}, error) {
// 读取报告数据文件
reportBytes, err := os.ReadFile(reportPath)
if err != nil {
return nil, err
}

// 解析 JSON 数据
var reportData map[string]interface{}
err = json.Unmarshal(reportBytes, &reportData)
if err != nil {
return nil, err
}

return reportData, nil
}

func startWebServer(ip, port string, reportData map[string]interface{}) {
r := gin.Default()
// 设置CORS头
r.Use(func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Accept")
c.Next()
})
// 添加路由
r.GET("/report", func(c *gin.Context) {
c.JSON(http.StatusOK, reportData)
})

// 启动服务器
fmt.Println(fmt.Sprintf("Listening on %s:%s", ip, port))
err := r.Run(fmt.Sprintf("%s:%s", ip, port))
if err != nil {
fmt.Println("Error starting server:", err)
}
}

func init() {
serveCmd.Flags().StringP("report", "r", "data/data.json", "The report data file to be served")
// 添加port
serveCmd.Flags().StringP("port", "p", "9000", "The port to listen on")
// 添加IP
serveCmd.Flags().StringP("ip", "i", "127.0.0.1", "The IP to listen on")
RootCmd.AddCommand(serveCmd)

}
47 changes: 47 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module github.com/Dragon-yy/WechatMsgAnalyzer

go 1.21.0

require (
github.com/go-echarts/go-echarts/v2 v2.3.3
github.com/spf13/cobra v1.8.0
github.com/xuri/excelize/v2 v2.8.0
)

require (
github.com/bytedance/sonic v1.11.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-ego/gse v0.80.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.18.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/vcaesar/cedar v0.20.1 // indirect
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca // indirect
github.com/xuri/nfp v0.0.0-20230819163627-dc951e3ffe1a // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 54465b1

Please sign in to comment.