forked from n0madic/twitter-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
125 lines (106 loc) · 2.96 KB
/
api.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
package twitterscraper
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"time"
jsoniter "github.com/json-iterator/go"
)
const bearerToken string = "AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw"
// RequestAPI get JSON from frontend API and decodes it
func (s *Scraper) RequestAPI(ctx context.Context, req *http.Request, target interface{}) error {
s.wg.Wait()
if s.delay > 0 {
defer func() {
s.wg.Add(1)
go func() {
time.Sleep(time.Second * time.Duration(s.delay))
s.wg.Done()
}()
}()
}
if !s.isLogged {
if !s.IsGuestToken() || s.guestCreatedAt.Before(time.Now().Add(-time.Hour*3)) {
err := s.GetGuestToken(ctx)
if err != nil {
return err
}
}
req.Header.Set("X-Guest-Token", s.guestToken)
}
if s.oAuthToken != "" && s.oAuthSecret != "" {
req.Header.Set("Authorization", s.sign(req.Method, req.URL))
} else {
req.Header.Set("Authorization", "Bearer "+s.bearerToken)
}
for _, cookie := range s.client.Jar.Cookies(req.URL) {
if cookie.Name == "ct0" {
req.Header.Set("X-CSRF-Token", cookie.Value)
break
}
}
if s.userAgent != nil {
req.Header.Set("User-Agent", *s.userAgent)
}
resp, err := s.client.Do(req)
if err != nil {
return errors.Join(err, fmt.Errorf("error while requesting API with proxy %s", s.proxy))
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
content, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
switch resp.StatusCode {
case http.StatusTooManyRequests:
return ErrRateLimitExceeded{description: content}
default:
return ErrOther{description: content, status: resp.Status, StatusCode: resp.StatusCode}
}
}
if resp.Header.Get("X-Rate-Limit-Remaining") == "0" {
s.guestToken = ""
}
if target == nil {
return nil
}
return jsoniter.NewDecoder(resp.Body).Decode(target)
}
// GetGuestToken from Twitter API
func (s *Scraper) GetGuestToken(ctx context.Context) error {
req, err := http.NewRequestWithContext(ctx, "POST", "https://api.twitter.com/1.1/guest/activate.json", nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+s.bearerToken)
resp, err := s.client.Do(req)
if err != nil {
return errors.Join(err, fmt.Errorf("error while getting guest token with proxy %s", s.proxy))
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
content, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
switch resp.StatusCode {
case http.StatusTooManyRequests:
return ErrRateLimitExceeded{description: content}
default:
return ErrOther{description: content, status: resp.Status, StatusCode: resp.StatusCode}
}
}
var jsn map[string]interface{}
if err = jsoniter.NewDecoder(resp.Body).Decode(&jsn); err != nil {
return errors.Join(err, ErrDecodeGuestTokenResponse)
}
var ok bool
if s.guestToken, ok = jsn["guest_token"].(string); !ok {
return ErrGuestTokenNotFound
}
s.guestCreatedAt = time.Now()
return nil
}