blob: 49c60383218852e6a3fbaa6ac7f5f8c2f0a9f7cd [file] [log] [blame]
ade@google.com60a53c02011-01-10 02:33:17 +00001#!/usr/bin/python2.4
2#
Joe Gregorio20a5aa92011-04-01 17:44:25 -04003# Copyright (C) 2010 Google Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
ade@google.com60a53c02011-01-10 02:33:17 +000016
17__author__ = 'ade@google.com (Ade Oshineye)'
18
19import apiclient.discovery
20import httplib2
21import logging
22
23class SimpleWrapper(object):
24 "Simple client that exposes the bare minimum set of common Buzz operations"
25
26 def __init__(self, api_key=None, credentials=None):
ade@google.come45ea6c2011-01-11 18:15:57 +000027 self.http = httplib2.Http()
ade@google.com60a53c02011-01-10 02:33:17 +000028 if credentials:
29 logging.debug('Using api_client with credentials')
ade@google.come45ea6c2011-01-11 18:15:57 +000030 self.http = credentials.authorize(self.http)
31 self.api_client = apiclient.discovery.build('buzz', 'v1', http=self.http, developerKey=api_key)
ade@google.com60a53c02011-01-10 02:33:17 +000032 else:
33 logging.debug('Using api_client that doesn\'t have credentials')
ade@google.come45ea6c2011-01-11 18:15:57 +000034 self.api_client = apiclient.discovery.build('buzz', 'v1', http=self.http, developerKey=api_key)
ade@google.com60a53c02011-01-10 02:33:17 +000035
36 def search(self, query, user_token=None, max_results=10):
37 if query is None or query.strip() is '':
38 return None
39
40 json = self.api_client.activities().search(q=query, max_results=max_results).execute()
41 if json.has_key('items'):
42 return json['items']
43 return []
44
45 def post(self, message_body, user_id='@me'):
46 if message_body is None or message_body.strip() is '':
47 return None
48
49 activities = self.api_client.activities()
50 logging.info('Retrieved activities for: %s' % user_id)
51 activity = activities.insert(userId=user_id, body={
52 'data' : {
53 'title': message_body,
54 'object': {
55 'content': message_body,
56 'type': 'note'}
57 }
58 }
59 ).execute()
60 url = activity['links']['alternate'][0]['href']
61 logging.info('Just created: %s' % url)
62 return url
63
64 def get_profile(self, user_id='@me'):
65 user_profile_data = self.api_client.people().get(userId=user_id).execute()
66 return user_profile_data
ade@google.come45ea6c2011-01-11 18:15:57 +000067
68 def get_follower_count(self, user_id='@me'):
69 return self.__get_group_count(user_id, '@followers')
70
71 def get_following_count(self, user_id='@me'):
72 return self.__get_group_count(user_id, '@following')
73
74 def __get_group_count(self, user_id, group_id):
75 # Fetching 0 results is a performance optimisation that minimises the
76 # amount of data that's getting retrieved from the server
77 cmd = self.api_client.people().list(userId=user_id, groupId=group_id,
78 max_results=0)
79 members = cmd.execute()
80 if 'totalResults' not in members.keys():
81 return -1
Joe Gregorio20a5aa92011-04-01 17:44:25 -040082 return members['totalResults']