Skip to content

Commit

Permalink
feat: expand URLs for profile and tweets
Browse files Browse the repository at this point in the history
  • Loading branch information
thewh1teagle committed Oct 14, 2024
1 parent 033e860 commit 9f31f38
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
22 changes: 11 additions & 11 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ type (
Name string
}

// Url represents a URL with display, expanded, and index data.
Url struct {
DisplayURL string `json:"display_url"`
ExpandedURL string `json:"expanded_url"`
URL string `json:"url"`
Indices []int `json:"indices"`
}

// Photo type.
Photo struct {
ID string
Expand Down Expand Up @@ -105,10 +113,7 @@ type (
Type string `json:"type"`
URL string `json:"url"`
} `json:"media"`
URLs []struct {
ExpandedURL string `json:"expanded_url"`
URL string `json:"url"`
} `json:"urls"`
URLs []Url `json:"urls"`
UserMentions []struct {
IDStr string `json:"id_str"`
Name string `json:"name"`
Expand Down Expand Up @@ -195,15 +200,10 @@ type (
Description string `json:"description"`
Entities struct {
Description struct {
Urls []interface{} `json:"urls"`
Urls []Url `json:"urls"`
} `json:"description"`
URL struct {
Urls []struct {
DisplayURL string `json:"display_url"`
ExpandedURL string `json:"expanded_url"`
URL string `json:"url"`
Indices []int `json:"indices"`
} `json:"urls"`
Urls []Url `json:"urls"`
} `json:"url"`
} `json:"entities"`
FastFollowersCount int `json:"fast_followers_count"`
Expand Down
14 changes: 12 additions & 2 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func parseLegacyTweet(user *legacyUser, tweet *legacyTweet) *Tweet {
if tweetID == "" {
return nil
}
text := expandURLs(tweet.FullText, tweet.Entities.URLs)
username := user.ScreenName
name := user.Name
tw := &Tweet{
Expand All @@ -167,7 +168,7 @@ func parseLegacyTweet(user *legacyUser, tweet *legacyTweet) *Tweet {
PermanentURL: fmt.Sprintf("https://twitter.com/%s/status/%s", username, tweetID),
Replies: tweet.ReplyCount,
Retweets: tweet.RetweetCount,
Text: tweet.FullText,
Text: text,
UserID: tweet.UserIDStr,
Username: username,
}
Expand Down Expand Up @@ -379,12 +380,21 @@ func parseProfile(user legacyUser) Profile {
return profile
}

func expandURLs(text string, urls []Url) string {
expandedText := text
for _, url := range urls {
expandedText = strings.ReplaceAll(expandedText, url.URL, url.ExpandedURL)
}
return expandedText
}

func parseProfileV2(user userResult) Profile {
u := user.Legacy
description := expandURLs(u.Description, u.Entities.Description.Urls)
profile := Profile{
Avatar: u.ProfileImageURLHTTPS,
Banner: u.ProfileBannerURL,
Biography: u.Description,
Biography: description,
FollowersCount: u.FollowersCount,
FollowingCount: u.FavouritesCount,
FriendsCount: u.FriendsCount,
Expand Down

0 comments on commit 9f31f38

Please sign in to comment.