blob: 416f58c8ebc534ab6ae39869ed440a6a08de1a95 [file] [log] [blame]
Joe Gregorio292b9b82011-01-12 11:36:11 -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
14from apiclient.discovery import build_from_document
15
16import httplib2
17import pickle
18import pprint
19
20# Uncomment the next line to get very detailed logging
21#httplib2.debuglevel = 4
22
23
24def main():
25 # Load the credentials and authorize
26 f = open("buzz.dat", "r")
27 credentials = pickle.loads(f.read())
28 f.close()
29
30 http = httplib2.Http()
31 http = credentials.authorize(http)
32
33 # Load the local copy of the discovery document
34 f = file("buzz.json", "r")
35 discovery = f.read()
36 f.close()
37
38 # Optionally load a futures discovery document
39 f = file("../../apiclient/contrib/buzz/future.json", "r")
40 future = f.read()
41 f.close()
42
43 # Construct a service from the local documents
44 p = build_from_document(discovery,
45 base="https://www.googleapis.com/",
46 future=future,
47 http=http,
48 developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
49 activities = p.activities()
50
51 # Retrieve the first two activities
52 activitylist = activities.list(
53 max_results='2', scope='@self', userId='@me').execute()
54 print "Retrieved the first two activities"
55
56 # Retrieve the next two activities
57 if activitylist:
58 activitylist = activities.list_next(activitylist).execute()
59 print "Retrieved the next two activities"
60
61 # Add a new activity
62 new_activity_body = {
63 "data": {
64 'title': 'Testing insert',
65 'object': {
66 'content': u'Just a short note to show that insert is working. ☄',
67 'type': 'note'}
68 }
69 }
70 activity = activities.insert(userId='@me', body=new_activity_body).execute()
71 print "Added a new activity"
72
73 activitylist = activities.list(
74 max_results='2', scope='@self', userId='@me').execute()
75
76 # Add a comment to that activity
77 comment_body = {
78 "data": {
79 "content": "This is a comment"
80 }
81 }
82 item = activitylist['items'][0]
83 comment = p.comments().insert(
84 userId=item['actor']['id'], postId=item['id'], body=comment_body
85 ).execute()
86 print 'Added a comment to the new activity'
87 pprint.pprint(comment)
88
89if __name__ == '__main__':
90 main()