-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathbuffer.js
More file actions
executable file
·260 lines (246 loc) · 9.36 KB
/
Copy pathbuffer.js
File metadata and controls
executable file
·260 lines (246 loc) · 9.36 KB
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
#!/usr/bin/env node
const API_KEY = process.env.BUFFER_API_KEY
const BASE_URL = 'https://api.bufferapp.com/1'
if (!API_KEY) {
console.error(JSON.stringify({ error: 'BUFFER_API_KEY environment variable required' }))
process.exit(1)
}
async function api(method, path, body) {
const headers = {
'Authorization': `Bearer ${API_KEY}`,
'Accept': 'application/json',
}
if (body && method !== 'GET') {
headers['Content-Type'] = 'application/x-www-form-urlencoded'
}
if (args['dry-run']) {
return { _dry_run: true, method, url: `${BASE_URL}${path}`, headers: { ...headers, 'Authorization': '***' }, body: body || undefined }
}
const res = await fetch(`${BASE_URL}${path}`, {
method,
headers,
body: body ? new URLSearchParams(body).toString() : undefined,
})
const text = await res.text()
try {
return JSON.parse(text)
} catch {
return { status: res.status, body: text }
}
}
async function apiJson(method, path, body) {
if (args['dry-run']) {
return { _dry_run: true, method, url: `${BASE_URL}${path}`, headers: { 'Authorization': '***', 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: body || undefined }
}
const res = await fetch(`${BASE_URL}${path}`, {
method,
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: body ? JSON.stringify(body) : undefined,
})
const text = await res.text()
try {
return JSON.parse(text)
} catch {
return { status: res.status, body: text }
}
}
function parseArgs(args) {
const result = { _: [] }
for (let i = 0; i < args.length; i++) {
const arg = args[i]
if (arg.startsWith('--')) {
const key = arg.slice(2)
const next = args[i + 1]
if (next && !next.startsWith('--')) {
result[key] = next
i++
} else {
result[key] = true
}
} else {
result._.push(arg)
}
}
return result
}
const args = parseArgs(process.argv.slice(2))
const [cmd, sub, ...rest] = args._
async function main() {
let result
switch (cmd) {
case 'user':
switch (sub) {
case 'info':
result = await api('GET', '/user.json')
break
case 'deauthorize':
result = await api('POST', '/user/deauthorize.json')
break
default:
result = { error: 'Unknown user subcommand. Use: info, deauthorize' }
}
break
case 'profiles':
switch (sub) {
case 'list':
result = await api('GET', '/profiles.json')
break
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/profiles/${id}.json`)
break
}
case 'schedules': {
const id = args.id
if (!id) { result = { error: '--id required (profile ID)' }; break }
result = await api('GET', `/profiles/${id}/schedules.json`)
break
}
default:
result = { error: 'Unknown profiles subcommand. Use: list, get, schedules' }
}
break
case 'updates':
switch (sub) {
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required (update ID)' }; break }
result = await api('GET', `/updates/${id}.json`)
break
}
case 'pending': {
const id = args.id
if (!id) { result = { error: '--id required (profile ID)' }; break }
const params = new URLSearchParams()
if (args.page) params.set('page', args.page)
if (args.count) params.set('count', args.count)
if (args.since) params.set('since', args.since)
const qs = params.toString() ? `?${params.toString()}` : ''
result = await api('GET', `/profiles/${id}/updates/pending.json${qs}`)
break
}
case 'sent': {
const id = args.id
if (!id) { result = { error: '--id required (profile ID)' }; break }
const params = new URLSearchParams()
if (args.page) params.set('page', args.page)
if (args.count) params.set('count', args.count)
if (args.since) params.set('since', args.since)
const qs = params.toString() ? `?${params.toString()}` : ''
result = await api('GET', `/profiles/${id}/updates/sent.json${qs}`)
break
}
case 'create': {
const profileIds = args['profile-ids']
const text = args.text
if (!profileIds) { result = { error: '--profile-ids required (comma-separated)' }; break }
if (!text) { result = { error: '--text required' }; break }
const body = { text }
profileIds.split(',').forEach(id => {
if (!body['profile_ids[]']) body['profile_ids[]'] = []
})
const formBody = new URLSearchParams()
formBody.append('text', text)
profileIds.split(',').forEach(id => formBody.append('profile_ids[]', id.trim()))
if (args['scheduled-at']) formBody.append('scheduled_at', args['scheduled-at'])
if (args.now) formBody.append('now', 'true')
if (args.top) formBody.append('top', 'true')
if (args.shorten) formBody.append('shorten', 'true')
if (args['dry-run']) {
result = { _dry_run: true, method: 'POST', url: `${BASE_URL}/updates/create.json`, headers: { 'Authorization': '***', 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' }, body: formBody.toString() }
break
}
const res = await fetch(`${BASE_URL}/updates/create.json`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
},
body: formBody.toString(),
})
const resText = await res.text()
try { result = JSON.parse(resText) } catch { result = { status: res.status, body: resText } }
break
}
case 'update': {
const id = args.id
const text = args.text
if (!id) { result = { error: '--id required (update ID)' }; break }
if (!text) { result = { error: '--text required' }; break }
const body = { text }
if (args['scheduled-at']) body.scheduled_at = args['scheduled-at']
result = await api('POST', `/updates/${id}/update.json`, body)
break
}
case 'share': {
const id = args.id
if (!id) { result = { error: '--id required (update ID)' }; break }
result = await api('POST', `/updates/${id}/share.json`)
break
}
case 'destroy': {
const id = args.id
if (!id) { result = { error: '--id required (update ID)' }; break }
result = await api('POST', `/updates/${id}/destroy.json`)
break
}
case 'reorder': {
const id = args.id
const order = args.order
if (!id) { result = { error: '--id required (profile ID)' }; break }
if (!order) { result = { error: '--order required (comma-separated update IDs)' }; break }
const formBody = new URLSearchParams()
order.split(',').forEach(uid => formBody.append('order[]', uid.trim()))
if (args['dry-run']) {
result = { _dry_run: true, method: 'POST', url: `${BASE_URL}/profiles/${id}/updates/reorder.json`, headers: { 'Authorization': '***', 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' }, body: formBody.toString() }
break
}
const res = await fetch(`${BASE_URL}/profiles/${id}/updates/reorder.json`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
},
body: formBody.toString(),
})
const resText = await res.text()
try { result = JSON.parse(resText) } catch { result = { status: res.status, body: resText } }
break
}
case 'shuffle': {
const id = args.id
if (!id) { result = { error: '--id required (profile ID)' }; break }
result = await api('POST', `/profiles/${id}/updates/shuffle.json`)
break
}
default:
result = { error: 'Unknown updates subcommand. Use: get, pending, sent, create, update, share, destroy, reorder, shuffle' }
}
break
case 'info':
result = await api('GET', '/info/configuration.json')
break
default:
result = {
error: 'Unknown command',
usage: {
user: 'user [info | deauthorize]',
profiles: 'profiles [list | get --id <id> | schedules --id <id>]',
updates: 'updates [get --id <id> | pending --id <profile-id> | sent --id <profile-id> | create --profile-ids <ids> --text <text> [--scheduled-at <time>] [--now] | update --id <id> --text <text> | share --id <id> | destroy --id <id> | reorder --id <profile-id> --order <id1,id2> | shuffle --id <profile-id>]',
info: 'info',
}
}
}
console.log(JSON.stringify(result, null, 2))
}
main().catch(err => {
console.error(JSON.stringify({ error: err.message }))
process.exit(1)
})