forked from n0madic/twitter-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.go
168 lines (146 loc) · 3.66 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
package twitterscraper
import (
"crypto/tls"
"errors"
"net"
"net/http"
"net/url"
"strings"
"sync"
"time"
"golang.org/x/net/proxy"
)
// Scraper object
type Scraper struct {
client *http.Client
clientTimeout time.Duration
delay int64
guestToken string
guestCreatedAt time.Time
includeReplies bool
searchMode SearchMode
wg sync.WaitGroup
cookie string
xCsrfToken string
}
// 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 {
return &Scraper{
client: &http.Client{Timeout: DefaultClientTimeout},
clientTimeout: DefaultClientTimeout,
}
}
// 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
}
// 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
}
// 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
}
// WithReplies wrapper for default Scraper
func WithReplies(b bool) *Scraper {
return defaultScraper.WithReplies(b)
}
// cookie
func (s *Scraper) WithCookie(cookie string) *Scraper {
s.cookie = cookie
return s
}
// x csrf token
func (s *Scraper) WithXCsrfToken(xcsrfToken string) *Scraper {
s.xCsrfToken = xcsrfToken
return s
}
// client timeout
func (s *Scraper) WithClientTimeout(timeout time.Duration) *Scraper {
s.clientTimeout = 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.clientTimeout,
}).DialContext,
},
}
return nil
}
if strings.HasPrefix(proxyAddr, "socks5") {
baseDialer := &net.Dialer{
Timeout: s.clientTimeout,
KeepAlive: s.clientTimeout,
}
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")
}
// SetProxy wrapper for default Scraper
func SetProxy(proxy string) error {
return defaultScraper.SetProxy(proxy)
}
func init() {
defaultScraper = New()
}