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