twitter-scrapper/profile.go

97 lines
2.4 KiB
Go
Raw Normal View History

2019-09-21 10:59:45 +03:00
package twitterscraper
import (
"fmt"
"net/http"
2019-09-21 10:59:45 +03:00
"time"
)
// Profile of twitter user.
2019-09-21 10:59:45 +03:00
type Profile struct {
Avatar string
2020-06-15 15:17:08 +03:00
Banner string
2019-09-21 10:59:45 +03:00
Biography string
Birthday string
FollowersCount int
FollowingCount int
FriendsCount int
IsPrivate bool
IsVerified bool
2019-09-21 10:59:45 +03:00
Joined *time.Time
LikesCount int
ListedCount int
2019-09-21 10:59:45 +03:00
Location string
Name string
PinnedTweetIDs []string
2019-09-21 10:59:45 +03:00
TweetsCount int
URL string
UserID string
2019-09-21 10:59:45 +03:00
Username string
Website string
}
// GetProfile return parsed user profile.
2020-12-12 23:33:57 +02:00
func (s *Scraper) GetProfile(username string) (Profile, error) {
var jsn user
req, err := http.NewRequest("GET", "https://api.twitter.com/graphql/4S2ihIKfF3xhp-ENxvUAfQ/UserByScreenName?variables=%7B%22screen_name%22%3A%22"+username+"%22%2C%22withHighlightedLabel%22%3Atrue%7D", nil)
if err != nil {
return Profile{}, err
}
err = s.RequestAPI(req, &jsn)
if err != nil {
return Profile{}, err
}
if len(jsn.Errors) > 0 {
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
}
if jsn.Data.User.RestID == "" {
return Profile{}, fmt.Errorf("rest_id not found")
2019-09-21 10:59:45 +03:00
}
if jsn.Data.User.Legacy.ScreenName == "" {
2020-08-10 14:08:35 +03:00
return Profile{}, fmt.Errorf("either @%s does not exist or is private", username)
}
user := jsn.Data.User.Legacy
profile := Profile{
Avatar: user.ProfileImageURLHTTPS,
Banner: user.ProfileBannerURL,
Biography: user.Description,
FollowersCount: user.FollowersCount,
FollowingCount: user.FavouritesCount,
FriendsCount: user.FriendsCount,
IsPrivate: user.Protected,
IsVerified: user.Verified,
LikesCount: user.FavouritesCount,
ListedCount: user.ListedCount,
Location: user.Location,
Name: user.Name,
PinnedTweetIDs: user.PinnedTweetIdsStr,
TweetsCount: user.StatusesCount,
URL: "https://twitter.com/" + user.ScreenName,
UserID: jsn.Data.User.RestID,
Username: user.ScreenName,
}
tm, err := time.Parse(time.RubyDate, user.CreatedAt)
if err == nil {
tm = tm.UTC()
profile.Joined = &tm
}
if len(user.Entities.URL.Urls) > 0 {
profile.Website = user.Entities.URL.Urls[0].ExpandedURL
}
2019-09-21 10:59:45 +03:00
return profile, nil
2019-09-21 10:59:45 +03:00
}
2020-12-12 23:33:57 +02:00
// GetProfile wrapper for default scraper
func GetProfile(username string) (Profile, error) {
return defaultScraper.GetProfile(username)
}