blob: eb12f855c4ad1e26ffdaad802e454e58309c5ffb [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):
15 if credentials:
16 logging.debug('Using api_client with credentials')
17 http = httplib2.Http()
18 http = credentials.authorize(http)
19 self.api_client = apiclient.discovery.build('buzz', 'v1', http=http, developerKey=api_key)
20 else:
21 logging.debug('Using api_client that doesn\'t have credentials')
22 self.api_client = apiclient.discovery.build('buzz', 'v1', developerKey=api_key)
23
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