ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 3 | # 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.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 16 | |
| 17 | __author__ = 'ade@google.com (Ade Oshineye)' |
| 18 | |
| 19 | import apiclient.discovery |
| 20 | import httplib2 |
| 21 | import logging |
| 22 | |
| 23 | class 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.com | e45ea6c | 2011-01-11 18:15:57 +0000 | [diff] [blame] | 27 | self.http = httplib2.Http() |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 28 | if credentials: |
| 29 | logging.debug('Using api_client with credentials') |
ade@google.com | e45ea6c | 2011-01-11 18:15:57 +0000 | [diff] [blame] | 30 | self.http = credentials.authorize(self.http) |
| 31 | self.api_client = apiclient.discovery.build('buzz', 'v1', http=self.http, developerKey=api_key) |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 32 | else: |
| 33 | logging.debug('Using api_client that doesn\'t have credentials') |
ade@google.com | e45ea6c | 2011-01-11 18:15:57 +0000 | [diff] [blame] | 34 | self.api_client = apiclient.discovery.build('buzz', 'v1', http=self.http, developerKey=api_key) |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 35 | |
| 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.com | e45ea6c | 2011-01-11 18:15:57 +0000 | [diff] [blame] | 67 | |
| 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 Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 82 | return members['totalResults'] |