blob: 74ee3a4fc29ab2f6c2e9d491db39fa042b7bdf98 [file] [log] [blame]
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -04001#!/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
8Command-line application that retrieves the users
9latest content and then adds a new entry.
10"""
11
12__author__ = 'jcgregorio@google.com (Joe Gregorio)'
13
Joe Gregorio6abf8702011-03-18 22:44:17 -040014import gflags
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040015import httplib2
Joe Gregorio6abf8702011-03-18 22:44:17 -040016import logging
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040017import pprint
Joe Gregorio6abf8702011-03-18 22:44:17 -040018import sys
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040019
Joe Gregorio6abf8702011-03-18 22:44:17 -040020from apiclient.discovery import build
21from oauth2client.file import Storage
22from oauth2client.client import OAuth2WebServerFlow
23from oauth2client.tools import run
24
25FLAGS = gflags.FLAGS
26FLOW = 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
32gflags.DEFINE_enum('logging_level', 'ERROR',
33 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
34 'Set the level of logging detail.')
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040035
Joe Gregorioaf276d22010-12-09 14:26:58 -050036
Joe Gregorio6abf8702011-03-18 22:44:17 -040037def 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 Gregoriofffa7d72011-02-18 17:20:39 -050046 storage = Storage('buzz.dat')
47 credentials = storage.get()
Joe Gregorioa0a52e42011-02-17 17:13:26 -050048 if credentials is None or credentials.invalid == True:
Joe Gregorio6abf8702011-03-18 22:44:17 -040049 credentials = run(FLOW, storage)
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040050
51 http = httplib2.Http()
52 http = credentials.authorize(http)
53
Joe Gregorio6abf8702011-03-18 22:44:17 -040054 # Build the Buzz service
Joe Gregorio1ae3e742011-02-25 15:17:14 -050055 service = build("buzz", "v1", http=http,
Joe Gregorio6abf8702011-03-18 22:44:17 -040056 developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
Joe Gregorio1ae3e742011-02-25 15:17:14 -050057 activities = service.activities()
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040058
Joe Gregorio6abf8702011-03-18 22:44:17 -040059 # 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.come3c8b6d2010-10-07 19:34:54 -040063
Joe Gregorio6abf8702011-03-18 22:44:17 -040064 # Retrieve the next two activities
65 if activitylist:
66 activitylist = activities.list_next(activitylist).execute()
67 print "Retrieved the next two activities"
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040068
Joe Gregorio6abf8702011-03-18 22:44:17 -040069 # 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 Gregorioa0a52e42011-02-17 17:13:26 -050079
Joe Gregorio6abf8702011-03-18 22:44:17 -040080 activitylist = activities.list(
81 max_results='2', scope='@self', userId='@me').execute()
Joe Gregorioa0a52e42011-02-17 17:13:26 -050082
Joe Gregorio6abf8702011-03-18 22:44:17 -040083 # 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.come3c8b6d2010-10-07 19:34:54 -040093
94if __name__ == '__main__':
Joe Gregorio6abf8702011-03-18 22:44:17 -040095 main(sys.argv)