-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
114 lines (96 loc) · 2.25 KB
/
main.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
// Newline format must be LF
package main
import (
"encoding/base64"
"fmt"
"github.com/ChimeraCoder/anaconda"
"io/ioutil"
"net/url"
"os"
"strings"
)
func main() {
api := GetTwitterApi()
if len(os.Args) < 2 {
fmt.Println(`
Usage:
twitter status_text
twitter -m media_filename1 status_text
twitter -m media_filename1 -m media_filename2 status_text (Up to 4 media)
twitter "-m foobar" (if status_text contains -m, must quote it.)
`)
os.Exit(255)
}
message, files := parse_message(os.Args)
var (
attachment url.Values = nil
media []string
)
if 4 < len(files) {
fmt.Println(os.Stderr, "Only 4 images can be posted at one time.")
os.Exit(-1)
} else if 0 < len(files) {
for _, file := range files {
media = append(media, upload_image(api, file))
}
attachment = url.Values{}
attachment.Add("media_ids", strings.Join(media, ","))
}
tweet, err := api.PostTweet(message, attachment)
if err != nil {
fmt.Println(os.Stderr, err)
os.Exit(-1)
}
fmt.Print(tweet.Text)
}
func upload_image(api *anaconda.TwitterApi, filename string) string {
media, err := api.UploadMedia(load_image(filename))
if err != nil {
fmt.Println(os.Stderr, err)
os.Exit(-1)
}
return media.MediaIDString
}
func load_image(path string) string {
data, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println(os.Stderr, err)
os.Exit(-1)
}
return base64.StdEncoding.EncodeToString(data)
}
func parse_message(args []string) (string, []string) {
var (
text string = ""
image_filename []string
image_flag bool
)
for i, v := range args {
if i == 0 {
continue // skip first arg value (It is name of executable)
}
if v == "-m" { // if -m flag found, next args is image filename
image_flag = true
continue
}
if image_flag == true {
image_filename = append(image_filename, v)
image_flag = false
} else {
text += " " + v
}
}
return text, image_filename
}
func GetTwitterApi() *anaconda.TwitterApi {
anaconda.SetConsumerKey(consumer_token)
anaconda.SetConsumerSecret(consumer_secret)
// If token is missing, get it
_, err := os.Stat(token_savefile)
if err != nil {
save_token(get_token())
}
access_token, access_secret := load_token()
api := anaconda.NewTwitterApi(access_token, access_secret)
return api
}