forked from g8rswimmer/go-twitter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tweet_dictionary.go
107 lines (93 loc) · 2.77 KB
/
tweet_dictionary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package twitter
// TweetDictionary is a struct of a tweet and all of the reference objects
type TweetDictionary struct {
Tweet TweetObj
Author *UserObj
InReplyUser *UserObj
Place *PlaceObj
AttachmentPolls []*PollObj
AttachmentMedia []*MediaObj
Mentions []*TweetMention
ReferencedTweets []*TweetReference
}
// TweetMention is the mention and the user associated with it
type TweetMention struct {
Mention *EntityMentionObj
User *UserObj
}
// TweetReference is the tweet referenced and it's dictionary
type TweetReference struct {
Reference *TweetReferencedTweetObj
TweetDictionary *TweetDictionary
}
// CreateTweetDictionary will create a dictionary from a tweet and the includes
func CreateTweetDictionary(tweet TweetObj, includes *TweetRawIncludes) *TweetDictionary {
dictionary := &TweetDictionary{
Tweet: tweet,
AttachmentMedia: []*MediaObj{},
AttachmentPolls: []*PollObj{},
Mentions: []*TweetMention{},
ReferencedTweets: []*TweetReference{},
}
if includes == nil {
return dictionary
}
userIDs := includes.UsersByID()
if user, has := userIDs[tweet.AuthorID]; has {
dictionary.Author = user
}
if user, has := userIDs[tweet.InReplyToUserID]; has {
dictionary.InReplyUser = user
}
if tweet.Entities != nil {
userNames := includes.UsersByUserName()
mentions := []*TweetMention{}
for i, entity := range tweet.Entities.Mentions {
if user, has := userNames[entity.UserName]; has {
mention := &TweetMention{
Mention: &tweet.Entities.Mentions[i],
User: user,
}
mentions = append(mentions, mention)
}
}
dictionary.Mentions = mentions
}
if tweet.Attachments != nil {
pollIDs := includes.PollsByID()
attachmentPolls := []*PollObj{}
for _, id := range tweet.Attachments.PollIDs {
if poll, has := pollIDs[id]; has {
attachmentPolls = append(attachmentPolls, poll)
}
}
dictionary.AttachmentPolls = attachmentPolls
mediaKeys := includes.MediaByKeys()
attachmentMedia := []*MediaObj{}
for _, key := range tweet.Attachments.MediaKeys {
if media, has := mediaKeys[key]; has {
attachmentMedia = append(attachmentMedia, media)
}
}
dictionary.AttachmentMedia = attachmentMedia
}
if tweet.Geo != nil {
placeIDs := includes.PlacesByID()
if place, has := placeIDs[tweet.Geo.PlaceID]; has {
dictionary.Place = place
}
}
tweets := includes.TweetsByID()
tweetReferences := []*TweetReference{}
for i, rt := range tweet.ReferencedTweets {
if t, has := tweets[rt.ID]; has {
ref := &TweetReference{
Reference: tweet.ReferencedTweets[i],
TweetDictionary: CreateTweetDictionary(*t, includes),
}
tweetReferences = append(tweetReferences, ref)
}
}
dictionary.ReferencedTweets = tweetReferences
return dictionary
}