-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpost.go
108 lines (94 loc) · 2.59 KB
/
post.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"golang.org/x/net/html"
"log"
"sort"
"strings"
)
func GetPosts(friends FriendsData) []Post {
var posts []Post
for _, friend := range friends.Friends {
//fmt.Println(friend.Feed)
if friend.Feed == nil {
// 跳过没有获取到RSS内容的朋友
log.Printf("跳过没有获取到RSS内容的朋友: %s", friend.Name)
continue
}
for _, item := range friend.Feed.Items {
// 简化示例,假设item.Published为格式为"2006-01-02T15:04:05Z"的时间字符串
publishedTime := item.PublishedParsed.Format("2006-01-02")
posts = append(posts, Post{
Title: item.Title,
Author: friend.Name,
Date: publishedTime,
Content: filterPosts(getShortContent( /*item.Content*/
func() string {
if item.Content != "" {
return item.Content
}
return item.Description
}(), Config.ContentLength)),
PostURL: item.Link,
AuthorURL: friend.URL,
})
}
}
// 按照日期降序排序文章
sort.SliceStable(posts, func(i, j int) bool {
return posts[i].Date > posts[j].Date
})
// 获取最近的 Config.NumOutputPost 篇文章
var recentPosts []Post
if len(posts) <= Config.NumOutputPost {
recentPosts = posts
} else {
recentPosts = posts[:Config.NumOutputPost]
}
return recentPosts
}
func filterPosts(content string) string {
content = strings.TrimSpace(content)
content = strings.ReplaceAll(content, "\n", "")
content = strings.ReplaceAll(content, "\r", "")
content = strings.ReplaceAll(content, "\t", "")
return content
}
// getShortContent 根据中文字符数截取文本,并去除收尾空白字符
func getShortContent(content string, length int) string {
// 使用golang.org/x/net/html解析HTML标签
doc, err := html.Parse(strings.NewReader(content))
if err != nil {
// 解析失败,则直接截取前length个字符
fmt.Println("解析失败,则直接截取前", length, "个字符")
return truncateText(content, length)
}
// 遍历HTML节点,将文字内容提取出来
var buf strings.Builder
var extractText func(*html.Node)
extractText = func(n *html.Node) {
if n.Type == html.TextNode {
buf.WriteString(n.Data)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
extractText(c)
}
}
extractText(doc)
// 提取的文字内容进行截取
truncatedText := truncateText(buf.String(), length)
return truncatedText
}
// 根据中文字符数截取文本
func truncateText(text string, length int) string {
var buf strings.Builder
var count int
for _, r := range text {
buf.WriteRune(r)
count++
if count >= length {
break
}
}
return buf.String()
}