forked from n0madic/twitter-scraper
/
scraper.go
207 lines (184 loc) · 4.82 KB
/
scraper.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 twitterscraper
import (
"crypto/tls"
"errors"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"sync"
"time"
"golang.org/x/net/proxy"
)
// Scraper object
type Scraper struct {
bearerToken string
client *http.Client
delay int64
guestToken string
guestCreatedAt time.Time
includeReplies bool
isLogged bool
searchMode SearchMode
wg sync.WaitGroup
}
// SearchMode type
type SearchMode int
const (
// SearchTop - default mode
SearchTop SearchMode = iota
// SearchLatest - live mode
SearchLatest
// SearchPhotos - image mode
SearchPhotos
// SearchVideos - video mode
SearchVideos
// SearchUsers - user mode
SearchUsers
)
// default http client timeout
const DefaultClientTimeout = 10 * time.Second
var defaultScraper *Scraper
// New creates a Scraper object
func New() *Scraper {
jar, _ := cookiejar.New(nil)
return &Scraper{
bearerToken: bearerToken,
client: &http.Client{
Jar: jar,
Timeout: DefaultClientTimeout,
},
}
}
func (s *Scraper) setBearerToken(token string) {
s.bearerToken = token
s.guestToken = ""
}
// IsGuestToken check if guest token not empty
func (s *Scraper) IsGuestToken() bool {
return s.guestToken != ""
}
// SetSearchMode switcher
func (s *Scraper) SetSearchMode(mode SearchMode) *Scraper {
s.searchMode = mode
return s
}
// Deprecated: SetSearchMode wrapper for default Scraper
func SetSearchMode(mode SearchMode) *Scraper {
return defaultScraper.SetSearchMode(mode)
}
// WithDelay add delay between API requests (in seconds)
func (s *Scraper) WithDelay(seconds int64) *Scraper {
s.delay = seconds
return s
}
// Deprecated: WithDelay wrapper for default Scraper
func WithDelay(seconds int64) *Scraper {
return defaultScraper.WithDelay(seconds)
}
// WithReplies enable/disable load timeline with tweet replies
func (s *Scraper) WithReplies(b bool) *Scraper {
s.includeReplies = b
return s
}
// Deprecated: WithReplies wrapper for default Scraper
func WithReplies(b bool) *Scraper {
return defaultScraper.WithReplies(b)
}
// client timeout
func (s *Scraper) WithClientTimeout(timeout time.Duration) *Scraper {
s.client.Timeout = timeout
return s
}
// SetProxy
// set http proxy in the format `http://HOST:PORT`
// set socket proxy in the format `socks5://HOST:PORT`
func (s *Scraper) SetProxy(proxyAddr string) error {
if strings.HasPrefix(proxyAddr, "http") {
urlproxy, err := url.Parse(proxyAddr)
if err != nil {
return err
}
s.client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(urlproxy),
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
DialContext: (&net.Dialer{
Timeout: s.client.Timeout,
}).DialContext,
},
}
return nil
}
if strings.HasPrefix(proxyAddr, "socks5") {
baseDialer := &net.Dialer{
Timeout: s.client.Timeout,
KeepAlive: s.client.Timeout,
}
socksHostPort := strings.ReplaceAll(proxyAddr, "socks5://", "")
dialSocksProxy, err := proxy.SOCKS5("tcp", socksHostPort, nil, baseDialer)
if err != nil {
return errors.New("error creating socks5 proxy :" + err.Error())
}
if contextDialer, ok := dialSocksProxy.(proxy.ContextDialer); ok {
dialContext := contextDialer.DialContext
s.client = &http.Client{
Transport: &http.Transport{
DialContext: dialContext,
},
}
} else {
return errors.New("failed type assertion to DialContext")
}
return nil
}
return errors.New("only support http(s) or socks5 protocol")
}
// SetProxyWithTransport
// set http proxy in the format `http://HOST:PORT`
// set socket proxy in the format `socks5://HOST:PORT`
func (s *Scraper) SetProxyWithTransport(proxyAddr string, transport *http.Transport) error {
if strings.HasPrefix(proxyAddr, "http") {
if _, err := url.Parse(proxyAddr); err != nil {
return err
}
s.client = &http.Client{
Transport: transport,
}
return nil
}
if strings.HasPrefix(proxyAddr, "socks5") {
baseDialer := &net.Dialer{
Timeout: s.client.Timeout,
KeepAlive: s.client.Timeout,
}
socksHostPort := strings.ReplaceAll(proxyAddr, "socks5://", "")
dialSocksProxy, err := proxy.SOCKS5("tcp", socksHostPort, nil, baseDialer)
if err != nil {
return errors.New("error creating socks5 proxy :" + err.Error())
}
if contextDialer, ok := dialSocksProxy.(proxy.ContextDialer); ok {
dialContext := contextDialer.DialContext
s.client = &http.Client{
Transport: &http.Transport{
DialContext: dialContext,
},
}
} else {
return errors.New("failed type assertion to DialContext")
}
return nil
}
return errors.New("only support http(s) or socks5 protocol")
}
func (s *Scraper) BearerToken() string {
return s.bearerToken
}
// Deprecated: SetProxy wrapper for default Scraper
func SetProxy(proxy string) error {
return defaultScraper.SetProxy(proxy)
}
func init() {
defaultScraper = New()
}