Skip to content

Commit c269a9b

Browse files
authored
Merge pull request #24 from thewh1teagle/feat/expand-description-urls
feat: expand description URLs
2 parents 4418322 + e5c1709 commit c269a9b

File tree

3 files changed

+49
-31
lines changed

3 files changed

+49
-31
lines changed

timeline_v2.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ func (result *result) parse() *Tweet {
5656
if result.QuotedStatusResult.Result != nil {
5757
tw.QuotedStatus = result.QuotedStatusResult.Result.parse()
5858
}
59+
tw.HTML = expandURLs(tw.HTML, legacy.Entities.URLs, legacy.ExtendedEntities.Media)
60+
tw.HTML = expandURLs(tw.Text, legacy.Entities.URLs, legacy.ExtendedEntities.Media)
5961
return tw
6062
}
6163

types.go

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ type (
1010
Name string
1111
}
1212

13+
// Url represents a URL with display, expanded, and index data.
14+
Url struct {
15+
DisplayURL string `json:"display_url"`
16+
ExpandedURL string `json:"expanded_url"`
17+
URL string `json:"url"`
18+
Indices []int `json:"indices"`
19+
}
20+
1321
// Photo type.
1422
Photo struct {
1523
ID string
@@ -91,6 +99,25 @@ type (
9199
GIFs []GIF
92100
}
93101

102+
ExtendedMedia struct {
103+
IDStr string `json:"id_str"`
104+
MediaURLHttps string `json:"media_url_https"`
105+
ExtSensitiveMediaWarning struct {
106+
AdultContent bool `json:"adult_content"`
107+
GraphicViolence bool `json:"graphic_violence"`
108+
Other bool `json:"other"`
109+
} `json:"ext_sensitive_media_warning"`
110+
Type string `json:"type"`
111+
URL string `json:"url"`
112+
VideoInfo struct {
113+
Variants []struct {
114+
Type string `json:"content_type"`
115+
Bitrate int `json:"bitrate"`
116+
URL string `json:"url"`
117+
} `json:"variants"`
118+
} `json:"video_info"`
119+
}
120+
94121
legacyTweet struct {
95122
ConversationIDStr string `json:"conversation_id_str"`
96123
CreatedAt string `json:"created_at"`
@@ -105,35 +132,15 @@ type (
105132
Type string `json:"type"`
106133
URL string `json:"url"`
107134
} `json:"media"`
108-
URLs []struct {
109-
ExpandedURL string `json:"expanded_url"`
110-
URL string `json:"url"`
111-
} `json:"urls"`
135+
URLs []Url `json:"urls"`
112136
UserMentions []struct {
113137
IDStr string `json:"id_str"`
114138
Name string `json:"name"`
115139
ScreenName string `json:"screen_name"`
116140
} `json:"user_mentions"`
117141
} `json:"entities"`
118142
ExtendedEntities struct {
119-
Media []struct {
120-
IDStr string `json:"id_str"`
121-
MediaURLHttps string `json:"media_url_https"`
122-
ExtSensitiveMediaWarning struct {
123-
AdultContent bool `json:"adult_content"`
124-
GraphicViolence bool `json:"graphic_violence"`
125-
Other bool `json:"other"`
126-
} `json:"ext_sensitive_media_warning"`
127-
Type string `json:"type"`
128-
URL string `json:"url"`
129-
VideoInfo struct {
130-
Variants []struct {
131-
Type string `json:"content_type"`
132-
Bitrate int `json:"bitrate"`
133-
URL string `json:"url"`
134-
} `json:"variants"`
135-
} `json:"video_info"`
136-
} `json:"media"`
143+
Media []ExtendedMedia `json:"media"`
137144
} `json:"extended_entities"`
138145
IDStr string `json:"id_str"`
139146
InReplyToStatusIDStr string `json:"in_reply_to_status_id_str"`
@@ -210,15 +217,10 @@ type (
210217
Description string `json:"description"`
211218
Entities struct {
212219
Description struct {
213-
Urls []interface{} `json:"urls"`
220+
Urls []Url `json:"urls"`
214221
} `json:"description"`
215222
URL struct {
216-
Urls []struct {
217-
DisplayURL string `json:"display_url"`
218-
ExpandedURL string `json:"expanded_url"`
219-
URL string `json:"url"`
220-
Indices []int `json:"indices"`
221-
} `json:"urls"`
223+
Urls []Url `json:"urls"`
222224
} `json:"url"`
223225
} `json:"entities"`
224226
FastFollowersCount int `json:"fast_followers_count"`

util.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func parseLegacyTweet(user *legacyUser, tweet *legacyTweet) *Tweet {
157157
if tweetID == "" {
158158
return nil
159159
}
160+
text := expandURLs(tweet.FullText, tweet.Entities.URLs, tweet.ExtendedEntities.Media)
160161
username := user.ScreenName
161162
name := user.Name
162163
tw := &Tweet{
@@ -167,7 +168,7 @@ func parseLegacyTweet(user *legacyUser, tweet *legacyTweet) *Tweet {
167168
PermanentURL: fmt.Sprintf("https://twitter.com/%s/status/%s", username, tweetID),
168169
Replies: tweet.ReplyCount,
169170
Retweets: tweet.RetweetCount,
170-
Text: tweet.FullText,
171+
Text: text,
171172
UserID: tweet.UserIDStr,
172173
Username: username,
173174
}
@@ -382,12 +383,25 @@ func parseProfile(user legacyUser) Profile {
382383
return profile
383384
}
384385

386+
func expandURLs(text string, urls []Url, extendedMediaEntities []ExtendedMedia) string {
387+
expandedText := text
388+
for _, url := range urls {
389+
expandedText = strings.ReplaceAll(expandedText, url.URL, url.ExpandedURL)
390+
}
391+
for _, entity := range extendedMediaEntities {
392+
expandedText = strings.ReplaceAll(expandedText, entity.URL, entity.MediaURLHttps)
393+
}
394+
395+
return expandedText
396+
}
397+
385398
func parseProfileV2(user userResult) Profile {
386399
u := user.Legacy
400+
description := expandURLs(u.Description, u.Entities.Description.Urls, []ExtendedMedia{})
387401
profile := Profile{
388402
Avatar: u.ProfileImageURLHTTPS,
389403
Banner: u.ProfileBannerURL,
390-
Biography: u.Description,
404+
Biography: description,
391405
FollowersCount: u.FollowersCount,
392406
FollowingCount: u.FavouritesCount,
393407
FriendsCount: u.FriendsCount,

0 commit comments

Comments
 (0)