This repository has been archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtwitterstream.go
49 lines (41 loc) · 1.67 KB
/
twitterstream.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
// Package twitterstream provides an easy way to stream tweets using Twitter's v2 Streaming API.
package twitterstream
import (
"github.com/fallenstedt/twitter-stream/httpclient"
"github.com/fallenstedt/twitter-stream/rules"
"github.com/fallenstedt/twitter-stream/stream"
"github.com/fallenstedt/twitter-stream/token_generator"
)
type TwitterApi struct {
Rules rules.IRules
Stream stream.IStream
}
// NewTokenGenerator creates a TokenGenerator which can request a Bearer token using a twitter api key and secret.
func NewTokenGenerator() token_generator.ITokenGenerator {
client := httpclient.NewHttpClient("")
tokenGenerator := token_generator.NewTokenGenerator(client)
return tokenGenerator
}
// NewRuleBuilder creates a rule builder for creating rules.
// It is used in `rules.Create`.
func NewRuleBuilder() rules.IRuleBuilder {
return rules.NewRuleBuilder()
}
// NewRuleDelete creates a delete rules request.
// It is used in `rules.Delete`.
func NewRuleDelete(ids ...int) rules.DeleteRulesRequest {
return rules.NewDeleteRulesRequest(ids...)
}
// NewStreamQueryParamsBuilder creates a stream query param builder.
// It is used with `stream.StartStream()` to include tweets with extra metadata.
func NewStreamQueryParamsBuilder() stream.IStreamQueryParamsBuilder {
return stream.NewStreamQueryParamsBuilder()
}
// NewTwitterStream consumes a twitter Bearer token.
// It is used to interact with Twitter's v2 filtered streaming API
func NewTwitterStream(token string) *TwitterApi {
client := httpclient.NewHttpClient(token)
rules := rules.NewRules(client)
stream := stream.NewStream(client, stream.NewStreamResponseBodyReader())
return &TwitterApi{Rules: rules, Stream: stream}
}