Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 96 lines (83 sloc) 2.878 kb
c603862 @mhagander Add a script that synchronizes a twitter list with the list of subscribe...
authored
1 #!/usr/bin/env python
2 # vim: ai ts=4 sts=4 sw=4
3 """PostgreSQL Planet Aggregator
4
5 This file contains a base class for twitter integration
6 scripts.
7
8 Copyright (C) 2009-2010 PostgreSQL Global Development Group
9 """
10 import oauth2 as oauth
11 import simplejson as json
12 import time
13 import urllib
14
15 class TwitterClient(object):
16 """
17 Base class representing a twitter client, implementing all those twitter
18 API calls that are in use.
19 Does not attempt to be a complete twitter client, just to fill the needs
20 for the planet software.
21 """
22
23 def __init__(self, cfg):
24 """
25 Initialize the instance. The parameter cfg is a ConfigParser object
26 that has loaded the planet.ini file.
27 """
28 self.twittername = cfg.get('twitter', 'account')
822afe7 @mhagander Make the name of the twitter list configurable.
authored
29 self.twitterlist = cfg.get('twitter', 'listname')
c603862 @mhagander Add a script that synchronizes a twitter list with the list of subscribe...
authored
30 self.oauth_token = oauth.Token(cfg.get('twitter', 'token'), cfg.get('twitter', 'secret'))
31 self.oauth_consumer = oauth.Consumer(cfg.get('twitter', 'consumer'), cfg.get('twitter', 'consumersecret'))
32
33 def twitter_request(self, apicall, method='GET', ext_params=None):
34 params = {
35 'oauth_version': "1.0",
36 'oauth_nonce': oauth.generate_nonce(),
37 'oauth_timestamp': int(time.time()),
38 'oauth_token': self.oauth_token.key,
39 'oauth_consumer_key': self.oauth_consumer.key,
40 }
41 if ext_params:
42 params.update(ext_params)
43
06feca0 @mhagander Update to use twitter API v1.1
authored
44 url = "https://api.twitter.com/1.1/%s" % apicall
c603862 @mhagander Add a script that synchronizes a twitter list with the list of subscribe...
authored
45
46 req = oauth.Request(method=method,
47 url=url,
48 parameters=params)
49 req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), self.oauth_consumer, self.oauth_token)
50 if method=='GET':
51 instream = urllib.urlopen(req.to_url())
52 else:
53 instream=urllib.urlopen(url, req.to_postdata())
54
55 # Make the actual call to twitter
56 ret=instream.read()
57 instream.close()
0ecaed6 @mhagander Add more info when JSON return is not in JSON format
authored
58 try:
59 return json.loads(ret)
60 except json.decoder.JSONDecodeError:
61 print "Received non-JSON response to a JSON request!"
62 print ret
63 raise
c603862 @mhagander Add a script that synchronizes a twitter list with the list of subscribe...
authored
64
faee99f @mhagander The list_subscribers function belongs in TwitterClient
authored
65 def list_subscribers(self):
2f1d0c3 @mhagander Support cursors when listing subscribers to a list.
authored
66 # Eek. It seems subscribers are paged even if we don't ask for it
67 # Thus, we need to loop with multiple requests
68 cursor=-1
69 handles = []
70 while cursor != 0:
06feca0 @mhagander Update to use twitter API v1.1
authored
71 response = self.twitter_request('lists/members.json', 'GET', {
72 'owner_screen_name': self.twittername,
73 'slug': self.twitterlist,
74 'cursor': cursor,
75 })
ab3ac58 @mhagander Turn all twitter handles to lowercase when comparing
authored
76 handles.extend([x['screen_name'].lower() for x in response['users']])
2f1d0c3 @mhagander Support cursors when listing subscribers to a list.
authored
77 cursor = response['next_cursor']
78
79 return handles
faee99f @mhagander The list_subscribers function belongs in TwitterClient
authored
80
c603862 @mhagander Add a script that synchronizes a twitter list with the list of subscribe...
authored
81 def remove_subscriber(self, name):
82 print "Removing twitter user %s from list." % name
06feca0 @mhagander Update to use twitter API v1.1
authored
83 self.twitter_request('lists/members/destroy.json', 'POST', {
84 'owner_screen_name': self.twittername,
85 'slug': self.twitterlist,
86 'screen_name': name,
87 })
c603862 @mhagander Add a script that synchronizes a twitter list with the list of subscribe...
authored
88
89 def add_subscriber(self, name):
90 print "Adding twitter user %s to list." % name
06feca0 @mhagander Update to use twitter API v1.1
authored
91 self.twitter_request('lists/members/create.json', 'POST', {
92 'owner_screen_name': self.twittername,
93 'slug': self.twitterlist,
94 'screen_name': name,
95 })
Something went wrong with that request. Please try again.