-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.go
222 lines (198 loc) · 5.49 KB
/
structs.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package twitter
import (
"encoding/json"
"fmt"
"net/url"
"path"
"strings"
"github.com/Ukraine-DAO/twitter-threads/common"
)
type ReferencedTweet struct {
Type string `json:"type"`
ID string `json:"id"`
}
type TwitterUser struct {
ID string `json:"id"`
Name string `json:"name"`
Username string `json:"username"`
}
type Media struct {
Type string `json:"type"`
Key string `json:"media_key"`
URL string `json:"url,omitempty"`
PreviewURL string `json:"preview_image_url,omitempty"`
Variants []map[string]interface{} `json:"variants,omitempty"`
AltText string `json:"alt_text,omitempty"`
}
type mediaVariant struct {
BitRate int `json:"bit_rate,omitempty"`
ContentType string `json:"content_type"`
URL string `json:"url"`
}
func (m Media) DisplayURLAndFilename() (string, string) {
switch m.Type {
case "video":
url, _ := url.Parse(m.PreviewURL)
parts := strings.Split(url.Path, ".")
ext := parts[len(parts)-1]
return m.PreviewURL, fmt.Sprintf("%s/preview.%s", m.Key, ext)
default:
url, _ := url.Parse(m.URL)
parts := strings.Split(url.Path, ".")
ext := parts[len(parts)-1]
return m.URL, fmt.Sprintf("%s.%s", m.Key, ext)
}
}
func (m Media) TargetURLAndFilename() (string, string) {
switch m.Type {
case "video":
bitrate := 0
u := ""
for _, v := range m.Variants {
mv := &mediaVariant{}
b, _ := json.Marshal(v)
if err := json.Unmarshal(b, mv); err != nil {
continue
}
if !strings.HasPrefix(mv.ContentType, "video/") {
continue
}
if mv.BitRate > bitrate {
bitrate = mv.BitRate
u = mv.URL
}
}
url, _ := url.Parse(u)
return u, fmt.Sprintf("%s/%s", m.Key, path.Base(url.Path))
default:
url, _ := url.Parse(m.URL)
parts := strings.Split(url.Path, ".")
ext := parts[len(parts)-1]
return m.URL, fmt.Sprintf("%s.%s", m.Key, ext)
}
}
func (m Media) FetchList() map[string]string {
r := map[string]string{}
switch m.Type {
case "video":
u, _ := url.Parse(m.PreviewURL)
parts := strings.Split(u.Path, ".")
ext := parts[len(parts)-1]
r[fmt.Sprintf("%s/preview.%s", m.Key, ext)] = m.PreviewURL
for _, v := range m.Variants {
mv := &mediaVariant{}
b, _ := json.Marshal(v)
if err := json.Unmarshal(b, mv); err != nil {
continue
}
if !strings.HasPrefix(mv.ContentType, "video/") {
continue
}
u, _ := url.Parse(mv.URL)
r[fmt.Sprintf("%s/%s", m.Key, path.Base(u.Path))] = mv.URL
}
default:
url, _ := url.Parse(m.URL)
parts := strings.Split(url.Path, ".")
ext := parts[len(parts)-1]
r[fmt.Sprintf("%s.%s", m.Key, ext)] = m.URL
}
return r
}
type TweetIncludes struct {
Users []TwitterUser `json:"users,omitempty"`
Media []Media `json:"media,omitempty"`
Tweets []TweetNoIncludes `json:"tweets,omitempty"`
}
type Entities struct {
URLs []EntityURL `json:"urls,omitempty"`
Hashtags []EntityHashtag `json:"hashtags,omitempty"`
Mentions []EntityMention `json:"mentions,omitempty"`
}
type TextEntity struct {
Start uint16 `json:"start"`
End uint16 `json:"end"`
}
type EntityURL struct {
TextEntity
URL string `json:"url,omitempty"`
ExpandedURL string `json:"expanded_url,omitempty"`
DisplayURL string `json:"display_url,omitempty"`
UnwoundURL string `json:"unwound_url,omitempty"`
Title string `json:"title,omitempty"`
}
type EntityHashtag struct {
TextEntity
Tag string `json:"tag,omitempty"`
}
type EntityMention struct {
TextEntity
Username string `json:"username,omitempty"`
}
type Attachments struct {
MediaKeys []string `json:"media_keys,omitempty"`
}
type TweetNoIncludes struct {
ID string `json:"id"`
Text string `json:"text"`
ConversationID string `json:"conversation_id"`
AuthorID string `json:"author_id"`
ReferencedTweets []ReferencedTweet `json:"referenced_tweets,omitempty"`
Entities Entities `json:"entities,omitempty"`
Attachments Attachments `json:"attachments,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
InReplyToUserID string `json:"in_reply_to_user_id,omitempty"`
}
type Tweet struct {
TweetNoIncludes
Includes TweetIncludes `json:"includes,omitempty"`
RequestConfig common.RequestConfig `json:",omitempty"`
}
func (t *Tweet) InReplyTo() string {
for _, ref := range t.ReferencedTweets {
if ref.Type == "replied_to" {
return ref.ID
}
}
return ""
}
func (t *Tweet) CopyIncludes(incl TweetIncludes) {
wantTweets := map[string]bool{}
for _, rt := range t.ReferencedTweets {
wantTweets[rt.ID] = true
}
for _, tt := range incl.Tweets {
if wantTweets[tt.ID] {
t.Includes.Tweets = append(t.Includes.Tweets, tt)
}
}
wantUserById := map[string]bool{
t.AuthorID: true,
}
wantUserByUsername := map[string]bool{}
for _, m := range t.Entities.Mentions {
wantUserByUsername[m.Username] = true
}
for _, tt := range t.Includes.Tweets {
wantUserById[tt.AuthorID] = true
}
for _, u := range incl.Users {
if wantUserById[u.ID] || wantUserByUsername[u.Username] {
t.Includes.Users = append(t.Includes.Users, u)
}
}
wantMedia := map[string]bool{}
for _, k := range t.Attachments.MediaKeys {
wantMedia[k] = true
}
for _, tt := range t.Includes.Tweets {
for _, k := range tt.Attachments.MediaKeys {
wantMedia[k] = true
}
}
for _, m := range incl.Media {
if wantMedia[m.Key] {
t.Includes.Media = append(t.Includes.Media, m)
}
}
}