/
main.go
125 lines (117 loc) · 3.37 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
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2011 Arne Roomann-Kurrik
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"fmt"
"github.com/kurrik/oauth1a"
"github.com/kurrik/twittergo"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"strings"
"time"
)
func LoadCredentials() (client *twittergo.Client, err error) {
credentials, err := ioutil.ReadFile("CREDENTIALS")
if err != nil {
return
}
lines := strings.Split(string(credentials), "\n")
config := &oauth1a.ClientConfig{
ConsumerKey: lines[0],
ConsumerSecret: lines[1],
}
user := oauth1a.NewAuthorizedConfig(lines[2], lines[3])
client = twittergo.NewClient(config, user)
return
}
func GetBody() (body io.ReadWriter, header string, err error) {
var (
mp *multipart.Writer
media []byte
writer io.Writer
)
body = bytes.NewBufferString("")
mp = multipart.NewWriter(body)
media, err = ioutil.ReadFile("tweet_media/media.png")
if err != nil {
return
}
mp.WriteField("status", fmt.Sprintf("Hello %v!", time.Now()))
writer, err = mp.CreateFormFile("media[]", "media.png")
if err != nil {
return
}
writer.Write(media)
header = fmt.Sprintf("multipart/form-data;boundary=%v", mp.Boundary())
mp.Close()
return
}
func main() {
var (
err error
client *twittergo.Client
req *http.Request
resp *twittergo.APIResponse
tweet *twittergo.Tweet
)
client, err = LoadCredentials()
if err != nil {
fmt.Printf("Could not parse CREDENTIALS file: %v\n", err)
os.Exit(1)
}
body, header, err := GetBody()
if err != nil {
fmt.Printf("Problem loading body: %v\n", err)
os.Exit(1)
}
endpoint := "/1.1/statuses/update_with_media.json"
req, err = http.NewRequest("POST", endpoint, body)
if err != nil {
fmt.Printf("Could not parse request: %v\n", err)
os.Exit(1)
}
req.Header.Set("Content-Type", header)
resp, err = client.SendRequest(req)
if err != nil {
fmt.Printf("Could not send request: %v\n", err)
os.Exit(1)
}
tweet = &twittergo.Tweet{}
err = resp.Parse(tweet)
if err != nil {
fmt.Printf("Problem parsing response: %v\n", err)
os.Exit(1)
}
fmt.Printf("ID: %v\n", tweet.Id())
fmt.Printf("Tweet: %v\n", tweet.Text())
fmt.Printf("User: %v\n", tweet.User().Name())
if resp.HasRateLimit() {
fmt.Printf("Rate limit: %v\n", resp.RateLimit())
fmt.Printf("Rate limit remaining: %v\n", resp.RateLimitRemaining())
fmt.Printf("Rate limit reset: %v\n", resp.RateLimitReset())
} else {
fmt.Printf("Could not parse rate limit from response.\n")
}
if resp.HasMediaRateLimit() {
fmt.Printf("Media Rate limit: %v\n", resp.MediaRateLimit())
fmt.Printf("Media Rate limit remaining: %v\n", resp.MediaRateLimitRemaining())
fmt.Printf("Media Rate limit reset: %v\n", resp.MediaRateLimitReset())
} else {
fmt.Printf("Could not parse media rate limit from response.\n")
}
}