add http proxy support

This commit is contained in:
JustHumanz 2020-12-03 21:42:16 +07:00
parent 111942f1b9
commit 341e1de6d0
4 changed files with 64 additions and 3 deletions

View file

@ -2,6 +2,7 @@ package twitterscraper
import (
"fmt"
"net"
"net/http"
"strconv"
"strings"
@ -35,13 +36,25 @@ type Profile struct {
func GetProfile(username string) (Profile, error) {
url := "https://mobile.twitter.com/" + username
client := http.DefaultClient
if HTTPProxy != nil {
client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(HTTPProxy),
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
}).DialContext,
},
}
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return Profile{}, err
}
req.Header.Set("Accept-Language", "en-US")
resp, err := http.DefaultClient.Do(req)
resp, err := client.Do(req)
if resp == nil {
return Profile{}, err
}