replace GetProfile with single call and improve error handling to fetch private profiles

This commit is contained in:
Michael 2021-01-25 10:31:41 +07:00
parent 1e048200bc
commit d33882ff94
5 changed files with 126 additions and 16 deletions

11
api.go
View file

@ -15,8 +15,12 @@ type user struct {
Data struct {
User struct {
RestID string `json:"rest_id"`
Legacy User `json:"legacy"`
} `json:"user"`
} `json:"data"`
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
}
// Global cache for user IDs
@ -40,7 +44,8 @@ func (s *Scraper) RequestAPI(req *http.Request, target interface{}) error {
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
// private profiles return forbidden, but also data
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusForbidden {
return fmt.Errorf("response status %s", resp.Status)
}
@ -104,6 +109,10 @@ func (s *Scraper) GetUserIDByScreenName(screenName string) (string, error) {
return "", err
}
if len(jsn.Errors) > 0 {
return "", fmt.Errorf("%s", jsn.Errors[0].Message)
}
if jsn.Data.User.RestID == "" {
return "", fmt.Errorf("rest_id not found")
}