Skip to content

Instantly share code, notes, and snippets.

@lopnor
Created January 5, 2010 14:51
Show Gist options
  • Select an option

  • Save lopnor/269416 to your computer and use it in GitHub Desktop.

Select an option

Save lopnor/269416 to your computer and use it in GitHub Desktop.
#!/bin/sh
MAIL=$1
PASS=$2
TITLE=$3
FORMAT=$4
if [ -z "$MAIL" -o -z "$PASS" -o -z "$TITLE" ]
then
echo "usage: $0 'your.account@gmail.com' 'your_password' 'filename' ['format']" >&2
exit 1
fi
if [ -z "$FORMAT" ]
then
FORMAT='html'
fi
# http://it.toolbox.com/wiki/index.php/Uri_escape_bash_shell_function
function uri_escape {
local string x
string=$1
var=${2:-_URI_ESCAPE}
while [ -n "$string" ] ## loop until $string is empty
do
case $string in
## If alphanumeric or underscore, add it unchanged
[_a-zA-Z0-9]* ) printf -v x "%s%c" "$x" "$string" ;;
## ...otherwise convert first character to hexadecimal
*) printf -v x "%s%%%X" "$x" "'$string" ;;
esac
string=${string#?} ## remove first character from string
done
eval "$var=\$x"
}
function getauth {
local SERVICE x
SERVICE=$1
var=${2:-AUTH}
x=`
wget \
-S \
-O - \
--post-data="Email=$MAIL&Passwd=$PASS&service=$SERVICE" \
'https://www.google.com/accounts/ClientLogin'|
egrep '^Auth='|
cut \
-d'=' \
-f2`
if [ -z "$x" ]
then
echo 'authorization failed' >&2
exit 1
fi
eval "$var=\$x"
}
getauth writely
uri_escape "$TITLE" TITLE
searchurl="https://docs.google.com/feeds/default/private/full/?title=$TITLE&title-exact=true"
url=`\
wget \
-S \
-O - \
--header="Authorization: GoogleLogin auth=\"$AUTH\"" \
--header="Gdata-Version: 3.0" \
$searchurl|
perl \
-e '<> =~ m/<content .+?src=\x27([^>]+)\x27\/>/ and print $1'`
if [ -z $url ]
then
echo 'url not found. sorry.' >&2
exit 1
fi
url="$url&exportFormat=$FORMAT"
echo "the document url is $url" >&2
if [ -n "`echo $url | grep spreadsheets.google.com`" ]
then
getauth wise
fi
got=`\
wget \
-S \
-O - \
--header="Authorization: GoogleLogin auth=\"$AUTH\"" \
--header="Gdata-Version: 3.0" \
$url`
if [ -n "`echo "$got" | grep gaia_universallogin`" ]
then
echo "request failed." >&2
echo "I suspect this is google's defect" >&2
echo "see http://code.google.com/p/gdata-issues/issues/detail?id=1756" >&2
exit 1
fi
echo $got;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment