Skip to content
This repository has been archived by the owner on Nov 5, 2022. It is now read-only.

Commit

Permalink
rc/0.2.1 (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallenstedt authored Jan 9, 2021
1 parent 5749377 commit f48db2b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ This project is not production ready. There are several things I need to do:

#### Starting a stream

Once you obtain an Access Token, you can create a TwitterStream instance with `NewTwitterStream(accesToken)`
Then, you can invoke `StartStream` to begin the streaming process.

To read messages from your stream, start a loop with `GetMessages`. The messages that is returned could be
a tweet, or an error.

The possible errors that can be returned are
* `io.EOF`: An error that you have reached the end of the stream.
* `non io.EOF errors`: This could be errors that are returned from Twitter during your stream

[You can learn more about processing data by reading Twitter's documentation here](https://dev.twitter.com/streaming/overview/processing)

```go
// Starting a stream assuming you already have
// stream rules set in place
Expand All @@ -40,11 +52,14 @@ func startStreaming() {

// With an access token, you can create a new twitterstream and start streaming
api := twitterstream.NewTwitterStream(token.AccessToken)
api.Stream.StartStream()
err := api.Stream.StartStream()
if err != nil {
panic(err)
}

// If you do not put this in a go routine, you will stream forever
go func() {
// Range over the messages channel to get a message
// Range over the messages channel to get a message, or an error.
for message := range *api.Stream.GetMessages() {
fmt.Println(message)
}
Expand Down Expand Up @@ -77,6 +92,7 @@ func addRules() {
// With an access token, you can create a new twitterstream and start adding rules
api := twitterstream.NewTwitterStream(token.AccessToken)


// You can add rules by passing in stringified JSON with the rules you want to add
// You can learn more about building rules here: https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/integrate/build-a-rule
// Or here: https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/post-tweets-search-stream-rules
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0
0.2.1
12 changes: 7 additions & 5 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type (
// IStream is the interface that the stream struct implements.
IStream interface {
StartStream()
StartStream() error
StopStream()
GetMessages() *chan interface{}
}
Expand Down Expand Up @@ -43,21 +43,22 @@ func (s *stream) StopStream() {
}

// StartStream makes an HTTP request to twitter and starts streaming tweets to the Messages channel.
func (s *stream) StartStream() {
func (s *stream) StartStream() error {

res, err := s.httpClient.newHttpRequest(&requestOpts{
Method: "GET",
Url: endpoints["stream"],
})

if err != nil {
panic(err)
return err
}

s.reader.setStreamResponseBody(res.Body)

s.group.Add(1)
go s.streamMessages(res)
return nil
}

func (s *stream) streamMessages(res *http.Response) {
Expand All @@ -67,15 +68,16 @@ func (s *stream) streamMessages(res *http.Response) {
for !stopped(s.done) {
data, err := s.reader.readNext()
if err != nil {
return
s.messages <- err
s.StopStream()
break
}
if len(data) == 0 {
// empty keep-alive
continue
}

m := string(data)
// TODO send data or error here
s.messages <- m
}
}

0 comments on commit f48db2b

Please sign in to comment.