Quick single-user twitter oauth setup
I had a tool that posted to twitter with a username and password that were saved in a neighboring file. Here is how I ported that setup to oauth. This procedure is not at all friendly to multiple users who would run your tool; it's only suitable if making a user account is something you do roughly once (like you used to do by pasting in the user+password into your config file).
Go to the new app page and make up some details. Application type is 'client'. No URLs matter.
Run this to get two new values:
% ipython
import oauth2, urlparse
consumer = oauth2.Consumer("consumer key goes here", "consumer secret goes here")
client = oauth2.Client(consumer)
resp, content = client.request("https://api.twitter.com/oauth/request_token")
request_token = dict(urlparse.parse_qsl(content))
print "%s?oauth_token=%s" % ("https://api.twitter.com/oauth/authorize", request_token['oauth_token'])
Visit that link, get a PIN.
token = oauth2.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
token.set_verifier("pin goes here")
client = oauth2.Client(consumer, token)
resp, content = client.request("https://api.twitter.com/oauth/access_token", "POST")
access_token = dict(urlparse.parse_qsl(content))
{"oauth_token" : access_token['oauth_token'], "oauth_token_secret" : access_token['oauth_token_secret']}
Save those two values with the consumer key and consumer secret you got from twitter during registration.
All that was the one-off process for getting all the magic cookies. If you now want to talk to twitter from python, import this:
def makeOauthFilter(conf):
consumer = restkit.util.oauth2.Consumer(conf['oauth_consumer_key'],
conf['oauth_consumer_secret'])
token = restkit.util.oauth2.Token(conf['oauth_token'],
conf['oauth_token_secret'])
return restkit.filters.oauth2.OAuthFilter("*", consumer, token,
restkit.util.oauth2.SignatureMethod_HMAC_SHA1())
oauthFilter = makeOauthFilter(conf)
timelineJs = restkit.request(
method="GET",
url="http://api.twitter.com/1/statuses/home_timeline.json",
filters=[oauthFilter],
since_id=since_id).body_string()
for status in jsonlib.read(timelineJs, use_float=True):
...
Arduino/LCD tests

I got this cool LCD from Adafruit. It has a tutorial for connecting it to an Arduino.
I wrote a tiny driver with web.py and spawning. Spawning has the cool feature of being able to use eventlet to handle multiple simultaneous requests in one process, in one thread. This helps me share my single USB port with all the requests; I didn't have to write any special queuing stuff. I would have gotten that property for free with twisted, too, but twisted.web and nevow are more complicated to use.
My code is essentially this:
import web, serialser = serial.Serial(port='/dev/ttyUSB1', baudrate=9600, timeout=1,xonxoff=0, rtscts=0)class index(object):def POST(self):ser.write("\xff" + web.data() + "\x00") # I made up this protocol and implemented it on the arduino sidereturn "ok"urls = (r'/', 'index')app = web.application(urls, globals())application = app.wsgifunc()# spawn --port=9017 --threads=0 host.application
This is probably more than one needs just to connect an LCD to a computer, but I intend to run several other sensors and light controls off the same Arduino, so my web server will be getting more elaborate.
In the meantime, I can run unix shell commands onto the LCD:
% forever { date | curl -d @- http://dash:9017/; sleep 1 }
Or I can do a live search on twitter and display its results on the LCD:
import sys, restkit, jsonlib, urllib
login = "...", "..."
keyword = sys.argv[1]
response = restkit.request(
"http://stream.twitter.com/1/statuses/filter.json?track=%s" %
urllib.quote(keyword), filters=[restkit.BasicAuth(*login)])
for out in response.body_stream():
text = jsonlib.read(out)['text']
print text
restkit.request(method="post", url="http://dash:9017/", body=text)

Atom feed of this blog