-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraytter.go
237 lines (220 loc) · 5.3 KB
/
traytter.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"fmt"
"log"
"os/exec"
"strings"
"time"
"github.com/caseymrm/menuet"
)
// Tweet represents one tweeter
type Tweet struct {
Text string
ID string
Username string
Author string
Timestamp time.Time
AvatarURL string
}
func (t *Tweet) href() string {
return fmt.Sprintf("https://twitter.com/%s/status/%s", t.Username, t.ID)
}
func (t *Tweet) open() {
exec.Command("open", t.href()).Run()
}
// Item returns a short menu item for the tweet, trucated as requested
func (t *Tweet) Item(truncate int) menuet.MenuItem {
text := t.Text
if len(text) > truncate-2 {
text = fmt.Sprintf("%s...", t.Text[0:truncate-3])
}
item := menuet.MenuItem{
Text: text,
FontWeight: menuet.WeightUltraLight,
Clicked: t.open,
Image: t.AvatarURL,
}
if text != t.Text {
item.Children = func() []menuet.MenuItem {
return t.FullItems()
}
}
return item
}
// FullItems returns several menu items for the tweet
func (t *Tweet) FullItems() []menuet.MenuItem {
lines := wrap(t.Text, 52)
items := make([]menuet.MenuItem, 0, len(lines)+1)
items = append(items, menuet.MenuItem{
Text: fmt.Sprintf("@%s - %s", t.Username, t.Timestamp.Format("Mon Jan 2 3:04pm")),
Clicked: t.open,
FontWeight: menuet.WeightBold,
Image: t.AvatarURL,
})
for _, line := range lines {
items = append(items, menuet.MenuItem{
Text: line,
Clicked: t.open,
FontWeight: menuet.WeightUltraLight,
})
}
return items
}
func wrap(text string, width int) []string {
lines := make([]string, 0, len(text)/width)
words := strings.Fields(text)
if len(words) == 0 {
return lines
}
current := words[0]
remaining := width - len(current)
for _, word := range words[1:] {
if len(word)+1 > remaining {
lines = append(lines, current)
current = word
remaining = width - len(word)
} else {
current += " " + word
remaining -= 1 + len(word)
}
}
lines = append(lines, current)
return lines
}
var usernames []string
var tweets map[string][]Tweet
func checkTwitter() {
ticker := time.NewTicker(10 * time.Minute)
for ; true; <-ticker.C {
err := fetchAllTweets()
if err != nil {
log.Printf("Error: %v", err)
continue
}
setTitle()
menuet.App().MenuChanged()
}
}
func setTitle() {
title := "🐦"
avatar := ""
if len(usernames) > 0 && len(tweets[usernames[0]]) > 0 {
tweet := tweets[usernames[0]][0]
avatar = tweet.AvatarURL
title = getKeyword(tweet.Text)
}
menuet.App().SetMenuState(&menuet.MenuState{
Title: title,
Image: avatar,
})
}
func follow(username string) {
username = strings.Trim(username, "@ ")
for _, name := range usernames {
if strings.EqualFold(name, username) {
return
}
}
newUsername, newTweets, err := fetchTweets(username)
if err != nil {
menuet.App().Alert(menuet.Alert{
MessageText: fmt.Sprintf("Could not fetch %s", username),
InformativeText: err.Error(),
})
return
}
tweets[newUsername] = newTweets
setTitle()
usernames = append(usernames, newUsername)
menuet.Defaults().Marshal("usernames", usernames)
}
func remove(username string) {
for ind, name := range usernames {
if name == username {
usernames = append(usernames[:ind], usernames[ind+1:]...)
delete(tweets, username)
setTitle()
menuet.Defaults().Marshal("usernames", usernames)
return
}
}
}
func menuItems() []menuet.MenuItem {
items := make([]menuet.MenuItem, 0, 2*len(usernames)+2)
for _, username := range usernames {
username := username
if len(tweets[username]) > 0 {
tweet := tweets[username][0]
items = append(items, menuet.MenuItem{
Text: fmt.Sprintf("@%s %s", username, getKeyword(tweet.Text)),
Clicked: tweet.open,
Children: func() []menuet.MenuItem {
return usernameItems(username)
},
Image: tweet.AvatarURL,
})
} else {
items = append(items, menuet.MenuItem{
Text: fmt.Sprintf("@%s", username),
Children: func() []menuet.MenuItem {
return usernameItems(username)
},
})
}
}
items = append(items, menuet.MenuItem{
Type: menuet.Separator,
})
items = append(items, menuet.MenuItem{
Text: "Follow a user",
Clicked: func() {
response := menuet.App().Alert(menuet.Alert{
MessageText: "What Twitter user would you like to follow?",
Inputs: []string{"@username"},
Buttons: []string{"Follow", "Cancel"},
})
if response.Button == 0 && len(response.Inputs) == 1 && response.Inputs[0] != "" {
follow(response.Inputs[0])
}
},
})
return items
}
func usernameItems(username string) []menuet.MenuItem {
recent := tweets[username]
items := make([]menuet.MenuItem, 0, len(recent))
if len(recent) == 0 {
items = append(items, menuet.MenuItem{
Text: "No tweets!",
})
} else {
items = append(items, menuet.MenuItem{
Text: "Recent tweets",
FontSize: 9,
})
for _, tweet := range recent {
tweet := tweet
items = append(items, tweet.Item(50))
}
}
items = append(items, menuet.MenuItem{
Type: menuet.Separator,
})
items = append(items, menuet.MenuItem{
Text: fmt.Sprintf("Remove @%s", username),
Clicked: func() {
remove(username)
},
})
return items
}
func main() {
go checkTwitter()
app := menuet.App()
app.Name = "Traytter"
app.Label = "com.github.caseymrm.traytter"
app.Children = menuItems
app.AutoUpdate.Version = "v0.1"
app.AutoUpdate.Repo = "caseymrm/traytter"
app.RunApplication()
}