Return result with error

This commit is contained in:
Nomadic 2019-09-15 10:56:58 +03:00
parent 5d2fb52297
commit 1faa46f8d1
2 changed files with 15 additions and 5 deletions

View file

@ -20,6 +20,9 @@ import (
func main() { func main() {
for tweet := range twitterscraper.GetTweets("kennethreitz", 25) { for tweet := range twitterscraper.GetTweets("kennethreitz", 25) {
if tweet.Error != nil {
panic(tweet.Error)
}
fmt.Println(tweet.HTML) fmt.Println(tweet.HTML)
} }
} }

View file

@ -38,23 +38,30 @@ type Tweet struct {
Videos []Video Videos []Video
} }
// Result of scrapping
type Result struct {
Tweet
Error error
}
// GetTweets returns channel with tweets for a given user // GetTweets returns channel with tweets for a given user
func GetTweets(user string, pages int) <-chan Tweet { func GetTweets(user string, pages int) <-chan *Result {
channel := make(chan Tweet, 0) channel := make(chan *Result, 0)
go func(user string) { go func(user string) {
defer close(channel)
var lastTweetID string var lastTweetID string
for pages > 0 { for pages > 0 {
tweets, err := FetchTweets(user, lastTweetID) tweets, err := FetchTweets(user, lastTweetID)
if err != nil { if err != nil {
panic(err) channel <- &Result{Error: err}
return
} }
for _, tweet := range tweets { for _, tweet := range tweets {
lastTweetID = tweet.ID lastTweetID = tweet.ID
channel <- *tweet channel <- &Result{Tweet: *tweet}
} }
pages-- pages--
} }
close(channel)
}(user) }(user)
return channel return channel
} }