blob: 7689a28ee7a1ebee20b801818aade24443d5dd01 [file] [log] [blame]
ade@google.com60a53c02011-01-10 02:33:17 +00001#!/usr/bin/python2.4
2#
3# Copyright 2010 Google Inc. All Rights Reserved.
4
5__author__ = 'ade@google.com (Ade Oshineye)'
6
7import apiclient.discovery
8import httplib2
9import logging
10
11class SimpleWrapper(object):
12 "Simple client that exposes the bare minimum set of common Buzz operations"
13
14 def __init__(self, api_key=None, credentials=None):
ade@google.come45ea6c2011-01-11 18:15:57 +000015 self.http = httplib2.Http()
ade@google.com60a53c02011-01-10 02:33:17 +000016 if credentials:
17 logging.debug('Using api_client with credentials')
ade@google.come45ea6c2011-01-11 18:15:57 +000018 self.http = credentials.authorize(self.http)
19 self.api_client = apiclient.discovery.build('buzz', 'v1', http=self.http, developerKey=api_key)
ade@google.com60a53c02011-01-10 02:33:17 +000020 else:
21 logging.debug('Using api_client that doesn\'t have credentials')
ade@google.come45ea6c2011-01-11 18:15:57 +000022 self.api_client = apiclient.discovery.build('buzz', 'v1', http=self.http, developerKey=api_key)
ade@google.com60a53c02011-01-10 02:33:17 +000023
24 def search(self, query, user_token=None, max_results=10):
25 if query is None or query.strip() is '':
26 return None
27
28 json = self.api_client.activities().search(q=query, max_results=max_results).execute()
29 if json.has_key('items'):
30 return json['items']
31 return []
32
33 def post(self, message_body, user_id='@me'):
34 if message_body is None or message_body.strip() is '':
35 return None
36
37 activities = self.api_client.activities()
38 logging.info('Retrieved activities for: %s' % user_id)
39 activity = activities.insert(userId=user_id, body={
40 'data' : {
41 'title': message_body,
42 'object': {
43 'content': message_body,
44 'type': 'note'}
45 }
46 }
47 ).execute()
48 url = activity['links']['alternate'][0]['href']
49 logging.info('Just created: %s' % url)
50 return url
51
52 def get_profile(self, user_id='@me'):
53 user_profile_data = self.api_client.people().get(userId=user_id).execute()
54 return user_profile_data
ade@google.come45ea6c2011-01-11 18:15:57 +000055
56 def get_follower_count(self, user_id='@me'):
57 return self.__get_group_count(user_id, '@followers')
58
59 def get_following_count(self, user_id='@me'):
60 return self.__get_group_count(user_id, '@following')
61
62 def __get_group_count(self, user_id, group_id):
63 # Fetching 0 results is a performance optimisation that minimises the
64 # amount of data that's getting retrieved from the server
65 cmd = self.api_client.people().list(userId=user_id, groupId=group_id,
66 max_results=0)
67 members = cmd.execute()
68 if 'totalResults' not in members.keys():
69 return -1
70 return members['totalResults']