-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwit.sh
67 lines (58 loc) · 1.4 KB
/
twit.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
#!/bin/sh
# Debug
#set -x
CKEY="ysJCh74mgNrzzQ0fZxXNw"
CSECRET="cvpHWJ3WSHLHeAX6WUOZjv5CR3qy7vwVvLf4WDuT0tY"
AKEY=""
ASECRET=""
#HTTP_POST="wget -q -O - --post-data"
HTTP_POST="curl -s --data"
Encode()
{
string=$1; format=; set --
while
literal=${string%%[!-._~0-9A-Za-z]*}
if [ -n "$literal" ]; then
format=$format%s
set -- "$@" "$literal"
string=${string#$literal}
fi
[ -n "$string" ]
do
tail=${string#?}
head=${string%$tail}
format=$format%%%02X
set -- "$@" "'$head"
string=$tail
done
printf "$format\\n" "$@"
}
GenerateHash()
{
EURL="`Encode $2`"
EPARAM="`Encode $3`"
QUERY="$1&$EURL&$EPARAM"
HASH="`echo -n \"$QUERY\" | openssl sha1 -hmac \"$CSECRET&$ASECRET\" -binary | openssl base64`"
Encode "$HASH"
}
UpdateTimeLine()
{
TWEET="`Encode \"$@\"`"
if [ "$TWEET" == "" ]
then
echo "Error: Text is missing" >&2
exit 1
fi
URL="http://api.twitter.com/1.1/statuses/update.json"
NONCE="`dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | cut -c1-32`"
PARAM="oauth_consumer_key=$CKEY&oauth_nonce=$NONCE&oauth_signature_method=HMAC-SHA1&oauth_timestamp=`date +%s`&oauth_token=$AKEY&oauth_version=1.0&status=$TWEET"
HASH="`GenerateHash "POST" "$URL" "$PARAM"`"
JSON="`$HTTP_POST "$PARAM&oauth_signature=$HASH" "$URL"`"
ERROR="`echo "$JSON" | grep "errors:"`"
if [ -n "$ERROR" ]
then
echo -n "Error: "
echo $JSON | sed -e 's/^.*"message":"\([^"]*\)".*$/\1/'
fi
}
UpdateTimeLine "$@"