ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame^] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 4 | |
| 5 | __author__ = 'ade@google.com (Ade Oshineye)' |
| 6 | |
| 7 | import apiclient.discovery |
| 8 | import httplib2 |
| 9 | import logging |
| 10 | |
| 11 | class 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 |