summaryrefslogtreecommitdiff
path: root/twitterclient.py
diff options
context:
space:
mode:
authorMagnus Hagander2010-08-28 11:53:20 +0000
committerMagnus Hagander2010-08-28 11:53:20 +0000
commitc603862e65e094723febe7347a77ce4f50dfc51b (patch)
treee7ff861390ca8c1064407b9b32e62011a5d57c46 /twitterclient.py
parent5698f2c8b93b8924f7bf0c18af5f65a0bac0dcaf (diff)
Add a script that synchronizes a twitter list with the list of subscribers.
In passing, create a TwitterClient base class for use for all communication with twitter.
Diffstat (limited to 'twitterclient.py')
-rw-r--r--twitterclient.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/twitterclient.py b/twitterclient.py
new file mode 100644
index 0000000..bf7c009
--- /dev/null
+++ b/twitterclient.py
@@ -0,0 +1,70 @@
+#!/usr/bin/env python
+# vim: ai ts=4 sts=4 sw=4
+"""PostgreSQL Planet Aggregator
+
+This file contains a base class for twitter integration
+scripts.
+
+Copyright (C) 2009-2010 PostgreSQL Global Development Group
+"""
+import oauth2 as oauth
+import simplejson as json
+import time
+import urllib
+
+class TwitterClient(object):
+ """
+ Base class representing a twitter client, implementing all those twitter
+ API calls that are in use.
+ Does not attempt to be a complete twitter client, just to fill the needs
+ for the planet software.
+ """
+
+ def __init__(self, cfg):
+ """
+ Initialize the instance. The parameter cfg is a ConfigParser object
+ that has loaded the planet.ini file.
+ """
+ self.twittername = cfg.get('twitter', 'account')
+ self.oauth_token = oauth.Token(cfg.get('twitter', 'token'), cfg.get('twitter', 'secret'))
+ self.oauth_consumer = oauth.Consumer(cfg.get('twitter', 'consumer'), cfg.get('twitter', 'consumersecret'))
+
+ def twitter_request(self, apicall, method='GET', ext_params=None):
+ params = {
+ 'oauth_version': "1.0",
+ 'oauth_nonce': oauth.generate_nonce(),
+ 'oauth_timestamp': int(time.time()),
+ 'oauth_token': self.oauth_token.key,
+ 'oauth_consumer_key': self.oauth_consumer.key,
+ }
+ if ext_params:
+ params.update(ext_params)
+
+ url = "https://api.twitter.com/1/%s/%s" % (self.twittername, apicall)
+
+ req = oauth.Request(method=method,
+ url=url,
+ parameters=params)
+ req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), self.oauth_consumer, self.oauth_token)
+ if method=='GET':
+ instream = urllib.urlopen(req.to_url())
+ else:
+ instream=urllib.urlopen(url, req.to_postdata())
+
+ # Make the actual call to twitter
+ ret=instream.read()
+ instream.close()
+ return json.loads(ret)
+
+ def remove_subscriber(self, name):
+ print "Removing twitter user %s from list." % name
+ self.twitter_request('subscribers/members.json', 'POST', {
+ 'id': name,
+ '_method': 'DELETE',
+ })
+
+ def add_subscriber(self, name):
+ print "Adding twitter user %s to list." % name
+ self.twitter_request('subscribers/members.json', 'POST', {
+ 'id': name,
+ })