forked from imperatrona/twitter-scraper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scraper.go
182 lines (161 loc) · 4.02 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
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
isOpenAccount bool
oAuthToken string
oAuthSecret string
proxy string
userAgent string
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
const DefaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
// New creates a Scraper object
func New() *Scraper {
jar, _ := cookiejar.New(nil)
return &Scraper{
bearerToken: bearerToken,
userAgent: DefaultUserAgent,
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
}
// WithDelay add delay between API requests (in seconds)
func (s *Scraper) WithDelay(seconds int64) *Scraper {
s.delay = seconds
return s
}
// WithReplies enable/disable load timeline with tweet replies
func (s *Scraper) WithReplies(b bool) *Scraper {
s.includeReplies = b
return s
}
// 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 proxyAddr == "" {
s.client.Transport = &http.Transport{
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
DialContext: (&net.Dialer{
Timeout: s.client.Timeout,
}).DialContext,
}
s.proxy = ""
return nil
}
if strings.HasPrefix(proxyAddr, "http") {
urlproxy, err := url.Parse(proxyAddr)
if err != nil {
return err
}
s.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,
}
s.proxy = proxyAddr
return nil
}
if strings.HasPrefix(proxyAddr, "socks5") {
baseDialer := &net.Dialer{
Timeout: s.client.Timeout,
KeepAlive: s.client.Timeout,
}
proxyURL, err := url.Parse(proxyAddr)
if err != nil {
panic(err)
}
// username password
username := proxyURL.User.Username()
password, _ := proxyURL.User.Password()
// ip and port
host := proxyURL.Hostname()
port := proxyURL.Port()
var auth *proxy.Auth
if username != "" || password != "" {
auth = &proxy.Auth{
User: username,
Password: password,
}
}
dialSocksProxy, err := proxy.SOCKS5("tcp", host+":"+port, auth, 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.Transport = &http.Transport{
DialContext: dialContext,
}
} else {
return errors.New("failed type assertion to DialContext")
}
s.proxy = proxyAddr
return nil
}
return errors.New("only support http(s) or socks5 protocol")
}
func (s *Scraper) SetUserAgent(userAgent string) {
s.userAgent = userAgent
}
func (s *Scraper) GetUserAgent() string {
return s.userAgent
}