-
Notifications
You must be signed in to change notification settings - Fork 11
/
tcli.sh
executable file
·210 lines (177 loc) · 5.83 KB
/
tcli.sh
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
#!/bin/bash
# Copyright (c) 2014 Mathew Paret
# Copyright (c) 2010, 2012 Yu-Jie Lin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
TCLI_RC="$HOME/.tcli.rc"
# Source TwitterOAuth.sh
OAuth_sh=$(which TwitterOAuth.sh)
(( $? != 0 )) && echo 'Unable to locate TwitterOAuth.sh! Make sure it is in searching PATH.' && exit 1
source "$OAuth_sh"
usage () {
echo "usage: $0 options
OPTIONS:
-h Show this message
-c Command
Valid Commands:
account_update_profile_image
statuses_update
twitpic_upload
twitpic_upload_post
Use -h -c command to get options for the command.
"
exit $1
}
show_config_help () {
echo "Please create $TCLI_RC with:
oauth_consumer_key=YOUR_CONSUMER_KEY
oauth_consumer_secret=YOUR_CONSUMER_SECRET
You can register new app to get consumer key and secret at
http://dev.twitter.com/apps/new
"
exit $1
}
show_account_update_profile_image () {
echo "Command account_update_profile_image
Requires:
-f file
"
exit $1
}
show_statuses_update () {
echo "Command statuses_update
Requires:
-s status
Optional:
-r in_reply_to_status_id
"
exit $1
}
show_twitpic_update () {
echo "Command twitpic_update
Requires:
-s status
-f file
"
exit $0
}
load_config () {
[[ -f "$TCLI_RC" ]] && . "$TCLI_RC" || show_config_help 1
[[ "$oauth_consumer_key" == "" ]] && show_config_help 1
[[ "$oauth_consumer_secret" == "" ]] && show_config_help 1
TO_init
if [[ "$oauth_token" == "" ]] || [[ "$oauth_token_secret" == "" ]]; then
TO_access_token_helper
if (( $? == 0 )); then
oauth_token=${TO_ret[0]}
oauth_token_secret=${TO_ret[1]}
echo "oauth_token='${TO_ret[0]}'" >> "$TCLI_RC"
echo "oauth_token_secret='${TO_ret[1]}'" >> "$TCLI_RC"
echo "Token saved."
else
echo 'Unable to get access token'
exit 1
fi
fi
}
main () {
load_config
tcli_command=
tcli_status=
tcli_in_reply_to_status_id=
tcli_file=
tcli_help_flag=
while getopts "c:s:r:f:h" name
do
case $name in
c) tcli_command="$OPTARG";;
s) tcli_status="$OPTARG";;
r) tcli_in_reply_to_status_id="$OPTARG";;
f) tcli_file="$OPTARG";;
h) Tcli_help_flag="1";;
?) usage
exit 2;;
esac
done
if [[ "$tcli_help_flag" == "1" ]]; then case $tcli_command in
account_update_profile_image)
show_account_update_profile_image 0
;;
statuses_update)
show_statuses_update 0
;;
twitpic_upload|twitpic_upload_post)
show_twitpic_upload 0
;;
*)
[[ "$tcli_command" == "" ]] && usage 0
usage 1
esac ; fi
case $tcli_command in
account_update_profile_image)
[[ "$tcli_file" == "" ]] && show_account_update_profile 1
TO_account_update_profile_image "$tcli_file"
echo "$TO_ret"
return $TO_rval
;;
statuses_update)
[[ "$tcli_status" == "" ]] && show_statuses_update 1
TO_statuses_update "$tcli_status" "$tcli_in_reply_to_status_id"
echo "$TO_ret"
return $TO_rval
;;
twitpic_upload)
[[ "$twitpic_api" == "" ]] && read -p 'You TwitPic API Key: ' twitpic_api && echo "twitpic_api=\"$twitpic_api\"" >> "$TCLI_RC"
[[ "$twitpic_api" == "" ]] && exit 1
[[ "$tcli_file" == "" ]] && show_twitpic_upload 1
[[ "$tcli_status" == "" ]] && show_twitpic_upload 1
T_ACCOUNT_VERIFY_CREDENTIALS='https://api.twitter.com/1/account/verify_credentials.json'
auth_header=$(OAuth_authorization_header 'X-Verify-Credentials-Authorization' 'http://api.twitter.com' '' '' 'GET' "$T_ACCOUNT_VERIFY_CREDENTIALS")
echo "Sending $tcli_file to TwitPic..."
ret=$(curl -s -H "X-Auth-Service-Provider=$T_ACCOUNT_VERIFY_CREDENTIALS" -H "$auth_header" -F "key=$twitpic_api" -F "message=$tcli_status" -F "media=@$tcli_file" http://twitpic.com/api/2/upload.xml)
rval=$?
(( $rval != 0 )) && echo "$ret" && exit $rval
image_url=$(egrep -o 'http://twitpic\.com/[^<]*' <<< "$ret")
echo " Image uploaded: $image_url"
;;
twitpic_upload_post)
[[ "$twitpic_api" == "" ]] && read -p 'You TwitPic API Key: ' twitpic_api && echo "twitpic_api=\"$twitpic_api\"" >> "$TCLI_RC"
[[ "$twitpic_api" == "" ]] && exit 1
[[ "$tcli_file" == "" ]] && show_twitpic_upload 1
[[ "$tcli_status" == "" ]] && show_twitpic_upload 1
T_ACCOUNT_VERIFY_CREDENTIALS='https://api.twitter.com/1/account/verify_credentials.json'
auth_header=$(OAuth_authorization_header 'X-Verify-Credentials-Authorization' 'http://api.twitter.com' '' '' 'GET' "$T_ACCOUNT_VERIFY_CREDENTIALS")
echo "Sending $tcli_file to TwitPic..."
ret=$(curl -s -H "X-Auth-Service-Provider=$T_ACCOUNT_VERIFY_CREDENTIALS" -H "$auth_header" -F "key=$twitpic_api" -F "message=$tcli_status" -F "media=@$tcli_file" http://twitpic.com/api/2/upload.xml)
rval=$?
(( $rval != 0 )) && echo "$ret" && exit $rval
image_url=$(egrep -o 'http://twitpic\.com/[^<]*' <<< "$ret")
echo " Image uploaded: $image_url"
echo "Posting tweet..."
TO_statuses_update "$tcli_status $image_url" "$tcli_in_reply_to_status_id"
echo "$TO_ret"
return $TO_rval
;;
*)
usage 1
;;
esac
return 0
}
main "$@"