jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
| 4 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 5 | |
| 6 | """Simple command-line example for Buzz. |
| 7 | |
| 8 | Command-line application that retrieves the users |
| 9 | latest content and then adds a new entry. |
| 10 | """ |
| 11 | |
| 12 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 13 | |
| 14 | from apiclient.discovery import build |
| 15 | |
| 16 | import httplib2 |
| 17 | import pickle |
| 18 | import pprint |
| 19 | |
| 20 | # Uncomment the next line to get very detailed logging |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 21 | #httplib2.debuglevel = 4 |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 22 | |
| 23 | def main(): |
| 24 | f = open("buzz.dat", "r") |
| 25 | credentials = pickle.loads(f.read()) |
| 26 | f.close() |
| 27 | |
| 28 | http = httplib2.Http() |
| 29 | http = credentials.authorize(http) |
| 30 | |
Joe Gregorio | 3b3a191 | 2010-11-04 19:37:05 -0400 | [diff] [blame] | 31 | p = build("buzz", "v1", http=http, developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0") |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 32 | activities = p.activities() |
| 33 | |
| 34 | # Retrieve the first two activities |
| 35 | activitylist = activities.list(max_results='2', scope='@self', userId='@me').execute() |
| 36 | print "Retrieved the first two activities" |
| 37 | |
| 38 | # Retrieve the next two activities |
Joe Gregorio | 913e70d | 2010-11-05 15:38:23 -0400 | [diff] [blame] | 39 | if activitylist: |
| 40 | activitylist = activities.list_next(activitylist).execute() |
| 41 | print "Retrieved the next two activities" |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 42 | |
| 43 | # Add a new activity |
| 44 | new_activity_body = { |
Joe Gregorio | 913e70d | 2010-11-05 15:38:23 -0400 | [diff] [blame] | 45 | "data": { |
| 46 | 'title': 'Testing insert', |
| 47 | 'object': { |
| 48 | 'content': u'Just a short note to show that insert is working. ☄', |
| 49 | 'type': 'note'} |
| 50 | } |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 51 | } |
| 52 | activity = activities.insert(userId='@me', body=new_activity_body).execute() |
| 53 | print "Added a new activity" |
| 54 | |
| 55 | activitylist = activities.list(max_results='2', scope='@self', userId='@me').execute() |
| 56 | |
| 57 | # Add a comment to that activity |
| 58 | comment_body = { |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 59 | "data": { |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 60 | "content": "This is a comment" |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 61 | } |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 62 | } |
| 63 | item = activitylist['items'][0] |
| 64 | comment = p.comments().insert( |
| 65 | userId=item['actor']['id'], postId=item['id'], body=comment_body |
| 66 | ).execute() |
| 67 | print 'Added a comment to the new activity' |
| 68 | pprint.pprint(comment) |
| 69 | |
| 70 | if __name__ == '__main__': |
| 71 | main() |