forked from maeveynot/peach-curl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeach
executable file
·169 lines (142 loc) · 5.53 KB
/
peach
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
#!/bin/sh
api_base="https://v1.peachapi.com"
session_file="$HOME/.peach-session"
temp="$(mktemp -t "peach.XXXXXX")"
cleanup() { rm -rf "$temp"; }
trap 'exit $?' HUP INT QUIT TERM; trap cleanup EXIT
print_api_help() {
cat <<EOF
Known Peach endpoints (see README.md for details of how to interpret this list):
# Account and device registration
POST /precheck/create/user {email: string} or {password: string}
POST /precheck/create/stream {name: string}
POST /register {email: string, password: string, name: string}
POST /login {email: string, password: string}
PUT /logout
POST /stream/device {applicationId: string, deviceId: string}
DELETE /stream/device/{{device_id}}
POST /recover {email: string, returnLink: string}
# Reading posts
GET /connections
GET /connections/explore
GET /recommended/streams
GET /stream/id/{{id}}
GET /stream/n/{{name}}
PUT /stream/id/{{id}}/read (no body)
# Listing friends
GET /stream/id/{{id}}/connections
GET /stream/n/{{name}}/connections
# Requesting friends
POST /stream/n/{{name}}/connection (no body)
DELETE /stream/n/{{name}}/connection
GET /connections/requests/inbound
POST /friend-request/{{id}}/accept (no body)
POST /friend-request/{{id}}/ignore (no body)
DELETE /friend-request/{{id}}
# Posting, liking, commenting
POST /post {message: [{type: "text", text: string}]}
or {type: "image", src: string, width: number, height: number}, etc...
DELETE /post/{{id}}
POST /like {postId: string}
DELETE /like/postID/{{id}}
POST /comment {postId: string, comment: string}
DELETE /comment/{{id}}
# Blocking
POST /stream/id/{{id}}/block (no body)
POST /stream/n/{{name}}/block (no body)
POST /stream/id/{{id}}/unblock (no body)
POST /stream/n/{{name}}/unblock (no body)
GET /stream/block-list
# Notifications
GET /activity/isUnread
GET /activity
PUT /activity/read (no body)
# Interacting
POST /activity/wave {targetStreamId: string, type: string}
GET /chat
GET /chat/token
POST /chat/id/{{id}} {clientPostID: string, message: [{type: "text", text: string, messageID: string, isTail: string, isFinalTail: string}]}
or {type: "image", subtype: "sticker", src: string, width: number, height: number}, etc...
PUT /chat/id/{{id}}/read (no body)
# Account settings
PUT /stream/name {name: string}
PUT /stream/displayName {displayName: string}
PUT /stream/bio {bio: string}
PUT /stream/avatarSrc {avatarSrc: string}
GET /stream/friends/auto
POST /stream/friends/auto {auto: boolean}
GET /stream/allow-pagination
POST /stream/allow-pagination {allowPagination: boolean}
GET /stream/online-visibility
POST /stream/online-visibility {onlineVisibility: boolean}
GET /stream/public
POST /stream/public {public: boolean}
GET /stream/sharing
POST /stream/sharing {friendsSharing: boolean}
GET /stream/visibility
POST /stream/visibility {friendsOnly: boolean}
Usage: peach [options] [endpoint] [curl options]
peach /connections
peach /activity/read -X PUT
peach -n /precheck/create/stream -d '{"name": "peach"}'
EOF
}
curl_peach() {
local resource="$1"; shift
for i; do
case "$i" in
-d|--data*) body=yes;;
esac
done
curl -fsSL -H 'Accept: application/json' \
${body:+-H 'Content-Type: application/json'} \
${token:+-H "Authorization: Bearer $token"} \
"$@" "$api_base/$resource"
}
authenticated=yes
while getopts 'f:hnq' opt; do
case "$opt" in
f) session_file="$OPTARG";;
h) print_api_help; exit;;
n) unset authenticated;;
q) quiet=y;;
*) echo 'Use `peach` or `peach -h` for help and usage'; exit 2;;
esac
done; shift $(($OPTIND-1))
if test -n "$authenticated"; then
if test -s "$session_file"; then
token="$(jq -r '.data.streams[0].token' "$session_file")"
fi
if test -z "$token" -o "$token" = 'null'; then
echo "You don't have a API token yet! Let's log in."
read -p "Please enter your 🍑 email address: " -r peach_email
stty -echo
read -p "Please enter your 🍑 password: " -r peach_password
stty echo; echo
login_body="$(jq --arg email "$peach_email" --arg password "$peach_password" -n '{email: $email, password: $password}')"
if curl_peach /login -d "$login_body" -o "$session_file"; then
if test "$(jq -r '.success' "$session_file")" = '1'; then
echo "Wrote your token to $session_file. Happy 🍑 ing!"
echo "Run this command again for more instructions."
exit 0
else
echo "Error logging in: $(jq -r '.error.Message' "$session_file")"
exit 1
fi
else
echo "Error writing $session_file. Sorry!"
exit 1
fi
fi
fi
if test "$#" -gt 0; then
resource="${1#/}"; shift
curl_peach "$resource" -o "$temp" "$@" || exit $?
if test -t 1; then
jq . "$temp"
else
cat "$temp"
fi
else
test -n "$quiet" || print_api_help
fi