twitter-scrapper/profile.go

97 lines
2.2 KiB
Go
Raw Normal View History

2019-09-21 10:59:45 +03:00
package twitterscraper
import (
"fmt"
"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) {
userID, err := s.GetUserIDByScreenName(username)
if err != nil {
return Profile{}, err
}
2020-12-12 23:33:57 +02:00
req, err := s.newRequest("GET", "https://twitter.com/i/api/2/timeline/profile/"+userID+".json")
if err != nil {
return Profile{}, err
}
q := req.URL.Query()
q.Add("count", "20")
q.Add("userId", userID)
req.URL.RawQuery = q.Encode()
var timeline timeline
2020-12-12 23:33:57 +02:00
err = s.RequestAPI(req, &timeline)
2019-09-21 10:59:45 +03:00
if err != nil {
return Profile{}, err
}
user, found := timeline.GlobalObjects.Users[userID]
if !found {
2020-08-10 14:08:35 +03:00
return Profile{}, fmt.Errorf("either @%s does not exist or is private", username)
}
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: user.IDStr,
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)
}