-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
129 lines (119 loc) · 3.34 KB
/
data.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package model
import (
"encoding/json"
"fmt"
"html"
"net/url"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
type Status struct {
StatusTitle string `json:"status_title"`
CreatedAt string `json:"created_at"`
Text string `json:"text"`
User User `json:"user"`
Bid string `json:"bid"`
Pics []Picture `json:"pics"`
}
type User struct {
ScreenName string `json:"screen_name"`
}
type Picture struct {
Large struct {
Size string `json:"size"`
URL string `json:"url"`
} `json:"large"`
}
type RenderData struct {
Status Status `json:"status"`
}
func (r *RenderData) HTML() (s string) {
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(`<!DOCTYPE html><html lang="zh-cn"></html>`))
head := doc.Find("head")
{
head.AppendHtml(`<meta charset="UTF-8">`)
head.AppendHtml(fmt.Sprintf(`<meta name="inostar:publish" content="%s">`, r.CreateTime().Format(time.RFC1123Z)))
head.AppendHtml(fmt.Sprintf(`<title>%s</title>`, html.EscapeString(r.Status.StatusTitle)))
}
body := doc.Find("body")
{
body.AppendHtml(r.info())
body.AppendHtml(r.Status.Text)
body.AppendHtml("<br />")
body.Find("a").Each(func(i int, a *goquery.Selection) {
ref, _ := a.Attr("href")
if ref != "" {
a.SetAttr("href", r.patchRef(ref))
}
})
for _, p := range r.Status.Pics {
body.AppendHtml(fmt.Sprintf(`<a href="%s"><img src="%s" alt="%s"></a><br />`, p.Large.URL, p.Large.URL, p.Large.URL))
}
body.AppendHtml(r.foot())
}
s, _ = doc.Html()
return
}
func (r *RenderData) CreateTime() time.Time {
t, err := time.Parse(time.RubyDate, r.Status.CreatedAt)
if err != nil {
t = time.Now()
}
return t
}
func (r *RenderData) CreateTimeString() string {
return r.CreateTime().Format("2006-01-02 15:04:05")
}
func (r *RenderData) Link() string {
return fmt.Sprintf("https://m.weibo.cn/status/%s", r.Status.Bid)
}
func (r *RenderData) From(data []byte) (err error) {
return json.Unmarshal(data, r)
}
func (r *RenderData) patchRef(ref string) string {
h, err := url.Parse(r.Link())
if err != nil {
return ref
}
u, err := url.Parse(ref)
if err != nil {
return ref
}
if u.Scheme == "" {
u.Scheme = h.Scheme
}
if u.Host == "" {
u.Host = h.Host
}
return u.String()
}
func (r *RenderData) info() string {
const tpl = `
<p>
<a title="Published: {time}" href="{link}"
style="display:block; color: #000; padding-bottom: 10px; text-decoration: none; font-size:1em; font-weight: normal;">
<span style="display: block; color: #666; font-size:1.0em; font-weight: normal;">{source}</span>
<span style="font-size: 1.5em;">{title}</span>
</a>
</p>
`
return strings.NewReplacer(
"{time}", r.CreateTimeString(),
"{link}", r.Link(),
"{source}", html.EscapeString(r.Status.User.ScreenName),
"{title}", html.EscapeString(r.Status.StatusTitle),
).Replace(tpl)
}
func (r *RenderData) foot() string {
const tpl = `
<br/><br/>
<a style="display: inline-block; border-top: 1px solid #ccc; padding-top: 5px; color: #666; text-decoration: none;"
href="{link}">{link}</a>
<p style="color:#999;">Save with <a style="color:#666; text-decoration:none; font-weight: bold;"
href="https://github.com/gonejack/extract-weibo">extract-weibo</a>
</p>`
return strings.NewReplacer(
"{link}", fmt.Sprintf("https://m.weibo.cn/status/%s", r.Status.Bid),
).Replace(tpl)
}