Chad Moore : About

tweetMaya

I wrote this about a year ago and haven’t touched it since unfortunately.  Take it and make cool stuff!

I needed to look into python and web application API for a lil’ project I am working on.  To prototype this, I decided to see if I could tweet from Maya.

Why?  Well Maya is extensible via python and there are a bunch of python wrappers for the twitter api. What follows is just really my notes to self when working this out this evening.  I am sure there is much to improve upon and learn, but if your curious take this stuff and run with it! A couple of notes before we begin:

easy_install

simplejson

twitter-python

Easy-Install gotcha?

This was probably user error of some kind since this was my first time with easy_install… but I wound up having to copy the modules I needed into the C:\Python26\Lib\site-packages directory.  I expect that to happen automatically with the easy install commands, but I probably missed something.  Either way, just manually copy and you’ll be set.

Make a python path environment variable.

In your maya.env you’ll need to make paths for Maya to go find python in.  Simply add a pythonpath as such … PYTHONPATH = C:\Python26\;C:\Python26\Lib\site-packages\

OK, on with the code

Now that we have everything we need here’s the code…

import twitter #import the module

twitterUser = 'USERNAME' #hard code in the username
twitterPass = 'PASSWORD' #hard code in the password

api = twitter.Api(twitterUser, twitterPass)#call the api with user credentials

def countCharacters(tweet):#make sure there isnt more than 140 characters in the tweet
    validTweet = 0
    numCharacters = len(tweet)
    if numCharacters < 140:
        validTweet += 1
    else:
        validTweet = 0

    return validTweet, numCharacters

#post a tweet
tweet = 'this tweet brought to you by the python script editor in Maya'

goNoGo = countCharacters(tweet)#do the work to see if we're over 140 characters
if goNoGo[0] == 1:# < 140 characters? == Tweet!
    status = api.PostUpdate(tweet)
else: #> 140 characters? == Fail!
    print "tweet exceeds 140 character limit by %d"%(goNoGo[1] - 140)

Theres a lot more that needs to be added; making this tool a standalone def, building a UI for user/pass/tweet text string fields and some optimization. But hey, it was a fun challenge for the evening.



Leave a Reply

Subscribe: rss | contact | @smapdi | +