Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2010/08/27

A Squeeze of Python to give me a Buzz

I've been learning Python.

I know, I know....

It starts with Google Buzz. I accept web pages and window apps as the only choices to see social media output from places like Identica, Twitter, Facebook, etc. But my preference is to type things on the command line for most of my content-generating needs. If I wanted to send the same thought to most of my microblogging sites, I just pipe it together.

echo My oh-so-crucial thought | twitter.pl | identi.pl | wiki.pl

So far, two things have been standing in my way. Facebook and Google Buzz. I have yet to find ways to use my beloved Perl to write to either. But I poked around and found some sample code on the Google Buzz API page. Specifically, I found make_post.py, which does write to Buzz, but it will not easily integrate to my preferred way. Specifically, it takes in your key and secret as command-line flags and accepts your Buzz update via STDIN. If that's how you roll, that's fine, but for me, the message is join ' ' , @ARGV . So, I recoded it, taking more out than I put in, and using some of my very slight knowledge of Python, to make this. I have the key and secret hard-coded, which is kinda acceptable for personal use but still not to be accepted. You should use the make_post.py that comes with buzz-python-client to set up your OAuth if you use this, because anything that would do that has been brutally ripped from the original code.

Expect Perl code based on this to come around eventually. For now, enjoy buzz-python-client and my addition.

# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
import traceback
import getopt

#
# Load Buzz library (if available...)
#

try:
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import buzz
except ImportError:
print 'Error importing Buzz library!!!'

token = verification_code = buzz_client = ''

#
# Function to obtain login data
#

def GetLoginData():
'Obtains login information from either the command line or by querying the user'

global token, verification_code, buzz_client
key = 'KEYKEYKEYKEYKEYKEYKEYKEYKEYKEYKEYKEYKEY'
secret = 'SECRETSECRETSECRETSECRETSECRETSECRET'
buzz_client.build_oauth_access_token(key, secret)

#
# Main program starts here
#

try:
buzz_client = buzz.Client()
buzz_client.oauth_scopes=[buzz.FULL_ACCESS_SCOPE]
buzz_client.use_anonymous_oauth_consumer()
GetLoginData()
args = sys.argv
del args[0]
message = ' '.join(args)
message = message.strip()
post = buzz.Post(content=message)
buzz_client.create_post(post)
print message
except:
print '\nBzzzz! Something broke!!!'
print '-' * 50
traceback.print_exc()
print '-' * 50

7 comments:

  1. WWW::Facebook::API allows posting to Facebook, for what it's worth -- you can see my seriously-not-ready-for-primetime solution to your problem at

    ReplyDelete
  2. Grr, that URL should be http://github.com/genehack/app-status-skein

    ReplyDelete
  3. Thanks, gene. I saw WWW::Facebook::API but didn't think it was for this sort of stuff. I thought it was more "Make your own quiz or game" than that.

    ReplyDelete
  4. Nope, it's pretty much full access to their (pretty annoying, IMO) API -- have a look at http://search.cpan.org/dist/WWW-Facebook-API/lib/WWW/Facebook/API/Status.pm , specifically the 'set()' method.

    ReplyDelete
  5. Yeah, it's grim. I'm having real problems getting to the point where I can authenticate. ICK ICK ICK. But my twitter friends are my geek friends and my facebook friends are my real-life friends, churchgoers and parents and such. I don't want to use a Twitter->Facebook bridge. GRRR.

    ReplyDelete
  6. There's some sample code at http://github.com/genehack/app-status-skein/blob/master/lib/App/StatusSkein/CLI/Client/Facebook.pm that builds a client.

    There's also a little CLI script at http://github.com/genehack/app-status-skein/blob/master/script/get-perm-facebook-session-key that will help you get a session key once you've got your API key and secret -- this was the most confusing part for me.

    ReplyDelete
  7. Gene, it's the most confusing part for me, too. It seems to want me to tell it where to go next, and I'm struggling to keep from telling it where to go.

    ReplyDelete