generated from actions-go/go-action
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
205 lines (178 loc) · 4.99 KB
/
main.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"strings"
//"github.com/actions-go/toolkit/core"
"github.com/gocolly/colly"
)
const (
MAX_LIMIT int = 56
)
type Author_ struct {
ID string `json:"id"`
Name string `json:"name"`
}
type ResharedStatus_ struct {
Author Author_ `json:"Author"`
}
type Card_ struct {
ID string `json:"id"`
URL string `json:"url"`
Title string `json:"title"`
Activity string `json:"activity"`
OwnerURI string `json:"owner_uri"`
OwnerName string `json:"owner_name"`
}
type Status_ struct {
ID string `json:"id"`
Card Card_ `json:"card,omitempty"`
Text string `json:"text,omitempty"`
Activity string `json:"activity"`
SharingURL string `json:"sharing_url"`
ResharedStatus ResharedStatus_ `json:"reshared_status,omitempty"`
}
type Item struct {
Status Status_ `json:"status"`
}
type Result struct {
Count int `json:"count"`
Items []Item `json:"items"`
}
func WriteToFile(path string, items []string) {
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
var lines []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err = scanner.Err(); err != nil {
log.Fatal(err)
}
f.Close()
f, err = os.Create(path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
stop := false
w := bufio.NewWriter(f)
for _, line := range lines {
line = strings.TrimSpace(line)
if !stop && line == "<!-- DOUBAN-ACTIVITIES:START -->" {
fmt.Fprintln(w, line)
stop = true
}
if line == "<!-- DOUBAN-ACTIVITIES:END -->" {
stop = false
for _, item := range items {
fmt.Fprintln(w, item)
}
}
if !stop {
fmt.Fprintln(w, line)
}
}
if err = w.Flush(); err != nil {
log.Fatal(err)
}
}
func main() {
uid := os.Getenv("INPUT_UID")
if uid == "" {
return
}
count, err := strconv.Atoi(os.Getenv("INPUT_MAX_COUNT"))
if err != nil {
count = 5
}
path := os.Getenv("INPUT_README_PATH")
items := []string{}
url := fmt.Sprintf("https://m.douban.com/rexxar/api/v2/status/user_timeline/%s?for_mobile=1", uid)
RefererURL := fmt.Sprintf("https://m.douban.com/people/%s/statuses", uid)
c := colly.NewCollector(
colly.UserAgent("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"),
)
c.OnRequest(func(r *colly.Request) {
r.Headers.Set("Referer", RefererURL)
})
c.OnError(func(_ *colly.Response, err error) {
fmt.Println("Something went wrong:", err)
})
c.OnResponse(func(r *colly.Response) {
rs := &Result{}
if err := json.Unmarshal(r.Body, rs); err != nil {
log.Fatal(err)
return
}
var (
text []rune
url, title, activity string
)
for index, item := range rs.Items {
if index+1 > count {
break
}
ResharedStatus := item.Status.ResharedStatus
url = item.Status.SharingURL
url = strings.Split(url, "?_i=")[0] // 链接多了一个_i参数,去掉
activity = item.Status.Activity
var item_ string
if ResharedStatus != (ResharedStatus_{}) {
item_ = fmt.Sprintf("- [%s %s 的动态](%s)", activity, ResharedStatus.Author.Name, url)
} else {
if activity == "说" { // 广播
text = []rune(item.Status.Text)
if len(text) > MAX_LIMIT {
activity = fmt.Sprintf("说: %s...", string(text[:MAX_LIMIT]))
} else {
activity = fmt.Sprintf("说: %s", item.Status.Text)
}
} else if strings.HasPrefix(activity, "收藏") { // 收藏
card := item.Status.Card
owner_id := card.OwnerURI[strings.LastIndex(card.OwnerURI,"/")+1:]
origin_url := card.URL
origin_title := "广播"
if card.Activity == "说" {
origin_url = fmt.Sprintf("https://www.douban.com/people/%s/status/%s/",
owner_id, card.ID)
}
title := item.Status.Text[strings.LastIndex(item.Status.Text, " ")+1:]
item_ = fmt.Sprintf("- [收藏 %s的%s](%s) 到 [豆列 %s](%s)", card.OwnerName,
origin_title, origin_url, title, url)
} else if strings.HasPrefix(activity, "上传") { // 上传
activity = fmt.Sprintf("上传 %s", string([]rune(activity)[2:]))
} else if strings.HasPrefix(activity, "写了") { // 读书笔记
text = []rune(activity)
item_ = fmt.Sprintf("- [写了 %s %s 的读书笔记](%s)",
string(text[4:len(text)-5]), item.Status.Card.Title, url)
} else if strings.HasPrefix(activity, "转发") { // 转发
act := []rune(activity)
if len(act) == 2 {
title = item.Status.Card.Title
} else {
title = string(act[2:])
}
item_ = fmt.Sprintf("- [%s %s](%s)", string(act[:2]), title, url)
} else { // 书影音
activity = string([]rune(activity)[:2])
}
if item_ == "" {
item_ = fmt.Sprintf("- [%s %s](%s)", activity, item.Status.Card.Title, url)
}
}
items = append(items, item_)
}
})
c.OnScraped(func(r *colly.Response) {
WriteToFile(path, items)
})
c.Visit(url)
}