This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
api.go
107 lines (91 loc) · 2.25 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
package twitterscraper
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
const bearerToken string = "AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw"
// RequestAPI get JSON from frontend API and decodes it
func (s *Scraper) RequestAPI(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()
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
}
}
resp, err := s.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
content, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("response status %s: %s", resp.Status, content)
}
if resp.Header.Get("X-Rate-Limit-Remaining") == "0" {
s.guestToken = ""
}
if target == nil {
return nil
}
return json.Unmarshal(content, target)
}
// GetGuestToken from Twitter API
func (s *Scraper) GetGuestToken() error {
req, err := http.NewRequest("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 err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("response status %s: %s", resp.Status, body)
}
var jsn map[string]interface{}
if err := json.Unmarshal(body, &jsn); err != nil {
return err
}
var ok bool
if s.guestToken, ok = jsn["guest_token"].(string); !ok {
return fmt.Errorf("guest_token not found")
}
s.guestCreatedAt = time.Now()
return nil
}