From a34a14d25a4090b95e805db43811d636d48e7bde Mon Sep 17 00:00:00 2001 From: Alexander Sheiko Date: Thu, 14 May 2020 21:52:55 +0300 Subject: [PATCH] Fix some golangci-lint warnings Minor changes --- profile.go | 16 +++++++++------- profile_test.go | 2 +- trends.go | 2 +- tweets.go | 17 +++++++++-------- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/profile.go b/profile.go index 4fc29dc..c3ec5e7 100644 --- a/profile.go +++ b/profile.go @@ -10,7 +10,7 @@ import ( "github.com/PuerkitoBio/goquery" ) -// Profile of twitter user +// Profile of twitter user. type Profile struct { Avatar string Biography string @@ -27,26 +27,28 @@ type Profile struct { Website string } -// GetProfile return parsed user profile +// GetProfile return parsed user profile. func GetProfile(username string) (Profile, error) { url := "https://twitter.com/" + username req, err := http.NewRequest("GET", url, nil) - if err != nil { return Profile{}, err } req.Header.Set("Accept-Language", "en-US") - res, err := http.DefaultClient.Do(req) - - if res == nil { + resp, err := http.DefaultClient.Do(req) + if resp == nil { return Profile{}, err } + defer resp.Body.Close() - doc, err := goquery.NewDocumentFromReader(res.Body) + if resp.StatusCode != http.StatusOK { + return Profile{}, fmt.Errorf("response status: %s", resp.Status) + } + doc, err := goquery.NewDocumentFromReader(resp.Body) if err != nil { return Profile{}, err } diff --git a/profile_test.go b/profile_test.go index 8cb1d10..adaf2e0 100644 --- a/profile_test.go +++ b/profile_test.go @@ -10,7 +10,7 @@ import ( func TestGetProfile(t *testing.T) { loc := time.FixedZone("UTC", 0) - joined := time.Date(2007,02, 20,6,35,0,0, loc) + joined := time.Date(2007, 02, 20, 6, 35, 0, 0, loc) sample := Profile{ Avatar: "https://pbs.twimg.com/profile_images/1111729635610382336/_65QFl7B_400x400.png", Biography: "What’s happening?!", diff --git a/trends.go b/trends.go index b630b2c..8017019 100644 --- a/trends.go +++ b/trends.go @@ -8,7 +8,7 @@ import ( const trendsURL = "https://twitter.com/i/trends" -// GetTrends return list of trends +// GetTrends return list of trends. func GetTrends() ([]string, error) { req, err := http.NewRequest("GET", trendsURL, nil) if err != nil { diff --git a/tweets.go b/tweets.go index 407b31b..f1f026a 100644 --- a/tweets.go +++ b/tweets.go @@ -13,13 +13,13 @@ import ( const ajaxURL = "https://twitter.com/i/profiles/show/%s/timeline/tweets" -// Video type +// Video type. type Video struct { ID string Preview string } -// Tweet type +// Tweet type. type Tweet struct { Hashtags []string HTML string @@ -38,13 +38,13 @@ type Tweet struct { Videos []Video } -// Result of scrapping +// 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 *Result { channel := make(chan *Result) go func(user string) { @@ -66,7 +66,7 @@ func GetTweets(user string, pages int) <-chan *Result { return channel } -// FetchTweets gets tweets for a given user, via the Twitter frontend API +// FetchTweets gets tweets for a given user, via the Twitter frontend API. func FetchTweets(user string, last string) ([]*Tweet, error) { var tweets []*Tweet @@ -119,13 +119,14 @@ func FetchTweets(user string, last string) ([]*Tweet, error) { }) s.Find(".ProfileTweet-actionCount").Each(func(i int, c *goquery.Selection) { txt := strings.TrimSpace(c.Text()) - if strings.HasSuffix(txt, "likes") { + switch { + case strings.HasSuffix(txt, "likes"): l := strings.Split(txt, " ") tweet.Likes, _ = strconv.Atoi(l[0]) - } else if strings.HasSuffix(txt, "replies") { + case strings.HasSuffix(txt, "replies"): l := strings.Split(txt, " ") tweet.Replies, _ = strconv.Atoi(l[0]) - } else if strings.HasSuffix(txt, "retweets") { + case strings.HasSuffix(txt, "retweets"): l := strings.Split(txt, " ") tweet.Retweets, _ = strconv.Atoi(l[0]) }