/
murinus.go
212 lines (172 loc) · 4.85 KB
/
murinus.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
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/signal"
"os/user"
"path"
"strconv"
"strings"
"syscall"
"text/template"
"github.com/ChimeraCoder/anaconda"
"github.com/go-ini/ini"
)
const (
CONFIG_FILENAME = ".murinus"
DEFAULT_COUNT int = 10
)
var (
CONSUMER_KEY string
CONSUMER_SECRET string
ACCESS_TOKEN string
ACCESS_TOKEN_SECRET string
)
func isConfigurationComplete() error {
if len(CONSUMER_KEY) == 0 || len(CONSUMER_SECRET) == 0 || len(ACCESS_TOKEN) == 0 || len(ACCESS_TOKEN_SECRET) == 0 {
return errors.New("Configuration not complete!")
}
return nil
}
func checkEnvironment() error {
CONSUMER_KEY = os.Getenv("CONSUMER_KEY")
CONSUMER_SECRET = os.Getenv("CONSUMER_SECRET")
ACCESS_TOKEN = os.Getenv("ACCESS_TOKEN")
ACCESS_TOKEN_SECRET = os.Getenv("ACCESS_TOKEN_SECRET")
return isConfigurationComplete()
}
func checkConfigFile() error {
usr, err := user.Current()
if err != nil {
return err
}
cfg, err := ini.Load(path.Join(usr.HomeDir, CONFIG_FILENAME))
if err != nil {
return err
}
CONSUMER_KEY = cfg.Section("").Key("CONSUMER_KEY").String()
CONSUMER_SECRET = cfg.Section("").Key("CONSUMER_SECRET").String()
ACCESS_TOKEN = cfg.Section("").Key("ACCESS_TOKEN").String()
ACCESS_TOKEN_SECRET = cfg.Section("").Key("ACCESS_TOKEN_SECRET").String()
return isConfigurationComplete()
}
func setupTwitterApi() *anaconda.TwitterApi {
if err := checkEnvironment(); err != nil {
if err := checkConfigFile(); err != nil {
fmt.Println(err)
os.Exit(1)
} else {
fmt.Println("Configuration read from configuration file.")
}
} else {
fmt.Println("Configuration read from environment.")
}
anaconda.SetConsumerKey(CONSUMER_KEY)
anaconda.SetConsumerSecret(CONSUMER_SECRET)
return anaconda.NewTwitterApi(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
}
func flushWriter(w *bufio.Writer) {
if err := w.Flush(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func createFile(file string) *os.File {
f, err := os.Create(file)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return f
}
func getTweetTemplate() *template.Template {
tmpl, err := ioutil.ReadFile("templates/tweet.tmpl")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return template.Must(template.New("tweet").Parse(string(tmpl)))
}
func writeTimeline(w *bufio.Writer, timeline []anaconda.Tweet) (int64, error) {
var lastTweetId int64
t := getTweetTemplate()
for _, tweet := range timeline {
lastTweetId = tweet.Id
if err := t.Execute(w, tweet); err != nil {
return lastTweetId, err
}
}
return lastTweetId, nil
}
func parseArguments() {
flag.Int("user-id", 0, "The ID of the user for whom to return results for.")
flag.String("screen-name", "", "The screen name of the user for whom to return results for.")
flag.Int("since-id", 0, "Returns results with an ID greater than (that is, more recent than) the specified ID.")
flag.Int("max-id", 0, "Returns results with an ID less than (that is, older than) or equal to the specified ID.")
flag.Bool("trim-user", true, "When set to true each tweet returned in a timeline will include a user object including only the status authors numerical ID.")
flag.Bool("include-rts", true, "When set to false, the timeline will strip any native retweets. Note: If you’re using the trim parameter in conjunction with includerts, the retweets will still contain a full user object.")
flag.Int("count", DEFAULT_COUNT, "Specifies the number of tweets to try and retrieve, up to a maximum of 200 per distinct request.")
flag.Parse()
}
func getQueryParams() url.Values {
v := url.Values{}
// Apply defaults
v.Set("count", strconv.Itoa(DEFAULT_COUNT))
v.Set("trim_user", "true")
// Apply set flags
flag.Visit(func(f *flag.Flag) {
param := strings.Replace(f.Name, "-", "_", -1)
v.Set(param, fmt.Sprintf("%v", f.Value))
})
return v
}
func main() {
parseArguments()
// setup catching of SIGINT and SIGTERM signals
var done bool
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
fmt.Println("Signal catched... exiting...")
done = true
}()
f := createFile("timeline.json")
defer f.Close()
w := bufio.NewWriter(f)
api := setupTwitterApi()
var fetchedTweets int
queryParams := getQueryParams()
fmt.Println(queryParams)
for {
if done {
fmt.Printf("\nFetched %d tweets!\n", fetchedTweets)
flushWriter(w)
os.Exit(0)
}
timeline, err := api.GetUserTimeline(queryParams)
if err != nil {
flushWriter(w)
fmt.Println(err)
os.Exit(1)
}
lastTweetId, err := writeTimeline(w, timeline)
if err != nil {
flushWriter(w)
fmt.Println(err)
os.Exit(1)
}
queryParams.Set("max_id", strconv.FormatInt(lastTweetId-1, 10))
fetchedTweets += len(timeline)
if len(timeline) == 0 {
fmt.Printf("\nNo more tweets left... Fetched %d tweets!\n", fetchedTweets)
break
}
fmt.Print(".")
}
}