The Accidental Rubyist

invalid byte sequence in UTF-8

More Curling with Google Blogger (new)

leave a comment »

This is a followup on my previous post on Curling with Google Blogger Data API.

In case you have been playing around with the CURL hints given on Google’s Blogger Data API Developer’s Guide and had problems, please peruse the following.

Authenticaton


curl --silent --header “Content-Type: application/x-www-form-urlencoded” -X POST --data-binary @file.txt https://www.google.com/accounts/ClientLogin > auth.txt

The above command needs only your email and password (from file.txt) in the following format:

 accountType=GOOGLE&Email=johndoe@gmail.com&Passwd=xyz123&service=blogger&source=blogger.sh

Please substitute your email and password above. Notice that I have dumped the auth token in a file (I don’t know anything about cookies). We are interested only in the line that says “Auth=”. We need to format that in the header as “auth=” when we send a POST or DELETE request.

POSTING TO BLOGGER


We first need to get our POST URI correct.
export URI=”$BLOGNAME/feeds/posts/default”
This will look like http://myblog.blogspot.com/feeds/posts/default”.

Here is all you need to post a file named sample.html:

curl --silent --compressed --location --location-trusted --header “Authorization: GoogleLogin auth=$AUTH” --header “Content-Type:application/atom+xml;charset=UTF-8” --header $ENCHDR -i -X POST --data-binary @sample.html $URI > posted.xml

The variable ENCHDR is not required but was being sent by the Java Client. It is
export ENCHDR=”Accept-Encoding:gzip”. I write the return value into posted.xml, we should check for a “201 Created” in it to ensure that the write was a success, as follows:
grep -q “HTTP/1.1 201” posted.xml.
The return file may be used for editing and updating the post. The edit link is what is required for updating and deleting.

DELETING A POST


Deleting a post requires the Edit-Link which contains the BLOGID (a large number) and the POSTID (an even larger one). The format is like this:
http://www.blogger.com/feeds/98888814/posts/default/3456482919486403855. This may be taken from the file returned by the POST procedure. Also the source of the page will contain it. The command is simply this.

curl --silent --compressed --location --location-trusted --header “Authorization: GoogleLogin auth=$AUTH” -X DELETE $URI

RETRIEVING FEEDS

This requires no authorization.

export URI=$BLOGNAME/feeds/posts/default
curl --silent --location --location-trusted -X GET $URI > myfeeds.xml

Here is the template for the post:

<?xml version=”1.0″?>
<entry xmlns=”http://www.w3.org/2005/Atom”&#62;
<title type=”text”>MYTITLE</title>
<content type=”xhtml”>
MYCONTENT – blah blah
</content>
</entry>

For a complete listing of the program blogger.sh please download from here (6kb).

Dependencies:
This program uses the popular curl program for connecting. It was tested on a MAC OSX Tiger with:
$ curl -V
curl 7.13.1 (powerpc-apple-darwin8.0) libcurl/7.13.1 OpenSSL/0.9.7l zlib/1.2.3

The view program parses out titles and Edit-URI’s using a single line of ruby.

My vim mapping for this are as follows:

map ;bbt :0<CR>:r !blogger.sh -t<CR>:set filetype=html<CR>:g/^$/d<CR>/EDIT_THIS<CR>cw
” ” (P)ew- posts new entry
map ;bbp :!blogger.sh -p %<CR>
” ” (V)iew list and delete selected
map ;bbv :!blogger.sh -v<CR>
” ” (H)elp
map ;bbh :!blogger.sh -h<CR>

Once you have extracted the template, you may save and modify it, and then modify the bbt setting as follows:
map ;bbt :0r ~/.bb.html<CR>:set filetype=html<CR>A
I am setting the filetype since I use the unparalleled HTML.vim for formatting.

See also:


  1. Vim Blogger Plugin

  2. Emacs Client

  3. Google API

  4. Also check the wonderful mtsend.py for Movable Type.

Happy posting! Long live Vim and the command-line!

[Edited in Vim using HTML.vim]

Advertisements

Written by totalrecall

March 6, 2007 at 4:17 am

Posted in being_geeky, unix

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: