-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearch.go
More file actions
93 lines (84 loc) · 2.78 KB
/
search.go
File metadata and controls
93 lines (84 loc) · 2.78 KB
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
package threads
import (
"context"
"fmt"
"net/url"
"strings"
)
// KeywordSearch searches for public Threads media by keyword
func (c *Client) KeywordSearch(ctx context.Context, query string, opts *SearchOptions) (*PostsResponse, error) {
if strings.TrimSpace(query) == "" {
return nil, NewValidationError(400, ErrEmptySearchQuery, "Cannot search without a query string", "query")
}
// Ensure we have a valid token
if err := c.EnsureValidToken(ctx); err != nil {
return nil, err
}
// Build query parameters according to API documentation
params := url.Values{
"q": {query},
"fields": {PostExtendedFields}, // Use PostExtendedFields for comprehensive search results
}
// Add search options if provided
if opts != nil {
if opts.SearchType != "" {
params.Set("search_type", string(opts.SearchType))
}
if opts.SearchMode != "" {
params.Set("search_mode", string(opts.SearchMode))
}
if opts.MediaType != "" {
// Validate media type
mediaType := strings.ToUpper(opts.MediaType)
if mediaType != MediaTypeText && mediaType != MediaTypeImage && mediaType != MediaTypeVideo {
return nil, NewValidationError(400, "Invalid media type", "Media type must be TEXT, IMAGE, or VIDEO", "media_type")
}
params.Set("media_type", mediaType)
}
if opts.AuthorUsername != "" {
// Strip leading @ if present for convenience
username := strings.TrimPrefix(opts.AuthorUsername, "@")
if strings.TrimSpace(username) == "" {
return nil, NewValidationError(400, "Invalid author username", "Author username cannot be empty", "author_username")
}
params.Set("author_username", username)
}
if opts.Limit > 0 {
if opts.Limit > 100 {
return nil, NewValidationError(400, "Limit too large", "Maximum limit is 100 posts per request", "limit")
}
params.Set("limit", fmt.Sprintf("%d", opts.Limit))
}
if opts.Since > 0 {
// Validate timestamp according to API documentation
if opts.Since < 1688540400 {
return nil, NewValidationError(400, "Invalid since timestamp", "Since timestamp must be greater than or equal to 1688540400", "since")
}
params.Set("since", fmt.Sprintf("%d", opts.Since))
}
if opts.Until > 0 {
params.Set("until", fmt.Sprintf("%d", opts.Until))
}
if opts.Before != "" {
params.Set("before", opts.Before)
}
if opts.After != "" {
params.Set("after", opts.After)
}
}
// Make API call to keyword search endpoint
path := "/keyword_search"
resp, err := c.httpClient.GET(path, params, c.getAccessTokenSafe())
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, c.handleAPIError(resp)
}
// Parse response
var postsResp PostsResponse
if err := safeJSONUnmarshal(resp.Body, &postsResp, "keyword search response", resp.RequestID); err != nil {
return nil, err
}
return &postsResp, nil
}