forked from Fallenstedt/twitter-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_expansion_builder.go
151 lines (132 loc) · 7.06 KB
/
stream_expansion_builder.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
package stream
import (
"fmt"
"net/url"
"strconv"
"strings"
)
type (
//IStreamQueryParamsBuilder is the interface for StreamQueryParamBuilder.
IStreamQueryParamsBuilder interface {
AddBackFillMinutes(minutes uint) *StreamQueryParamBuilder
AddExpansion(expansion string) *StreamQueryParamBuilder
AddMediaField(mediaField string) *StreamQueryParamBuilder
AddPlaceField(placeField string) *StreamQueryParamBuilder
AddPollField(pollField string) *StreamQueryParamBuilder
AddTweetField(tweetField string) *StreamQueryParamBuilder
AddUserField(userField string) *StreamQueryParamBuilder
Build() *url.Values
}
// StreamQueryParamBuilder is a struct used for requesting additional data from a tweet.
// Read more at https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream.
StreamQueryParamBuilder struct {
backFillMinutes uint
expansions []*string
mediaFields []*string
placeFields []*string
pollFields []*string
tweetFields []*string
userFields []*string
}
)
// NewStreamQueryParamsBuilder creeates a struct that implements IStreamQueryParamsBuilder.
// It is used to request additional data from a tweet.
// Read more at https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream.
func NewStreamQueryParamsBuilder() IStreamQueryParamsBuilder {
return &StreamQueryParamBuilder{
backFillMinutes: 0,
expansions: []*string{},
mediaFields: []*string{},
placeFields: []*string{},
pollFields: []*string{},
tweetFields: []*string{},
userFields: []*string{},
}
}
// Build will build and encode the required query params.
func (s *StreamQueryParamBuilder) Build() *url.Values {
query := new(url.URL).Query()
s.addQuery(&query, &s.expansions, "expansions")
s.addQuery(&query, &s.mediaFields, "media.fields")
s.addQuery(&query, &s.placeFields, "place.fields")
s.addQuery(&query, &s.pollFields, "poll.fields")
s.addQuery(&query, &s.tweetFields, "tweet.fields")
s.addQuery(&query, &s.userFields, "user.fields")
if s.backFillMinutes > 0 {
query.Add("backfill_minutes", strconv.Itoa(int(s.backFillMinutes)))
}
return &query
}
// AddExpansion adds an expansion defined in https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream.
// With expansions, developers can expand objects referenced in the payload. Objects available for expansion are referenced by ID.
// Add a single expansion for each invoke of `AddExpansion`.
func (s *StreamQueryParamBuilder) AddExpansion(expansion string) *StreamQueryParamBuilder {
s.expansions = append(s.expansions, &expansion)
return s
}
// AddMediaField adds a media field which enables you to select which specific media fields will deliver in each returned tweet.
// The Tweet will only return media fields if the Tweet contains media and if you've also included `AddExpansion("attachments.media_keys")`.
// Learn more about media fields on twitter docs https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream.
// Add a single media field for each invoke of `AddMediaField`.
func (s *StreamQueryParamBuilder) AddMediaField(mediaField string) *StreamQueryParamBuilder {
s.mediaFields = append(s.mediaFields, &mediaField)
return s
}
// AddPlaceField adds a place field which enables you to select which specific place fields will deliver in each returned tweet.
// The Tweet will only return place fields if the Tweet contains a place and if you've also included `AddExpansion("geo.place_id")`.
// Learn more about place fields on twitter docs https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream.
// Add a single place field for each invoke of `AddPlaceField`.
func (s *StreamQueryParamBuilder) AddPlaceField(placeField string) *StreamQueryParamBuilder {
s.placeFields = append(s.placeFields, &placeField)
return s
}
// AddPollField adds a poll field which enables you to select which specific poll fields will deliver in each returned tweet.
// The Tweet will only return poll fields if the Tweet contains a place and if you've also included `AddExpansion("attachments.poll_ids")`.
// Learn more about poll fields on twitter docs https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream.
// Add a single poll field for each invoke of `AddPollField`.
func (s *StreamQueryParamBuilder) AddPollField(pollField string) *StreamQueryParamBuilder {
s.pollFields = append(s.pollFields, &pollField)
return s
}
// AddTweetField This fields parameter enables you to select which specific Tweet fields will deliver in each returned Tweet object.
// Specify the desired fields in a comma-separated list without spaces between commas and fields.
// You can also include `AddExpansion("referenced_tweets.id")` to return the specified fields for both the original Tweet and any included referenced Tweets.
// The requested Tweet fields will display in both the original Tweet data object, as well as in the referenced Tweet expanded data object that will be located in the includes data object.
// Learn more about tweet fields on twitter docs https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream.
func (s *StreamQueryParamBuilder) AddTweetField(tweetField string) *StreamQueryParamBuilder {
s.tweetFields = append(s.tweetFields, &tweetField)
return s
}
// AddUserField This fields parameter enables you to select which specific user fields will deliver in each returned Tweet.
// Specify the desired fields in a comma-separated list without spaces between commas and fields.
// While the user ID will be located in the original Tweet object, you will find this ID and all additional user fields in the includes data object.
// You must also pass one of the user expansions to return the desired user field.
// `AddExpansion("author_id")`
// `AddExpansion("entities.mentions.username")`
// `AddExpansion("in_reply_to_user_id")`
// `AddExpansion("referenced_tweets.id.author_id")`
func (s *StreamQueryParamBuilder) AddUserField(userField string) *StreamQueryParamBuilder {
s.userFields = append(s.userFields, &userField)
return s
}
// AddBackFillMinutes will allow you to recover up to 5 minutes worth of data that might have been missed during a disconnection.
// This feature is currently only available to the academic research product track!
// Learn more about media fields on twitter docs https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream.
func (s *StreamQueryParamBuilder) AddBackFillMinutes(backFillMinutes uint) *StreamQueryParamBuilder {
s.backFillMinutes = backFillMinutes
return s
}
func (s StreamQueryParamBuilder) addQuery(qb *url.Values, fields *[]*string, param string) {
if len(*fields) > 0 {
var sb strings.Builder
for i, expansion := range *fields {
if i == len(*fields) - 1 {
sb.WriteString(fmt.Sprintf("%v", *expansion))
} else {
sb.WriteString(fmt.Sprintf("%v,", *expansion))
}
}
value := sb.String()
qb.Add(param, value)
}
}