Code optimization
This commit is contained in:
parent
a4375c3b2d
commit
6c4a1cc054
4 changed files with 28 additions and 42 deletions
31
api.go
31
api.go
|
|
@ -90,34 +90,3 @@ func (s *Scraper) GetGuestToken() error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserIDByScreenName from API
|
|
||||||
func (s *Scraper) GetUserIDByScreenName(screenName string) (string, error) {
|
|
||||||
id, ok := cacheIDs.Load(screenName)
|
|
||||||
if ok {
|
|
||||||
return id.(string), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var jsn user
|
|
||||||
req, err := http.NewRequest("GET", "https://api.twitter.com/graphql/4S2ihIKfF3xhp-ENxvUAfQ/UserByScreenName?variables=%7B%22screen_name%22%3A%22"+screenName+"%22%2C%22withHighlightedLabel%22%3Atrue%7D", nil)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = s.RequestAPI(req, &jsn)
|
|
||||||
if err != nil {
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
cacheIDs.Store(screenName, jsn.Data.User.RestID)
|
|
||||||
|
|
||||||
return jsn.Data.User.RestID, nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
11
api_test.go
11
api_test.go
|
|
@ -13,14 +13,3 @@ func TestGetGuestToken(t *testing.T) {
|
||||||
t.Error("Expected non-empty guestToken")
|
t.Error("Expected non-empty guestToken")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetUserIDByScreenName(t *testing.T) {
|
|
||||||
scraper := New()
|
|
||||||
userID, err := scraper.GetUserIDByScreenName("Twitter")
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("getUserByScreenName() error = %v", err)
|
|
||||||
}
|
|
||||||
if userID == "" {
|
|
||||||
t.Error("Expected non-empty user ID")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
17
profile.go
17
profile.go
|
|
@ -94,3 +94,20 @@ func (s *Scraper) GetProfile(username string) (Profile, error) {
|
||||||
func GetProfile(username string) (Profile, error) {
|
func GetProfile(username string) (Profile, error) {
|
||||||
return defaultScraper.GetProfile(username)
|
return defaultScraper.GetProfile(username)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetUserIDByScreenName from API
|
||||||
|
func (s *Scraper) GetUserIDByScreenName(screenName string) (string, error) {
|
||||||
|
id, ok := cacheIDs.Load(screenName)
|
||||||
|
if ok {
|
||||||
|
return id.(string), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
profile, err := s.GetProfile(screenName)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheIDs.Store(screenName, profile.UserID)
|
||||||
|
|
||||||
|
return profile.UserID, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -129,3 +129,14 @@ func TestGetProfileErrorNotFound(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetUserIDByScreenName(t *testing.T) {
|
||||||
|
scraper := New()
|
||||||
|
userID, err := scraper.GetUserIDByScreenName("Twitter")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("getUserByScreenName() error = %v", err)
|
||||||
|
}
|
||||||
|
if userID == "" {
|
||||||
|
t.Error("Expected non-empty user ID")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue