This repository has been archived by the owner on Aug 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.go
83 lines (71 loc) · 2.1 KB
/
search.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
// Copyright (C) 2020 Michael J. Fromberger. All Rights Reserved.
package tweets
import (
"strconv"
"time"
"github.com/creachadair/twitter/jape"
"github.com/creachadair/twitter/types"
)
// SearchRecent conducts a search query on recent tweets matching the specified
// query filter.
//
// For query syntax, see
// https://developer.twitter.com/en/docs/twitter-api/tweets/search/integrate/build-a-rule
//
// API: 2/tweets/search/recent
func SearchRecent(query string, opts *SearchOpts) Query {
req := &jape.Request{
Method: "2/tweets/search/recent",
Params: make(jape.Params),
}
req.Params.Set("query", query)
opts.addRequestParams(req)
return Query{Request: req}
}
// SearchOpts provides parameters for tweet search. A nil *SearchOpts provides
// empty or zero values for all fields.
type SearchOpts struct {
// A pagination token provided by the server.
PageToken string
// The oldest UTC time from which results will be provided.
StartTime time.Time
// The latest (most recent) UTC time to which results will be provided.
EndTime time.Time
// The maximum number of results to return; 0 means let the server choose.
// Non-zero values < 10 or > 100 are invalid.
MaxResults int
// If set, return results with IDs greater than this (exclusive).
SinceID string
// If set, return results with IDs smaller than this (exclusive).
UntilID string
// Optional response fields and expansions
Optional []types.Fields
}
func (o *SearchOpts) addRequestParams(req *jape.Request) {
if o == nil {
return // nothing to do
}
if o.PageToken != "" {
req.Params.Set("next_token", o.PageToken)
}
if !o.StartTime.IsZero() {
req.Params.Set("start_time", o.StartTime.Format(types.DateFormat))
}
if !o.EndTime.IsZero() {
req.Params.Set("end_time", o.EndTime.Format(types.DateFormat))
}
if o.MaxResults > 0 {
req.Params.Set("max_results", strconv.Itoa(o.MaxResults))
}
if o.SinceID != "" {
req.Params.Set("since_id", o.SinceID)
}
if o.UntilID != "" {
req.Params.Set("until_id", o.UntilID)
}
for _, fs := range o.Optional {
if vs := fs.Values(); len(vs) != 0 {
req.Params.Add(fs.Label(), vs...)
}
}
}