-
Notifications
You must be signed in to change notification settings - Fork 0
/
gotter.go
237 lines (221 loc) · 5.94 KB
/
gotter.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 gotter
import (
"bufio"
"exec"
"github.com/garyburd/twister/oauth"
"github.com/garyburd/twister/web"
"http"
"iconv"
"io/ioutil"
"json"
"log"
"os"
"path/filepath"
"strings"
"syscall"
)
type Tweet struct {
Text string
Identifier string "id_str"
Source string
CreatedAt string "created_at"
User struct {
Name string
ScreenName string "screen_name"
FollowersCount int "followers_count"
ProfileImageURL string "profile_image_url"
}
Place *struct {
Id string
FullName string "full_name"
}
Entities struct {
HashTags []struct {
Indices [2]int
Text string
}
UserMentions []struct {
Indices [2]int
ScreenName string "screen_name"
} "user_mentions"
Urls []struct {
Indices [2]int
Url string
}
}
}
var oauthClient = oauth.Client{
TemporaryCredentialRequestURI: "https://api.twitter.com/oauth/request_token",
ResourceOwnerAuthorizationURI: "https://api.twitter.com/oauth/authenticate",
TokenRequestURI: "https://api.twitter.com/oauth/access_token",
}
func ClientAuth(requestToken *oauth.Credentials) (*oauth.Credentials, os.Error) {
cmd := "xdg-open"
url := oauthClient.AuthorizationURL(requestToken)
args := []string{cmd, url}
if syscall.OS == "windows" {
cmd = "rundll32.exe"
args = []string{cmd, "url.dll,FileProtocolHandler", url}
} else if syscall.OS == "darwin" {
cmd = "open"
args = []string{cmd, url}
}
cmd, err := exec.LookPath(cmd)
if err != nil {
log.Fatal("command not found:", err)
}
p, err := os.StartProcess(cmd, args, &os.ProcAttr{Dir: "", Files: []*os.File{nil, nil, os.Stderr}})
if err != nil {
log.Fatal("failed to start command:", err)
}
defer p.Release()
print("PIN: ")
stdin := bufio.NewReader(os.Stdin)
b, err := stdin.ReadBytes('\n')
if err != nil {
log.Fatal("canceled")
}
if b[len(b)-2] == '\r' {
b = b[0:len(b)-2]
} else {
b = b[0:len(b)-1]
}
accessToken, _, err := oauthClient.RequestToken(requestToken, string(b))
if err != nil {
log.Fatal("failed to request token:", err)
}
return accessToken, nil
}
func GetAccessToken(config map[string]string) (*oauth.Credentials, bool, os.Error) {
oauthClient.Credentials.Token = config["ClientToken"]
oauthClient.Credentials.Secret = config["ClientSecret"]
authorized := false
var token *oauth.Credentials
accessToken, foundToken := config["AccessToken"]
accessSecert, foundSecret := config["AccessSecret"]
if foundToken && foundSecret {
token = &oauth.Credentials{accessToken, accessSecert}
} else {
requestToken, err := oauthClient.RequestTemporaryCredentials("")
if err != nil {
log.Print("failed to request temporary credentials:", err)
return nil, false, err
}
token, err = ClientAuth(requestToken)
if err != nil {
log.Print("failed to request temporary credentials:", err)
return nil, false, err
}
config["AccessToken"] = token.Token
config["AccessSecret"] = token.Secret
authorized = true
}
return token, authorized, nil
}
func GetTweets(token *oauth.Credentials, url string, opt map[string]string) ([]Tweet, os.Error) {
param := make(web.ParamMap)
for k, v := range opt {
param.Set(k, v)
}
oauthClient.SignParam(token, "GET", url, param)
url = url + "?" + param.FormEncodedString()
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, err
}
var tweets []Tweet
err = json.NewDecoder(res.Body).Decode(&tweets)
if err != nil {
return nil, err
}
return tweets, nil
}
func convert_utf8(s string) string {
ic, err := iconv.Open("char", "UTF-8")
if err != nil {
return s
}
defer ic.Close()
ret, _ := ic.Conv(s)
return ret
}
func ShowTweets(tweets []Tweet, verbose bool) {
if verbose {
for i := len(tweets) - 1; i >= 0; i-- {
name := convert_utf8(tweets[i].User.Name)
user := convert_utf8(tweets[i].User.ScreenName)
text := tweets[i].Text
text = strings.Replace(text, "\r", "", -1)
text = strings.Replace(text, "\n", " ", -1)
text = strings.Replace(text, "\t", " ", -1)
text = convert_utf8(text)
println(user + ": " + name)
println(" " + text)
println(" " + tweets[i].Identifier)
println(" " + tweets[i].CreatedAt)
println()
}
} else {
for i := len(tweets) - 1; i >= 0; i-- {
user := convert_utf8(tweets[i].User.ScreenName)
text := convert_utf8(tweets[i].Text)
println(user + ": " + text)
}
}
}
func PostTweet(token *oauth.Credentials, url string, opt map[string]string) os.Error {
param := make(web.ParamMap)
for k, v := range opt {
param.Set(k, v)
}
oauthClient.SignParam(token, "POST", url, param)
res, err := http.PostForm(url, param.StringMap())
if err != nil {
log.Println("failed to post tweet:", err)
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Println("failed to get timeline:", err)
return err
}
return nil
}
func GetConfig() (string, map[string]string) {
home := os.Getenv("HOME")
dir := filepath.Join(home, ".config")
if syscall.OS == "windows" {
home = os.Getenv("USERPROFILE")
dir = filepath.Join(home, "Application Data")
}
_, err := os.Stat(dir)
if err != nil {
if os.Mkdir(dir, 0700) != nil {
log.Fatal("failed to create directory:", err)
}
}
dir = filepath.Join(dir, "twty")
_, err = os.Stat(dir)
if err != nil {
if os.Mkdir(dir, 0700) != nil {
log.Fatal("failed to create directory:", err)
}
}
file := filepath.Join(dir, "settings.json")
config := map[string]string{}
b, err := ioutil.ReadFile(file)
if err != nil {
config["ClientToken"] = "lhCgJRAE1ECQzwVXfs5NQ"
config["ClientSecret"] = "qk9i30vuzWHspsRttKsYrnoKSw9XBmWHdsis76z4"
} else {
err = json.Unmarshal(b, &config)
if err != nil {
log.Fatal("could not unmarhal settings.json:", err)
}
}
return file, config
}