-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.go
130 lines (105 loc) · 2.66 KB
/
twitter.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
package main
/**
* Load recent tweets from an account.
*/
import (
"fmt"
"log"
"time"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
ui "github.com/gizak/termui"
)
////////////////////////////////////////////
// Util: Twitter
////////////////////////////////////////////
var twitterConfig = oauth1.NewConfig(GetTwitterConsumerKey(), GetTwitterConsumerSecret())
var twitterToken = oauth1.NewToken(GetTwitterAccessToken(), GetTwitterAccessTokenSecret())
var twitterHttpClient = twitterConfig.Client(oauth1.NoContext, twitterToken)
var twitterClient = twitter.NewClient(twitterHttpClient)
func newBool(myBool bool) *bool {
b := myBool
return &b
}
func GetLatestTweet(account string) string {
tweets, _, err := twitterClient.Timelines.UserTimeline(&twitter.UserTimelineParams{
ScreenName: account,
Count: 10,
TrimUser: newBool(true),
ExcludeReplies: newBool(true),
IncludeRetweets: newBool(false),
})
if err != nil {
log.Printf("Error loading tweets for '%v': %v", account, err)
} else if len(tweets) < 1 {
log.Printf("Failed to load any tweets for '%v'.", account)
} else {
t := tweets[0].Text
return t
}
return "(no data)"
}
////////////////////////////////////////////
// Widget: Twitter
////////////////////////////////////////////
const TwitterWidgetUpdateInterval = 10 * time.Minute
type TwitterWidget struct {
account string
color ui.Attribute
widget *ui.Paragraph
lastUpdated *time.Time
}
func NewTwitterWidget(account string, color ui.Attribute) *TwitterWidget {
// Create base element
e := ui.NewParagraph("")
e.Border = true
e.BorderLabel = fmt.Sprintf("@%s", account)
e.BorderLabelFg = ui.ColorGreen
e.TextFgColor = color
// Create widget
w := &TwitterWidget{
account: account,
color: color,
widget: e,
}
w.update()
w.resize()
return w
}
func (w *TwitterWidget) getGridWidget() ui.GridBufferer {
return w.widget
}
func (w *TwitterWidget) update() {
if shouldUpdate(w) {
// Get latest tweet
w.widget.Text = GetLatestTweet(w.account)
}
w.resize()
}
func (w *TwitterWidget) resize() {
borderCount := 0
if w.widget.Border {
borderCount = 2
}
// Make line wrapping better
wrap := w.widget.Width - borderCount
if wrap <= 0 {
wrap = 30
}
w.widget.WrapLength = wrap
// Guess at line count
height := borderCount + 1 + len(w.widget.Text)/wrap
if height < 7 {
height = 7
}
w.widget.Height = height
}
func (w *TwitterWidget) getUpdateInterval() time.Duration {
return TwitterWidgetUpdateInterval
}
func (w *TwitterWidget) getLastUpdated() *time.Time {
return w.lastUpdated
}
func (w *TwitterWidget) setLastUpdated(t time.Time) {
w.lastUpdated = &t
}