blob: 74ee3a4fc29ab2f6c2e9d491db39fa042b7bdf98 [file] [log] [blame]
Joe Gregorio695fdc12011-01-16 16:46:55 -05001#!/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 Gregorio205e73a2011-03-12 09:55:31 -050014import gflags
Joe Gregorio695fdc12011-01-16 16:46:55 -050015import httplib2
Joe Gregorio205e73a2011-03-12 09:55:31 -050016import logging
Joe Gregorio695fdc12011-01-16 16:46:55 -050017import pprint
Joe Gregorio205e73a2011-03-12 09:55:31 -050018import sys
Joe Gregorio695fdc12011-01-16 16:46:55 -050019
Joe Gregoriodeeb0202011-02-15 14:49:57 -050020from apiclient.discovery import build
21from oauth2client.file import Storage
22from oauth2client.client import OAuth2WebServerFlow
23from oauth2client.tools import run
24
Joe Gregorio205e73a2011-03-12 09:55:31 -050025FLAGS = 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.')
Joe Gregorio695fdc12011-01-16 16:46:55 -050035
36
Joe Gregorio205e73a2011-03-12 09:55:31 -050037def 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 Gregoriodeeb0202011-02-15 14:49:57 -050046 storage = Storage('buzz.dat')
47 credentials = storage.get()
Joe Gregorio9ce4b622011-02-17 15:32:11 -050048 if credentials is None or credentials.invalid == True:
Joe Gregorio205e73a2011-03-12 09:55:31 -050049 credentials = run(FLOW, storage)
Joe Gregorio695fdc12011-01-16 16:46:55 -050050
51 http = httplib2.Http()
52 http = credentials.authorize(http)
53
Joe Gregorio205e73a2011-03-12 09:55:31 -050054 # Build the Buzz service
Joe Gregorio1ae3e742011-02-25 15:17:14 -050055 service = build("buzz", "v1", http=http,
Joe Gregoriodeeb0202011-02-15 14:49:57 -050056 developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
Joe Gregorio1ae3e742011-02-25 15:17:14 -050057 activities = service.activities()
Joe Gregorio695fdc12011-01-16 16:46:55 -050058
59 # Retrieve the first two activities
Joe Gregoriodeeb0202011-02-15 14:49:57 -050060 activitylist = activities.list(
61 max_results='2', scope='@self', userId='@me').execute()
Joe Gregorio695fdc12011-01-16 16:46:55 -050062 print "Retrieved the first two activities"
63
64 # Retrieve the next two activities
65 if activitylist:
66 activitylist = activities.list_next(activitylist).execute()
67 print "Retrieved the next two activities"
68
69 # Add a new activity
70 new_activity_body = {
Joe Gregorio205e73a2011-03-12 09:55:31 -050071 'title': 'Testing insert',
72 'object': {
73 'content':
74 u'Just a short note to show that insert is working. ☄',
75 'type': 'note'}
Joe Gregorio695fdc12011-01-16 16:46:55 -050076 }
77 activity = activities.insert(userId='@me', body=new_activity_body).execute()
78 print "Added a new activity"
79
Joe Gregoriodeeb0202011-02-15 14:49:57 -050080 activitylist = activities.list(
81 max_results='2', scope='@self', userId='@me').execute()
Joe Gregorio695fdc12011-01-16 16:46:55 -050082
83 # Add a comment to that activity
84 comment_body = {
Joe Gregorio205e73a2011-03-12 09:55:31 -050085 "content": "This is a comment"
Joe Gregorio695fdc12011-01-16 16:46:55 -050086 }
87 item = activitylist['items'][0]
Joe Gregorio1ae3e742011-02-25 15:17:14 -050088 comment = service.comments().insert(
Joe Gregorio695fdc12011-01-16 16:46:55 -050089 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)
93
94if __name__ == '__main__':
Joe Gregorio205e73a2011-03-12 09:55:31 -050095 main(sys.argv)