/
tipranks.go
207 lines (198 loc) · 6.13 KB
/
tipranks.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
package screenshot
import (
"bytes"
"context"
"image"
"image/png"
"log"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/device"
"github.com/comerc/segezha4/utils"
)
// MakeScreenshotForTipRanks description
func MakeScreenshotForTipRanks(linkURL string) []byte {
// o := append(chromedp.DefaultExecAllocatorOptions[:],
// // chromedp.ProxyServer("socks5://138.59.207.118:9076"),
// // chromedp.Flag("blink-settings", "imagesEnabled=false"),
// // chromedp.DisableGPU,
// )
// ctx, cancel := chromedp.NewExecAllocator(context.Background(), o...)
// defer cancel()
// ctx1, cancel1 := chromedp.NewContext(ctx)
// defer cancel1()
// ctx1, cancel1 := chromedp.NewContext(context.Background())
// defer cancel1()
// start the browser without a timeout
// if err := chromedp.Run(ctx1); err != nil {
// log.Println(err)
// return nil
// }
ctx0 := context.Background()
ctx1, cancel1 := chromedp.NewContext(ctx0)
defer cancel1()
// start the browser without a timeout
if err := chromedp.Run(ctx1, func() chromedp.Tasks {
return chromedp.Tasks{
network.SetBlockedURLS([]string{
"https://blog.tipranks.com/*",
"https://randomuser.me/*",
"/new-images/stock-research/banner/*",
}),
}
}()); err != nil {
log.Println(err)
return nil
}
const average = 45
ctx2, cancel2 := context.WithTimeout(ctx1, utils.GetTimeout(average))
defer cancel2()
sel1 := "body div.client-components-stock-research-smart-score-style__rank"
sel2 := "body div.client-components-stock-research-analysts-style__analystTopPart"
sel3 := "body div.client-components-stock-research-individual-investors-style__topSection"
sel41 := "body div.client-components-stock-research-bloggers-bloggerStyles__generalOpinions"
sel42 := "body div.client-components-stock-research-style__emptyContainer"
var buf1, buf2, buf3, buf4 []byte
if err := chromedp.Run(ctx2, func() chromedp.Tasks {
return chromedp.Tasks{
chromedp.Emulate(device.IPadlandscape),
chromedp.Navigate(linkURL),
chromedp.WaitReady("body > #app "),
chromedp.Sleep(4 * time.Second),
chromedp.SetAttributeValue("body > #app > div > div > div.tr-app", "style", "display:none"),
chromedp.Click("body nav > a:nth-child(1)", chromedp.BySearch),
chromedp.SetAttributeValue(sel1, "style", "margin: 10px 0"),
chromedp.ActionFunc(screenshotWithoutPopups(sel1, &buf1)),
chromedp.Click("body nav > a:nth-child(2)", chromedp.BySearch),
chromedp.SetAttributeValue(sel2, "style", "margin: 0 0 10px"),
chromedp.ActionFunc(screenshotWithoutPopups(sel2, &buf2)),
chromedp.Click("body nav > a:nth-child(3)", chromedp.BySearch),
chromedp.SetAttributeValue(sel3, "style", "margin: 0 0 10px"),
chromedp.ActionFunc(screenshotWithoutPopups(sel3, &buf3)),
chromedp.Click("body nav > a:nth-child(4)", chromedp.BySearch),
// chromedp.SetAttributeValue(sel4, "style", "margin: 0 0 10px"),
// chromedp.SetAttributeValue(sel4, "style", "margin: 0 0 10px; height: 174px"),
chromedp.ActionFunc(setAttributeValueIfExistViaResult(sel41, "style", "margin: 0 0 10px")),
chromedp.ActionFunc(setAttributeValueIfExistViaResult(sel42, "style", "margin: 0 0 10px; height: 174px")),
chromedp.ActionFunc(screenshotWithoutPopupsViaResult(&buf4)),
}
}()); err != nil {
log.Println(err)
return nil
}
var src image.Image
if err := glueForTipRanks(buf1, buf2, buf3, buf4, &src); err != nil {
log.Println(err)
return nil
}
buf1, buf2, buf3, buf4 = nil, nil, nil, nil
// resize to width 800 using Bicubic resampling
// and preserve aspect ratio
// res := resize.Resize(800, 0, src, resize.Bicubic)
// encode
out := &bytes.Buffer{}
if err := png.Encode(out, src); err != nil {
log.Println(err)
return nil
}
src = nil
// res = nil
return out.Bytes()
}
func screenshotWithoutPopups(sel string, buf *[]byte) func(context.Context) error {
var fn func(context.Context) error
fn = func(ctx context.Context) error {
if err := chromedp.Screenshot(sel, buf, chromedp.NodeVisible).Do(ctx); err != nil {
return err
}
var isPopup1, isPopup2 bool
if err := hidePopup(ctx, "body #gtm_popup_blocker_iframe", &isPopup1); err != nil {
return err
}
if err := hidePopup(ctx, "body > #popup-ios-modal-v4", &isPopup2); err != nil {
return err
}
if isPopup1 || isPopup2 {
return fn(ctx)
}
return nil
}
return fn
}
func hidePopup(ctx context.Context, sel string, isPopup *bool) error {
var nodes []*cdp.Node
if err := chromedp.Nodes(sel, &nodes, chromedp.AtLeast(0)).Do(ctx); err != nil {
return err
}
if len(nodes) == 0 {
return nil
}
var ok = false
var value string
if err := chromedp.AttributeValue(sel, "style", &value, &ok).Do(ctx); err != nil {
return err
}
if ok && value == "display:none" {
return nil
}
if err := chromedp.SetAttributeValue(sel, "style", "display:none").Do(ctx); err != nil {
return err
}
*isPopup = true
return nil
}
func glueForTipRanks(buf1, buf2, buf3, buf4 []byte, src *image.Image) error {
img1, _, err := image.Decode(bytes.NewReader(buf1))
if err != nil {
return err
}
buf1 = nil
img2, _, err := image.Decode(bytes.NewReader(buf2))
if err != nil {
return err
}
buf2 = nil
img3, _, err := image.Decode(bytes.NewReader(buf3))
if err != nil {
return err
}
buf3 = nil
img4, _, err := image.Decode(bytes.NewReader(buf4))
if err != nil {
return err
}
buf4 = nil
glueImages(img1, img2, src)
glueImages(*src, img3, src)
glueImages(*src, img4, src)
img1 = nil
img2 = nil
img3 = nil
img4 = nil
return nil
}
var result string
func setAttributeValueIfExistViaResult(sel, name, value string) func(context.Context) error {
return func(ctx context.Context) error {
var nodes []*cdp.Node
if err := chromedp.Nodes(sel, &nodes, chromedp.AtLeast(0)).Do(ctx); err != nil {
return err
}
if len(nodes) == 0 {
return nil
}
if err := chromedp.SetAttributeValue(sel, name, value).Do(ctx); err != nil {
return err
} else {
result = sel
}
return nil
}
}
func screenshotWithoutPopupsViaResult(buf *[]byte) func(context.Context) error {
return func(context context.Context) error {
return screenshotWithoutPopups(result, buf)(context)
}
}