Skip to content

Instantly share code, notes, and snippets.

@azizshamim
Created January 15, 2013 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azizshamim/f7d11bc305431dd5f87b to your computer and use it in GitHub Desktop.
Save azizshamim/f7d11bc305431dd5f87b to your computer and use it in GitHub Desktop.
#!/bin/bash
# Copyright (c) 2012 Aziz Shamim
# 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.
TUMBLROAUTH_VERSION=0.0.1
TU_API_VERSION="2"
# Tumblr API Endpoints
TU_REQUEST_TOKEN="http://www.tumblr.com/oauth/request_token"
TU_ACCESS_TOKEN="http://www.tumblr.com/oauth/access_token"
TU_AUTHORIZE="http://www.tumblr.com/oauth/authorize?oauth_token="
oauth_consumer_key=''
oauth_consumer_secret=''
oauth_token=''
oauth_token_secret=''
oauth_version='1.0'
oauth_signature_method='HMAC-SHA1'
oauth_timestamp=`date +%s`
#oauth_nonce=`md5sum <<< "$RANDOM-$(date +%s.%N)" | cut -d' ' -f 1`
oauth_nonce=`date +%s`
# OAuth helpers
OAuth_PE () {
# Encode $1 using Percent-encoding as defined in
# http://tools.ietf.org/html/rfc5849#section-3.6
# Any character other than [a-zA-Z0-9-._~] is converted into format %XX
[ -n "$1" ] \
&& echo -n "$1" | perl -p -e 's/([^A-Za-z0-9-._~])/sprintf("%%%02X", ord($1))/seg'
}
OAuth_HMAC_SHA1 () {
# Hash the text $1 with key $2
local text="$1"
local key="$2"
echo -n "$text" | openssl dgst -sha1 -binary -hmac "$key" | base64
}
# Tumblr API
TU_info() {
TU_ret=$(curl -X GET "https://api.tumblr.com/v2/blog/soggies.tumblr.com/info?api_key=${oauth_consumer_key}" -sq)
echo $TU_ret
}
TU_signature() {
signature_params="oauth_consumer_key=$oauth_consumer_key&oauth_nonce=$oauth_nonce&oauth_signature_method=$oauth_signature_method&oauth_timestamp=$oauth_timestamp&oauth_token=$oauth_token&oauth_version=$oauth_version"
signature_text="$api_method&$(OAuth_PE $api_url)&$(OAuth_PE $signature_params)"
#echo "Signature Text: $signature_text"
signature_key="${oauth_consumer_secret}&${oauth_token_secret}"
oauth_signature=$(OAuth_HMAC_SHA1 $signature_text $signature_key)
#echo $oauth_signature
}
TU_followers() {
api_url="http://api.tumblr.com/v2/blog/soggies.tumblr.com/followers"
api_method="GET"
base_hostname='soggies.tumblr.com'
TU_signature
oauth_headers="OAuth oauth_consumer_key=$oauth_consumer_key, oauth_token=$oauth_token, oauth_signature_method=$oauth_signature_method, oauth_signature=$oauth_signature, oauth_timestamp=$oauth_timestamp, oauth_nonce=$oauth_nonce, oauth_version=$oauth_version"
TU_ret=$(curl -X GET ${api_url} -H "authorization: $oauth_headers" -H "content-type: application/x-www-form-urlencoded")
echo $TU_ret
}
TU_quote() {
api_url="http://api.tumblr.com/v2/blog/soggies.tumblr.com/post"
api_method="POST"
ptype="quote"
conv_quote=$(OAuth_PE "Do or Do not")
conv_source=$(OAuth_PE "Yoda")
signature_params_base="oauth_consumer_key=$(OAuth_PE $oauth_consumer_key)&oauth_nonce=$(OAuth_PE $oauth_nonce)&oauth_signature_method=$(OAuth_PE $oauth_signature_method)&oauth_timestamp=$(OAuth_PE $oauth_timestamp)&oauth_token=$(OAuth_PE $oauth_token)&oauth_version=$(OAuth_PE $oauth_version)"
signature_params_content="quote=$conv_quote&source=$conv_source&type=$ptype"
signature_params="$signature_params_base&$signature_params_content"
echo "Signature Params: $signature_params"
signature_text="$api_method&$(OAuth_PE $api_url)&$(OAuth_PE $signature_params)"
echo "Signature Text: $signature_text"
#echo
#echo
#echo
signature_key="${oauth_consumer_secret}&${oauth_token_secret}"
echo "Signature Key: ${signature_key}"
oauth_signature=$(OAuth_HMAC_SHA1 $signature_text $signature_key)
echo $oauth_signature
oauth_headers="OAuth oauth_consumer_key=$oauth_consumer_key, oauth_token=$oauth_token, oauth_signature_method=$oauth_signature_method, oauth_signature=$oauth_signature, oauth_timestamp=$oauth_timestamp, oauth_nonce=$oauth_nonce, oauth_version=$oauth_version"
echo $oauth_headers
body="$signature_params_content&$signature_params_base"
echo $body
#echo
TU_ret=$(curl -X POST ${api_url} --data "$body" -H "authorization: $oauth_headers" -H "content-type: application/x-www-form-urlencoded" -qs)
echo $TU_ret
}
# uncomment to run
#TU_followers
#TU_quote
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment