From f48db2bfee333ae865dc657aaa33ecef8487456c Mon Sep 17 00:00:00 2001 From: Alexander Fallenstedt Date: Sat, 9 Jan 2021 11:52:36 -0800 Subject: [PATCH] rc/0.2.1 (#9) --- README.md | 20 ++++++++++++++++++-- VERSION | 2 +- stream.go | 12 +++++++----- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a1cf081..52bb6e6 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) } @@ -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 diff --git a/VERSION b/VERSION index 341cf11..7dff5b8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.0 \ No newline at end of file +0.2.1 \ No newline at end of file diff --git a/stream.go b/stream.go index e8341ed..141ba29 100644 --- a/stream.go +++ b/stream.go @@ -8,7 +8,7 @@ import ( type ( // IStream is the interface that the stream struct implements. IStream interface { - StartStream() + StartStream() error StopStream() GetMessages() *chan interface{} } @@ -43,7 +43,7 @@ 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", @@ -51,13 +51,14 @@ func (s *stream) StartStream() { }) 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) { @@ -67,7 +68,9 @@ 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 @@ -75,7 +78,6 @@ func (s *stream) streamMessages(res *http.Response) { } m := string(data) - // TODO send data or error here s.messages <- m } }