-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreads.go
227 lines (196 loc) · 6.34 KB
/
threads.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package threads
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
)
const (
baseURL = "https://www.threads.net"
apiURL = "https://www.threads.net/api/graphql"
getUserDocID = "23996318473300828"
getUserThreadsDocID = "6232751443445612"
getUserRepliesDocID = "6307072669391286"
getPostDocID = "5587632691339264"
getLikersDocID = "9360915773983802"
)
var userIDRegex = regexp.MustCompile(`"user_id":"(\d+)"`)
type Client struct {
client *http.Client
header http.Header
token string
}
type Option func(*Client)
// NewClient returns a new threads API client.
// If no token is provided, it will fetch it automatically.
// If a token is provided, it will use it to make requests to the API.
// If a client is provided, it will be used to make requests to the API.
// If a header is provided, the original header will be modified to make requests to the API.
func NewClient(ctx context.Context, opts ...Option) (*Client, error) {
c := Client{
client: http.DefaultClient,
header: make(http.Header),
}
c.header.Add("Authority", "www.threads.net")
c.header.Add("Accept", "*/*")
c.header.Add("Accept-Language", "en-US,en;q=0.9")
c.header.Add("Cache-Control", "no-cache")
c.header.Add("Content-Type", "application/x-www-form-urlencoded")
c.header.Add("Connetion", "keep-alive")
c.header.Add("Origin", "https://www.threads.net")
c.header.Add("Pragma", "no-cache")
c.header.Add("Sec-Fetch-Site", "same-origin")
c.header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.44.639.844 Safari/537.36")
c.header.Add("X-IG-ASBD-ID", "129477")
c.header.Add("X-IG-App-ID", "238260118697367")
for _, opt := range opts {
opt(&c)
}
if c.token == "" {
token, err := c.getToken(ctx)
if err != nil {
return nil, err
}
c.token = token
}
c.header.Add("X-FB-LSD", c.token)
return &c, nil
}
// WithToken returns an option that sets the token used to make requests to the API.
func WithToken(token string) Option {
return func(c *Client) {
c.token = token
}
}
// WithClient returns an option that sets the client used to make requests to the API.
func WithClient(client *http.Client) Option {
return func(c *Client) {
c.client = client
}
}
// WithHeader returns an option that sets the header used to make requests to the API.
func WithHeader(header http.Header) Option {
return func(c *Client) {
c.header = header
}
}
// getToken returns the token used to make requests to the API.
func (c *Client) getToken(ctx context.Context) (string, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/@instagram", nil)
if err != nil {
return "", err
}
req.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.44.639.844 Safari/537.36")
resp, err := c.client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
pos := bytes.Index(body, []byte("\"token\""))
return string(body[pos+9 : pos+31]), nil
}
// GetUserID returns the user ID of the given username.
func (c *Client) GetUserID(ctx context.Context, name string) (int, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/@"+name, nil)
if err != nil {
return 0, err
}
req.Header = c.header.Clone()
req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
req.Header.Add("Referer", baseURL)
req.Header.Add("Sec-Fetch-Dest", "document")
req.Header.Add("Sec-Fetch-Mode", "navigate")
req.Header.Add("Sec-Fetch-Site", "cross-site")
req.Header.Add("Sec-Fetch-User", "?1")
req.Header.Add("Upgrade-Insecure-Requests", "1")
req.Header.Del("X-Asbd-Id")
req.Header.Del("X-Fb-Lsd")
req.Header.Del("X-Ig-App-Id")
resp, err := c.client.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, err
}
return strconv.Atoi(string(userIDRegex.FindSubmatch(body)[1]))
}
// GetUser returns the profile of the given user ID.
func (c *Client) GetUser(ctx context.Context, userID int) ([]byte, error) {
h := c.header.Clone()
h.Add("X-FB-Friendly-Name", "BarcelonaProfileRootQuery")
return sendRequest(ctx, c.client, h, c.token,
getUserDocID,
map[string]int{"userID": userID},
)
}
// GetUserThreads returns the threads posted by the given user ID.
func (c *Client) GetUserThreads(ctx context.Context, userID int) ([]byte, error) {
h := c.header.Clone()
h.Add("X-FB-Friendly-Name", "BarcelonaProfileThreadsTabQuery")
return sendRequest(ctx, c.client, h, c.token,
getUserThreadsDocID,
map[string]int{"userID": userID},
)
}
// GetUserReplies returns the replies posted by the given user ID.
func (c *Client) GetUserReplies(ctx context.Context, userID int) ([]byte, error) {
h := c.header.Clone()
h.Add("X-FB-Friendly-Name", "BarcelonaProfileRepliesTabQuery")
return sendRequest(ctx, c.client, h, c.token,
getUserRepliesDocID,
map[string]int{"userID": userID},
)
}
// GetPost returns the post of the given post ID.
func (c *Client) GetPost(ctx context.Context, postID int) ([]byte, error) {
h := c.header.Clone()
h.Add("X-FB-Friendly-Name", "BarcelonaPostPageQuery")
return sendRequest(ctx, c.client, h, c.token,
getPostDocID,
map[string]int{"postID": postID},
)
}
// GetLikers returns the liker list of the given post ID.
func (c *Client) GetLikers(ctx context.Context, postID int) ([]byte, error) {
return sendRequest(ctx, c.client, c.header, c.token,
getLikersDocID,
map[string]int{"mediaID": postID},
)
}
func sendRequest(ctx context.Context, c *http.Client, headers http.Header, token, docID string, variables map[string]int) ([]byte, error) {
b, err := json.Marshal(variables)
if err != nil {
return nil, err
}
data := url.Values{}
data.Set("lsd", token)
data.Set("doc_id", docID)
data.Set("variables", string(b))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
req.Header = headers
resp, err := c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("status code %d", resp.StatusCode)
}
return io.ReadAll(resp.Body)
}