-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_caixin.go
308 lines (280 loc) · 8.17 KB
/
agent_caixin.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"context"
"errors"
"fmt"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
"strings"
"time"
)
type Caixin struct {
conf *AgentConf
paywallLocator string
}
func (a *Caixin) Name() string {
return "caixin"
}
func (a *Caixin) DisplayName() string {
return "Caixin"
}
func (a *Caixin) ConfOptions() []ConfMeta {
return []ConfMeta{
{
ConfigName: "Caixin Username",
ConfigDescription: "Your username for Caixin.",
ConfigKey: AgentConfUsername,
Type: FieldTypeString,
Required: true,
},
{
ConfigName: "Caixin Password",
ConfigDescription: "Your password for Caixin.",
ConfigKey: AgentConfPassword,
Type: FieldTypeString,
Required: true,
},
{
ConfigName: "Keyword Blocklist",
ConfigDescription: "Keywords you want to filter out. Split by comma(,).",
ConfigKey: AgentConfKeyBlocklist,
Type: FieldTypeStringList,
Required: false,
},
{
ConfigName: "Custom RSS feed link",
ConfigDescription: "Default feed link is https://rsshub.app/caixin/latest. You can replace it with your own wanted feed link. Multiple links should split by comma(,).",
ConfigKey: AgentConfKeyRSSLinks,
Type: FieldTypeStringList,
Required: false,
},
{
ConfigName: "Include data-pass(数据通) articles",
ConfigDescription: "This requires higher level of subscription. Default is false.",
ConfigKey: AgentConfIncludePremiumArticles,
Type: FieldTypeBool,
Required: false,
},
}
}
func (a *Caixin) BaseDomains() []string {
return []string{"caixin.com"}
}
func (a *Caixin) TestPage() string {
return "https://www.caixin.com/"
}
func (a *Caixin) URLPrefixBlockList() []string {
prefixList := []string{
"https://photos.caixin.com",
}
if !a.conf.IncludePremiumArticles {
prefixList = append(prefixList, "https://database.caixin.com")
}
return prefixList
}
func (a *Caixin) RSSLinks() []string {
return []string{"https://rsshub.app/caixin/latest"}
}
func (a *Caixin) RequireScrolling() bool {
return true
}
func (a *Caixin) Init(conf *AgentConf) error {
a.paywallLocator = "#chargeWallContent"
a.conf = conf
return nil
}
func (a *Caixin) CleanURL(url string) (string, error) {
return url, nil
}
func (a *Caixin) CheckFinishLoading(ctx context.Context) error {
logger.Infof("waiting for article body to load...")
_, err := browser.GetWebElementWithWait(ctx, "#the_content")
if err != nil {
return err
}
err = browser.WaitUntilInvisible(ctx, "#loadinWall")
if err != nil {
return fmt.Errorf("waitUntilInvisible failed: %w", err)
}
// Check if video exist
// Readwise Reader seems cannot handle this type of video yet. But we still make sure it to exist
// in HTML for future usage.
var nodes []*cdp.Node
err = chromedp.Run(ctx, chromedp.Nodes("div.content_video", &nodes, chromedp.AtLeast(0)))
if err != nil {
return err
} else if len(nodes) > 0 {
logger.Infof("video found, wait for it to load...")
_, err = browser.GetWebElementWithWait(ctx, "div.cx-audio-rep")
if err != nil {
return err
}
}
logger.Infof("body loading finished")
return nil
}
func (a *Caixin) IsArticlePaywalled(ctx context.Context) (bool, error) {
return a.isPaywalled(ctx)
}
func (a *Caixin) EnsureLoggedIn(ctx context.Context) error {
isPaywalled, err := a.isPaywalled(ctx)
if err != nil {
return fmt.Errorf("isPaywalled failed: %w", err)
}
if isPaywalled {
logger.Infof("is paywalled content and not logged-in")
err = chromedp.Run(ctx, chromedp.Tasks{
chromedp.Navigate(`https://u.caixin.com/web/login`),
browser.WaitUntilDocumentReady(),
})
if err != nil {
return fmt.Errorf("failed to go to login page: %w", err)
}
currentURL, err := browser.GetCurrentURL(ctx)
if err != nil {
return fmt.Errorf("GetCurrentURL failed: %w", err)
}
if currentURL == "https://u.caixin.com/web/workbench" {
// already login
return fmt.Errorf("already logged in but still paywalled. Check if your subscription is valid")
}
err = a.login(ctx)
if err != nil {
return fmt.Errorf("failed to login: %w", err)
}
return nil
} else {
logger.Infof("is not paywalled content or already logged in")
return nil
}
}
func (a *Caixin) isPaywalled(ctx context.Context) (bool, error) {
var nodes []*cdp.Node
err := chromedp.Run(ctx, chromedp.Nodes(a.paywallLocator, &nodes, chromedp.AtLeast(0)))
if err != nil {
return false, err
} else if len(nodes) == 0 {
return false, nil
} else {
// check if visible
var nodes []*cdp.Node
var isHidden bool
err := chromedp.Run(ctx,
chromedp.Nodes(a.paywallLocator, &nodes, chromedp.AtLeast(1)),
browser.IsElementHidden(a.paywallLocator, &isHidden),
)
if err != nil {
return false, fmt.Errorf("check isHidden failed: %w", err)
}
if isHidden {
return false, nil
}
var bodyText string
err = chromedp.Run(ctx, chromedp.OuterHTML("html", &bodyText))
if err != nil {
return false, fmt.Errorf("get document body failed: %w", err)
}
if strings.Contains(bodyText, "请升级后阅读") {
return true, fmt.Errorf("当前用户会员等级不足,需要升级后阅读")
}
return true, nil
}
}
func (a *Caixin) login(ctx context.Context) error {
logger.Infof("logging in...")
username := a.conf.Username
if username == "" {
return errors.New("username is empty, cannot proceed")
}
password := a.conf.Password
if password == "" {
return errors.New("password is empty, cannot proceed")
}
logger.Infof("next step: waiting icon to be visible")
err := chromedp.Run(ctx,
chromedp.WaitVisible(`#app > div > section > div > div:nth-child(1) > div > div > span > svg > use`),
)
if err != nil {
return fmt.Errorf("wait icon visible failed: %w", err)
}
err = chromedp.Run(ctx,
chromedp.Click(`#app > div > section > div > div:nth-child(1) > div > div > span > svg > use`),
)
if err != nil {
return fmt.Errorf("click icon failed: %w", err)
}
logger.Infof("next step: wait mobile input to be visible")
err = chromedp.Run(ctx,
chromedp.WaitVisible(`input[name='mobile']`),
)
if err != nil {
return fmt.Errorf("wait mobilt input visible failed: %w", err)
}
err = chromedp.Run(ctx,
chromedp.Focus(`input[name='mobile']`),
)
if err != nil {
return fmt.Errorf("focus mobile input failed: %w", err)
}
logger.Infof("next step: clear mobile input")
err = chromedp.Run(ctx,
chromedp.Evaluate(`document.querySelector("input[name='mobile']").value = ""`, nil),
)
if err != nil {
return fmt.Errorf("clear mobile input failed: %w", err)
}
logger.Infof("next step: sending mobile number")
err = chromedp.Run(ctx,
chromedp.SendKeys(`input[name='mobile']`, username),
chromedp.Sleep(1*time.Second),
)
if err != nil {
return fmt.Errorf("input mobile number failed: %w", err)
}
logger.Infof("next step: send password")
err = chromedp.Run(ctx,
chromedp.SendKeys(`input[name='password']`, password),
chromedp.Sleep(1*time.Second),
)
if err != nil {
return fmt.Errorf("input password failed: %w", err)
}
err = chromedp.Run(ctx,
chromedp.Click(`#app > div > section > div > div.cx-login-argree > label > span > span`),
chromedp.Sleep(1*time.Second),
)
if err != nil {
return fmt.Errorf("click agreement failed: %w", err)
}
logger.Infof("next step: click login button")
err = chromedp.Run(ctx,
chromedp.Click(`button.login-btn`),
)
if err != nil {
return fmt.Errorf("click login button failed: %w", err)
}
err = chromedp.Run(ctx,
chromedp.WaitNotPresent(`button.login-btn`),
)
if err != nil {
return fmt.Errorf("wait login button to disappear failed: %w", err)
}
return nil
}
func (a *Caixin) EventListener(ctx context.Context) func(ev interface{}) {
return func(ev interface{}) {
if ev, ok := ev.(*page.EventJavascriptDialogOpening); ok {
logger.Warnf("[%s] Reveived dialog message: %+v", a.Name(), ev)
// 当检测到弹窗时,自动点击确定按钮(当前用于财新被动登出时的弹窗)
go func() {
err := chromedp.Run(ctx,
page.HandleJavaScriptDialog(true),
)
if err != nil {
logger.Errorf("[caixin] HandleJavaScriptDialog failed: %v", err)
}
}()
}
}
}