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
|
package client
import (
"bufio"
"encoding/json"
"fmt"
"git.db.org.ai/dborg/internal/models"
"io"
"net/http"
"net/url"
"strings"
)
func (c *Client) SearchTwitterHistory(username string) (*models.XResponse, error) {
path := fmt.Sprintf("/x/history/%s", url.PathEscape(username))
data, err := c.Get(path, nil)
if err != nil {
return nil, err
}
var response models.XResponse
if err := json.Unmarshal(data, &response); err != nil {
return nil, fmt.Errorf("failed to parse Twitter/X response: %w", err)
}
return &response, nil
}
func (c *Client) GetFirstFollowers(username string) (*models.FirstFollowersResponse, error) {
path := fmt.Sprintf("/x/first/%s", url.PathEscape(username))
data, err := c.Get(path, nil)
if err != nil {
return nil, err
}
var response models.FirstFollowersResponse
if err := json.Unmarshal(data, &response); err != nil {
return nil, fmt.Errorf("failed to parse first followers response: %w", err)
}
return &response, nil
}
func (c *Client) GetNotableFollowers(username string) (*models.NotableFollowersResponse, error) {
path := fmt.Sprintf("/x/nfl/%s", url.PathEscape(username))
data, err := c.Get(path, nil)
if err != nil {
return nil, err
}
var response models.NotableFollowersResponse
if err := json.Unmarshal(data, &response); err != nil {
return nil, fmt.Errorf("failed to parse notable followers response: %w", err)
}
return &response, nil
}
func (c *Client) FetchTweetsStream(username string, callback func(result json.RawMessage) error) error {
path := fmt.Sprintf("/x/tweets/%s", url.PathEscape(username))
fullURL := c.config.BaseURL + path
req, err := http.NewRequest(http.MethodGet, fullURL, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("User-Agent", c.config.UserAgent)
req.Header.Set("Accept", "application/x-ndjson, application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to execute request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
}
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Bytes()
if len(line) == 0 {
continue
}
if err := callback(json.RawMessage(line)); err != nil {
return err
}
}
if err := scanner.Err(); err != nil {
if !strings.Contains(err.Error(), "context deadline exceeded") && !strings.Contains(err.Error(), "timeout") {
return fmt.Errorf("stream reading error: %w", err)
}
}
return nil
}
func (c *Client) FetchRepliesStream(tweetID string, limit int, callback func(result json.RawMessage) error) error {
path := fmt.Sprintf("/x/replies/%s", url.PathEscape(tweetID))
params := url.Values{}
if limit > 0 {
params.Set("limit", fmt.Sprintf("%d", limit))
}
fullURL := c.config.BaseURL + path
if len(params) > 0 {
fullURL += "?" + params.Encode()
}
req, err := http.NewRequest(http.MethodGet, fullURL, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("User-Agent", c.config.UserAgent)
req.Header.Set("Accept", "application/x-ndjson, application/json")
req.Header.Set("X-API-Key", c.config.APIKey)
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to execute request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
}
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Bytes()
if len(line) == 0 {
continue
}
if err := callback(json.RawMessage(line)); err != nil {
return err
}
}
if err := scanner.Err(); err != nil {
if !strings.Contains(err.Error(), "context deadline exceeded") && !strings.Contains(err.Error(), "timeout") {
return fmt.Errorf("stream reading error: %w", err)
}
}
return nil
}
func (c *Client) SearchTweetsStream(query string, limit int, callback func(result json.RawMessage) error) error {
path := fmt.Sprintf("/x/search/%s", url.PathEscape(query))
params := url.Values{}
if limit > 0 {
params.Set("limit", fmt.Sprintf("%d", limit))
}
fullURL := c.config.BaseURL + path
if len(params) > 0 {
fullURL += "?" + params.Encode()
}
req, err := http.NewRequest(http.MethodGet, fullURL, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("User-Agent", c.config.UserAgent)
req.Header.Set("Accept", "application/x-ndjson, application/json")
req.Header.Set("X-API-Key", c.config.APIKey)
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to execute request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
}
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Bytes()
if len(line) == 0 {
continue
}
if err := callback(json.RawMessage(line)); err != nil {
return err
}
}
if err := scanner.Err(); err != nil {
if !strings.Contains(err.Error(), "context deadline exceeded") && !strings.Contains(err.Error(), "timeout") {
return fmt.Errorf("stream reading error: %w", err)
}
}
return nil
}
|