diff --git a/README.md b/README.md index ce0a9a1..fe67fcd 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,9 @@ import ( func main() { for tweet := range twitterscraper.GetTweets("kennethreitz", 25) { + if tweet.Error != nil { + panic(tweet.Error) + } fmt.Println(tweet.HTML) } } diff --git a/tweets.go b/tweets.go index ccde9d9..dca1bc7 100644 --- a/tweets.go +++ b/tweets.go @@ -38,23 +38,30 @@ type Tweet struct { Videos []Video } +// Result of scrapping +type Result struct { + Tweet + Error error +} + // GetTweets returns channel with tweets for a given user -func GetTweets(user string, pages int) <-chan Tweet { - channel := make(chan Tweet, 0) +func GetTweets(user string, pages int) <-chan *Result { + channel := make(chan *Result, 0) go func(user string) { + defer close(channel) var lastTweetID string for pages > 0 { tweets, err := FetchTweets(user, lastTweetID) if err != nil { - panic(err) + channel <- &Result{Error: err} + return } for _, tweet := range tweets { lastTweetID = tweet.ID - channel <- *tweet + channel <- &Result{Tweet: *tweet} } pages-- } - close(channel) }(user) return channel }