This repository has been archived by the owner on Oct 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtopic.go
93 lines (77 loc) · 2.34 KB
/
topic.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
package zhihu
import (
"fmt"
"strconv"
"github.com/PuerkitoBio/goquery"
)
type Topic struct {
*Page
// name 是改话题的名称
name string
}
func NewTopic(link string, name string) *Topic {
if !validTopicURL(link) {
panic("非法的 Topic 链接:%s" + link)
}
return &Topic{
Page: newZhihuPage(link),
name: name,
}
}
// GetName 返回话题名称
func (t *Topic) GetName() string {
if t.name != "" {
return t.name
}
// <h1 class="zm-editable-content" data-disabled="1">Python</h1>
t.name = strip(t.Doc().Find("h1.zm-editable-content").Text())
return t.name
}
// GetDescription 返回话题的描述
func (t *Topic) GetDescription() string {
if got, ok := t.getStringField("description"); ok {
return got
}
// <div class="zm-editable-content" data-editable-maxlength="130">
// Python 是一种面向对象的解释型计算机程序设计语言,在设计中注重代码的可读性,同时也是一种功能强大的通用型语言。
// <a href="javascript:;" class="zu-edit-button" name="edit">
// <i class="zu-edit-button-icon"></i>修改
// </a>
// </div>
description := strip(t.Doc().Find("div.zm-editable-content").Text())
t.setField("description", description)
return description
}
// GetFollowersNum 返回关注者数量
func (t *Topic) GetFollowersNum() int {
if got, ok := t.getIntField("followers-num"); ok {
return got
}
// <div class="zm-topic-side-followers-info">
// <a href="/topic/19552832/followers">
// <strong>82155</strong>
// </a> 人关注了该话题
// </div>
text := strip(t.Doc().Find("div.zm-topic-side-followers-info strong").Text())
num, _ := strconv.Atoi(text)
t.setField("followers-num", num)
return num
}
// GetTopAuthors 返回最佳回答者,一般来说是 5 个
func (t *Topic) GetTopAuthors() []*User {
authors := make([]*User, 0, 5)
div := t.Doc().Find("div#zh-topic-top-answerer")
div.Find("div.zm-topic-side-person-item-content").Each(func(index int, sel *goquery.Selection) {
tag := sel.Find("a").First()
uHref, _ := tag.Attr("href")
uId := strip(tag.Text())
thisAuthor := NewUser(makeZhihuLink(uHref), uId)
bio, _ := sel.Find("div.zm-topic-side-bio").Attr("title")
thisAuthor.setBio(bio)
authors = append(authors, thisAuthor)
})
return authors
}
func (t *Topic) String() string {
return fmt.Sprintf("<Topic: %s - %s>", t.GetName(), t.Link)
}