/
main.go
320 lines (260 loc) · 7.03 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
Copyright 2014 Franc[e]sco (lolisamurai@tfwno.gf)
This file is part of gweet.
gweet is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
gweet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with gweet. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"github.com/ChimeraCoder/anaconda"
"github.com/garyburd/go-oauth/oauth"
"github.com/kardianos/osext"
"image/color"
"image/png"
"io/ioutil"
"log"
"math"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"strconv"
"strings"
)
const gweetVersion = "gweet 1.1.2"
func configPath() (res string, err error) {
exeFolder, err := osext.ExecutableFolder()
if err != nil {
return
}
res = path.Join(exeFolder, "gweet.json")
return
}
type gweet struct {
DefaultAccount string
Accounts map[string]oauth.Credentials
}
func initialize() (g *gweet, err error) {
g = &gweet{"", make(map[string]oauth.Credentials)}
cfgPath, err := configPath()
if err != nil {
return
}
log.Println("Loading config from", cfgPath)
file, err := ioutil.ReadFile(cfgPath)
if err != nil {
return
}
err = json.Unmarshal(file, &g)
return
}
func (g *gweet) setupAccount() (err error) {
fmt.Printf("Account name: ")
var name string
fmt.Scanln(&name)
authUrl, tmpCred, err := anaconda.AuthorizationURL("oob")
if err != nil {
return
}
exec.Command("xdg-open", authUrl).Run()
fmt.Printf(
"1. Go to %s (should have opened automatically)\n"+
"2. Authorize the application\n"+
"3. Enter verification code: ", authUrl,
)
var pin string
fmt.Scanln(&pin)
cred, _, err := anaconda.GetCredentials(tmpCred, pin)
if err != nil {
return
}
g.Accounts[name] = *cred
if len(g.DefaultAccount) == 0 {
g.DefaultAccount = name
}
newJson, err := json.MarshalIndent(&g, "", " ")
if err != nil {
return
}
cfgPath, err := configPath()
if err != nil {
return
}
err = ioutil.WriteFile(cfgPath, newJson, os.ModePerm)
return
}
func (g *gweet) newApi(account string) (api *anaconda.TwitterApi, err error) {
if len(account) == 0 {
account = g.DefaultAccount
}
log.Println("Initializing API interface for account", account)
cred := g.Accounts[account]
if len(cred.Token) == 0 {
err = fmt.Errorf("The account '%s' does not exist.", account)
return
}
api = anaconda.NewTwitterApi(cred.Token, cred.Secret)
api.SetLogger(anaconda.BasicLogger)
return
}
/* uset to check if the image has a Set method */
type Changeable interface {
Set(x, y int, c color.Color)
}
func (g *gweet) tweet(api *anaconda.TwitterApi,
text string, files []string, lewd bool,
bypassCompression bool) (tweetUrl string, err error) {
log.Printf(`Tweeting {"%s", %v, lewd=%v}.`+"\n", text, files, lewd)
var ids string
var data []byte
for _, filePath := range files {
log.Println("Uploading", filePath)
// TODO: refuse files larger than 15mb for vids and 5mb for images
data, err = ioutil.ReadFile(filePath)
if err != nil {
return
}
/*mime*/ meme := http.DetectContentType(data)
log.Println(meme)
if !strings.HasPrefix( /*mime*/ meme, "image") {
var media anaconda.ChunkedMedia
var videoMedia anaconda.VideoMedia
// TODO: do not read entire file to memory
media, err = api.UploadVideoInit(len(data), "video/mp4")
if err != nil {
return
}
chunkIndex := 0
for i := 0; i < len(data); i += 5242879 {
log.Println("Chunk", chunkIndex)
err = api.UploadVideoAppend(media.MediaIDString, chunkIndex,
base64.StdEncoding.EncodeToString(
data[i:int(math.Min(5242879.0, float64(len(data))))],
),
)
if err != nil {
return
}
chunkIndex++
}
videoMedia, err = api.UploadVideoFinalize(media.MediaIDString)
if err != nil {
return
}
ids += videoMedia.MediaIDString
ids += ","
} else {
var media anaconda.Media
if bypassCompression {
img, err := png.Decode(bytes.NewReader(data))
if err != nil {
err = nil
/* probably not a png */
} else if imgedit, ok := img.(Changeable); ok {
firstpx := img.At(0, 0)
r, g, b, a := firstpx.RGBA()
if a == 0xFFFF {
log.Println("Lowering first pixel's alpha by 1 " +
"to bypass jpeg compression on opaque PNGs")
imgedit.Set(0, 0,
color.RGBA{
uint8(r / 0x101),
uint8(g / 0x101),
uint8(b / 0x101),
254})
buf := new(bytes.Buffer)
err = png.Encode(buf, img)
if err != nil {
log.Println(err)
} else {
data = buf.Bytes()
}
}
} else {
log.Println("Warning: image format is not changeable, " +
" will upload it unchanged")
}
}
media, err = api.UploadMedia(
base64.StdEncoding.EncodeToString(data))
ids += media.MediaIDString
ids += ","
}
if err != nil {
return
}
}
v := url.Values{}
if len(ids) != 0 {
ids = ids[:len(ids)-1]
log.Println("media_ids:", ids)
v.Set("media_ids", ids)
}
v.Set("possibly_sensitive", strconv.FormatBool(lewd))
tweet, err := api.PostTweet(text, v)
if err != nil {
return
}
tweetUrl = fmt.Sprintf("https://twitter.com/%s/status/%s",
tweet.User.ScreenName, tweet.IdStr)
return
}
func main() {
log.SetOutput(os.Stdout)
anaconda.SetConsumerKey("yXT6a6UeVs7gcPIwQ7z4lMP0I")
anaconda.SetConsumerSecret(
"Hea8xkdJACSvK4F9cil7R64hAN8eAORGv5T0i5X5yoeIEu3qiQ")
// I know this shouldn't be readable but it's pointless to try and obfuscate
// it when it's trivial to reverse.
fmt.Println(gweetVersion)
fmt.Println()
configPtr := flag.Bool("config", false, "Adds an account to the config, "+
"prompting the user to authorize the app and enter the pin code.")
lewdPtr := flag.Bool("lewd", false, "Marks the tweet as sensitive.")
accountPtr := flag.String("account", "", "The target account where the "+
"program will tweet. Uses the default account in the config file if "+
"left blank.")
textPtr := flag.String("text", "", "Text of the tweet. Optional if the "+
"tweet contains at least one media file. Max 140 characters.")
bypassCompressionPtr := flag.Bool("bypass-compression", true,
"If enabled, jpeg compression for opaque png images will be bypassed")
flag.Parse()
g, err := initialize()
if *configPtr {
err := g.setupAccount()
if err != nil {
log.Fatal(err)
}
return
}
if err != nil {
log.Fatalf(
"Failed to load config: %v. Please run 'gweet -config'.", err)
}
api, err := g.newApi(*accountPtr)
if err != nil {
log.Fatal(err)
}
// the last line of output is used as the sharenix plugin's output
tweetUrl, err := g.tweet(api, *textPtr, flag.Args(),
*lewdPtr, *bypassCompressionPtr)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(tweetUrl)
}