77 lines
2.0 KiB
Go
Raw Normal View History

2021-05-22 18:20:18 -04:00
package scraper
2021-06-13 14:34:20 -07:00
import (
"time"
"fmt"
2021-07-22 14:16:40 -07:00
"strings"
2021-06-13 14:34:20 -07:00
)
2021-05-22 18:20:18 -04:00
type UserID string
2021-06-16 13:14:56 -07:00
type UserHandle string
2021-06-13 14:34:20 -07:00
2021-07-22 14:16:40 -07:00
func JoinArrayOfHandles(handles []UserHandle) string {
2021-06-16 19:31:27 -07:00
ret := []string{}
2021-07-22 14:16:40 -07:00
for _, h := range handles {
ret = append(ret, string(h))
2021-06-16 19:31:27 -07:00
}
2021-07-22 14:16:40 -07:00
return strings.Join(ret, ",")
2021-06-16 19:31:27 -07:00
}
2021-06-13 14:34:20 -07:00
type User struct {
ID UserID
DisplayName string
2021-06-16 13:14:56 -07:00
Handle UserHandle
2021-06-13 14:34:20 -07:00
Bio string
FollowingCount int
FollowersCount int
Location string
Website string
JoinDate time.Time
IsPrivate bool
IsVerified bool
ProfileImageUrl string
BannerImageUrl string
PinnedTweetID TweetID
PinnedTweet *Tweet
2021-06-13 14:34:20 -07:00
}
func (u User) String() string {
return fmt.Sprintf("%s (@%s)[%s]: %q", u.DisplayName, u.Handle, u.ID, u.Bio)
}
// Turn an APIUser, as returned from the scraper, into a properly structured User object
func ParseSingleUser(apiUser APIUser) (ret User, err error) {
ret.ID = UserID(apiUser.IDStr)
ret.DisplayName = apiUser.Name
2021-06-16 13:14:56 -07:00
ret.Handle = UserHandle(apiUser.ScreenName)
2021-06-13 14:34:20 -07:00
ret.Bio = apiUser.Description
ret.FollowingCount = apiUser.FriendsCount
ret.FollowersCount = apiUser.FollowersCount
ret.Location = apiUser.Location
if len(apiUser.Entities.URL.Urls) > 0 {
ret.Website = apiUser.Entities.URL.Urls[0].ExpandedURL
}
ret.JoinDate, err = time.Parse(time.RubyDate, apiUser.CreatedAt)
if err != nil {
return
}
ret.IsPrivate = apiUser.Protected
ret.IsVerified = apiUser.Verified
ret.ProfileImageUrl = apiUser.ProfileImageURLHTTPS
ret.BannerImageUrl = apiUser.ProfileBannerURL
if len(apiUser.PinnedTweetIdsStr) > 0 {
ret.PinnedTweetID = TweetID(apiUser.PinnedTweetIdsStr[0])
2021-06-13 14:34:20 -07:00
}
return
}
2021-06-16 19:31:27 -07:00
// Calls API#GetUser and returns the parsed result
func GetUser(handle UserHandle) (User, error) {
api := API{}
apiUser, err := api.GetUser(handle)
if err != nil {
return User{}, err
}
return ParseSingleUser(apiUser)
}