add cookie and x-csrf-token

This commit is contained in:
one.cat 2021-09-08 16:02:53 +08:00
parent d8dd7d54ea
commit 31c9e5da5a
5 changed files with 58 additions and 1 deletions

View file

@ -3,6 +3,9 @@ package twitterscraper
import (
"crypto/tls"
"errors"
"fmt"
"golang.org/x/net/proxy"
"log"
"net"
"net/http"
"net/url"
@ -20,6 +23,9 @@ type Scraper struct {
includeReplies bool
searchMode SearchMode
wg sync.WaitGroup
Cookie string
XCsrfToken string
}
// SearchMode type
@ -80,6 +86,18 @@ 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
}
// SetProxy set http proxy in the format `http://HOST:PORT`
func (s *Scraper) SetProxy(proxy string) error {
if !strings.HasPrefix(proxy, "http") {
@ -101,6 +119,27 @@ func (s *Scraper) SetProxy(proxy string) error {
return nil
}
// SetProxy set socks5 proxy in the format `HOST:PORT`
func (s *Scraper) SetSocks5Proxy(socks5 string) error {
log.Println(socks5)
if dialer, err := proxy.SOCKS5("tcp", socks5, nil, proxy.Direct); err != nil {
return errors.New(fmt.Sprintf("can't connect to the socks5 proxy: %s, err: %s", socks5, err.Error()))
} else {
s.client = &http.Client{
Transport: &http.Transport{
Dial: dialer.Dial,
// TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
}).DialContext,
},
}
log.Println(s.client)
}
return nil
}
// SetProxy wrapper for default Scraper
func SetProxy(proxy string) error {
return defaultScraper.SetProxy(proxy)