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 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame^] | 14 | import gflags |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 15 | import httplib2 |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame^] | 16 | import logging |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 17 | import pprint |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame^] | 18 | import sys |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 19 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame^] | 20 | from apiclient.discovery import build |
| 21 | from oauth2client.file import Storage |
| 22 | from oauth2client.client import OAuth2WebServerFlow |
| 23 | from oauth2client.tools import run |
| 24 | |
| 25 | FLAGS = gflags.FLAGS |
| 26 | FLOW = OAuth2WebServerFlow( |
| 27 | client_id='433807057907.apps.googleusercontent.com', |
| 28 | client_secret='jigtZpMApkRxncxikFpR+SFg', |
| 29 | scope='https://www.googleapis.com/auth/buzz', |
| 30 | user_agent='buzz-cmdline-sample/1.0') |
| 31 | |
| 32 | gflags.DEFINE_enum('logging_level', 'ERROR', |
| 33 | ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 34 | 'Set the level of logging detail.') |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 35 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 36 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame^] | 37 | def main(argv): |
| 38 | try: |
| 39 | argv = FLAGS(argv) |
| 40 | except gflags.FlagsError, e: |
| 41 | print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS) |
| 42 | sys.exit(1) |
| 43 | |
| 44 | logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) |
| 45 | |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 46 | storage = Storage('buzz.dat') |
| 47 | credentials = storage.get() |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 48 | if credentials is None or credentials.invalid == True: |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame^] | 49 | credentials = run(FLOW, storage) |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 50 | |
| 51 | http = httplib2.Http() |
| 52 | http = credentials.authorize(http) |
| 53 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame^] | 54 | # Build the Buzz service |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame] | 55 | service = build("buzz", "v1", http=http, |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame^] | 56 | developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0") |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame] | 57 | activities = service.activities() |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 58 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame^] | 59 | # Retrieve the first two activities |
| 60 | activitylist = activities.list( |
| 61 | max_results='2', scope='@self', userId='@me').execute() |
| 62 | print "Retrieved the first two activities" |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 63 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame^] | 64 | # Retrieve the next two activities |
| 65 | if activitylist: |
| 66 | activitylist = activities.list_next(activitylist).execute() |
| 67 | print "Retrieved the next two activities" |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 68 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame^] | 69 | # Add a new activity |
| 70 | new_activity_body = { |
| 71 | 'title': 'Testing insert', |
| 72 | 'object': { |
| 73 | 'content': |
| 74 | u'Just a short note to show that insert is working. ☄', |
| 75 | 'type': 'note'} |
| 76 | } |
| 77 | activity = activities.insert(userId='@me', body=new_activity_body).execute() |
| 78 | print "Added a new activity" |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 79 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame^] | 80 | activitylist = activities.list( |
| 81 | max_results='2', scope='@self', userId='@me').execute() |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 82 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame^] | 83 | # Add a comment to that activity |
| 84 | comment_body = { |
| 85 | "content": "This is a comment" |
| 86 | } |
| 87 | item = activitylist['items'][0] |
| 88 | comment = service.comments().insert( |
| 89 | userId=item['actor']['id'], postId=item['id'], body=comment_body |
| 90 | ).execute() |
| 91 | print 'Added a comment to the new activity' |
| 92 | pprint.pprint(comment) |
jcgregorio@google.com | e3c8b6d | 2010-10-07 19:34:54 -0400 | [diff] [blame] | 93 | |
| 94 | if __name__ == '__main__': |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame^] | 95 | main(sys.argv) |