247 lines
7.7 KiB
Go
247 lines
7.7 KiB
Go
package main
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"log"
|
||
|
||
"github.com/wechat-crawler/pkg/database"
|
||
)
|
||
|
||
func main() {
|
||
fmt.Println("==============================================")
|
||
fmt.Println(" 微信公众号文章数据库管理系统示例")
|
||
fmt.Println("==============================================\n")
|
||
|
||
// 1. 初始化数据库
|
||
db, err := database.InitDB("../data/wechat_articles.db")
|
||
if err != nil {
|
||
log.Fatal("数据库初始化失败:", err)
|
||
}
|
||
defer db.Close()
|
||
|
||
// 2. 创建仓库实例
|
||
officialRepo := database.NewOfficialAccountRepository(db)
|
||
articleRepo := database.NewArticleRepository(db)
|
||
contentRepo := database.NewArticleContentRepository(db)
|
||
|
||
// 3. 示例:添加公众号
|
||
fmt.Println("📝 示例1: 添加公众号信息")
|
||
official := &database.OfficialAccount{
|
||
Biz: "MzI1NjEwMTM4OA==",
|
||
Nickname: "研招网资讯",
|
||
Homepage: "https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzI1NjEwMTM4OA==&scene=124",
|
||
Description: "中国研究生招生信息网官方公众号",
|
||
}
|
||
|
||
// 检查是否已存在
|
||
existing, err := officialRepo.GetByBiz(official.Biz)
|
||
if err != nil {
|
||
log.Fatal("查询公众号失败:", err)
|
||
}
|
||
|
||
var officialID int64
|
||
if existing == nil {
|
||
// 不存在,创建新记录
|
||
officialID, err = officialRepo.Create(official)
|
||
if err != nil {
|
||
log.Fatal("创建公众号失败:", err)
|
||
}
|
||
fmt.Printf("✅ 成功创建公众号: %s (ID: %d)\n\n", official.Nickname, officialID)
|
||
} else {
|
||
// 已存在
|
||
officialID = existing.ID
|
||
fmt.Printf("ℹ️ 公众号已存在: %s (ID: %d)\n\n", existing.Nickname, officialID)
|
||
}
|
||
|
||
// 4. 示例:添加文章
|
||
fmt.Println("📝 示例2: 添加文章信息")
|
||
article := &database.Article{
|
||
OfficialID: officialID,
|
||
Title: "专家分析2026年考研报名人数",
|
||
Author: "研招网资讯",
|
||
Link: "https://mp.weixin.qq.com/s?__biz=MzI1NjEwMTM4OA==&mid=2651232405&idx=1",
|
||
PublishTime: "2024-11-27 10:00:00",
|
||
CreateTime: "2024-11-27 15:30:00",
|
||
CommentID: "2247491372",
|
||
ReadNum: 15234,
|
||
LikeNum: 456,
|
||
ShareNum: 123,
|
||
ContentPreview: "根据最新统计数据显示,2026年全国硕士研究生报名人数预计将达到新高...",
|
||
ParagraphCount: 15,
|
||
}
|
||
|
||
// 检查文章是否已存在
|
||
existingArticle, err := articleRepo.GetByLink(article.Link)
|
||
if err != nil {
|
||
log.Fatal("查询文章失败:", err)
|
||
}
|
||
|
||
var articleID int64
|
||
if existingArticle == nil {
|
||
articleID, err = articleRepo.Create(article)
|
||
if err != nil {
|
||
log.Fatal("创建文章失败:", err)
|
||
}
|
||
fmt.Printf("✅ 成功创建文章: %s (ID: %d)\n\n", article.Title, articleID)
|
||
} else {
|
||
articleID = existingArticle.ID
|
||
fmt.Printf("ℹ️ 文章已存在: %s (ID: %d)\n\n", existingArticle.Title, articleID)
|
||
}
|
||
|
||
// 5. 示例:添加文章内容
|
||
fmt.Println("📝 示例3: 添加文章详细内容")
|
||
|
||
paragraphs := []string{
|
||
"根据最新统计数据显示,2026年全国硕士研究生报名人数预计将达到新高。",
|
||
"教育部相关负责人表示,随着社会对高层次人才需求的增加,考研热度持续上升。",
|
||
"专家建议考生理性选择,注重提升自身综合素质。",
|
||
}
|
||
|
||
images := []string{
|
||
"https://mmbiz.qpic.cn/mmbiz_jpg/xxx1.jpg",
|
||
"https://mmbiz.qpic.cn/mmbiz_jpg/xxx2.jpg",
|
||
}
|
||
|
||
content := &database.ArticleContent{
|
||
ArticleID: articleID,
|
||
HtmlContent: "<div>文章HTML内容</div>",
|
||
TextContent: "文章纯文本内容...",
|
||
Paragraphs: database.StringsToJSON(paragraphs),
|
||
Images: database.StringsToJSON(images),
|
||
}
|
||
|
||
// 检查内容是否已存在
|
||
existingContent, err := contentRepo.GetByArticleID(articleID)
|
||
if err != nil {
|
||
log.Fatal("查询文章内容失败:", err)
|
||
}
|
||
|
||
if existingContent == nil {
|
||
contentID, err := contentRepo.Create(content)
|
||
if err != nil {
|
||
log.Fatal("创建文章内容失败:", err)
|
||
}
|
||
fmt.Printf("✅ 成功添加文章内容 (ID: %d)\n\n", contentID)
|
||
} else {
|
||
fmt.Printf("ℹ️ 文章内容已存在 (ID: %d)\n\n", existingContent.ID)
|
||
}
|
||
|
||
// 6. 示例:查询文章列表
|
||
fmt.Println("📋 示例4: 查询文章列表")
|
||
articles, total, err := articleRepo.List(officialID, 1, 10)
|
||
if err != nil {
|
||
log.Fatal("查询文章列表失败:", err)
|
||
}
|
||
|
||
fmt.Printf("共找到 %d 篇文章:\n", total)
|
||
for i, item := range articles {
|
||
fmt.Printf("%d. %s (👁️ %d | 👍 %d)\n", i+1, item.Title, item.ReadNum, item.LikeNum)
|
||
}
|
||
fmt.Println()
|
||
|
||
// 7. 示例:获取文章详情
|
||
fmt.Println("📖 示例5: 获取文章详情")
|
||
detail, err := contentRepo.GetArticleDetail(articleID)
|
||
if err != nil {
|
||
log.Fatal("获取文章详情失败:", err)
|
||
}
|
||
|
||
if detail != nil {
|
||
fmt.Printf("标题: %s\n", detail.Title)
|
||
fmt.Printf("作者: %s\n", detail.Author)
|
||
fmt.Printf("公众号: %s\n", detail.OfficialName)
|
||
fmt.Printf("发布时间: %s\n", detail.PublishTime)
|
||
fmt.Printf("阅读数: %d | 点赞数: %d\n", detail.ReadNum, detail.LikeNum)
|
||
fmt.Printf("段落数: %d\n", len(detail.Paragraphs))
|
||
fmt.Printf("图片数: %d\n", len(detail.Images))
|
||
if len(detail.Paragraphs) > 0 {
|
||
fmt.Printf("第一段: %s\n", detail.Paragraphs[0])
|
||
}
|
||
}
|
||
fmt.Println()
|
||
|
||
// 8. 示例:搜索文章
|
||
fmt.Println("🔍 示例6: 搜索文章")
|
||
searchResults, searchTotal, err := articleRepo.Search("考研", 1, 10)
|
||
if err != nil {
|
||
log.Fatal("搜索文章失败:", err)
|
||
}
|
||
|
||
fmt.Printf("搜索\"考研\"找到 %d 篇文章:\n", searchTotal)
|
||
for i, item := range searchResults {
|
||
fmt.Printf("%d. %s\n", i+1, item.Title)
|
||
}
|
||
fmt.Println()
|
||
|
||
// 9. 示例:获取统计信息
|
||
fmt.Println("📊 示例7: 获取统计信息")
|
||
stats, err := db.GetStatistics()
|
||
if err != nil {
|
||
log.Fatal("获取统计信息失败:", err)
|
||
}
|
||
|
||
fmt.Printf("公众号总数: %d\n", stats.TotalOfficials)
|
||
fmt.Printf("文章总数: %d\n", stats.TotalArticles)
|
||
fmt.Printf("总阅读数: %d\n", stats.TotalReadNum)
|
||
fmt.Printf("总点赞数: %d\n", stats.TotalLikeNum)
|
||
fmt.Println()
|
||
|
||
// 10. 示例:批量插入文章
|
||
fmt.Println("📦 示例8: 批量插入文章")
|
||
batchArticles := []*database.Article{
|
||
{
|
||
OfficialID: officialID,
|
||
Title: "教育部:2026年全国硕士研究生报名人数为343万",
|
||
Author: "研招网资讯",
|
||
Link: "https://mp.weixin.qq.com/s?__biz=MzI1NjEwMTM4OA==&mid=2651232406",
|
||
PublishTime: "2024-11-26 09:00:00",
|
||
ReadNum: 8965,
|
||
LikeNum: 234,
|
||
ContentPreview: "教育部公布2026年研究生招生数据...",
|
||
ParagraphCount: 12,
|
||
},
|
||
{
|
||
OfficialID: officialID,
|
||
Title: "研考网上确认成功后,需重点关注四件事",
|
||
Author: "研招网资讯",
|
||
Link: "https://mp.weixin.qq.com/s?__biz=MzI1NjEwMTM4OA==&mid=2651232407",
|
||
PublishTime: "2024-11-25 15:30:00",
|
||
ReadNum: 6543,
|
||
LikeNum: 189,
|
||
ContentPreview: "网上确认通过后,考生还需要注意以下事项...",
|
||
ParagraphCount: 8,
|
||
},
|
||
}
|
||
|
||
err = articleRepo.BatchInsertArticles(batchArticles)
|
||
if err != nil {
|
||
log.Fatal("批量插入文章失败:", err)
|
||
}
|
||
fmt.Printf("✅ 成功批量插入 %d 篇文章\n\n", len(batchArticles))
|
||
|
||
// 11. 示例:导出JSON数据
|
||
fmt.Println("💾 示例9: 导出文章列表为JSON")
|
||
allArticles, _, err := articleRepo.List(0, 1, 100)
|
||
if err != nil {
|
||
log.Fatal("查询文章列表失败:", err)
|
||
}
|
||
|
||
jsonData, err := json.MarshalIndent(allArticles, "", " ")
|
||
if err != nil {
|
||
log.Fatal("JSON序列化失败:", err)
|
||
}
|
||
|
||
fmt.Println("文章列表JSON (前200字符):")
|
||
if len(jsonData) > 200 {
|
||
fmt.Println(string(jsonData[:200]) + "...")
|
||
} else {
|
||
fmt.Println(string(jsonData))
|
||
}
|
||
fmt.Println()
|
||
|
||
fmt.Println("==============================================")
|
||
fmt.Println(" 数据库操作示例演示完成!")
|
||
fmt.Println("==============================================")
|
||
}
|