blob: 72ef991c38d4509ce49c702d369f597572f907ea [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
14from apiclient.discovery import build
15
16import httplib2
17import pickle
18import pprint
19
20# Uncomment the next line to get very detailed logging
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -050021#httplib2.debuglevel = 4
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040022
Joe Gregorioaf276d22010-12-09 14:26:58 -050023
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040024def main():
25 f = open("buzz.dat", "r")
26 credentials = pickle.loads(f.read())
27 f.close()
28
29 http = httplib2.Http()
30 http = credentials.authorize(http)
31
Joe Gregorio88243be2011-01-11 14:13:06 -050032 p = build("buzz", "v1", http=http,
33 developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0")
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040034 activities = p.activities()
35
36 # Retrieve the first two activities
Joe Gregorio88243be2011-01-11 14:13:06 -050037 activitylist = activities.list(
38 max_results='2', scope='@self', userId='@me').execute()
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040039 print "Retrieved the first two activities"
40
41 # Retrieve the next two activities
Joe Gregorio913e70d2010-11-05 15:38:23 -040042 if activitylist:
43 activitylist = activities.list_next(activitylist).execute()
44 print "Retrieved the next two activities"
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040045
46 # Add a new activity
47 new_activity_body = {
Joe Gregorio913e70d2010-11-05 15:38:23 -040048 "data": {
Joe Gregorio88243be2011-01-11 14:13:06 -050049 'title': 'Testing insert',
50 'object': {
51 'content': u'Just a short note to show that insert is working. ☄',
52 'type': 'note'}
53 }
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040054 }
55 activity = activities.insert(userId='@me', body=new_activity_body).execute()
56 print "Added a new activity"
57
Joe Gregorio88243be2011-01-11 14:13:06 -050058 activitylist = activities.list(
59 max_results='2', scope='@self', userId='@me').execute()
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040060
61 # Add a comment to that activity
62 comment_body = {
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -050063 "data": {
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040064 "content": "This is a comment"
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -050065 }
jcgregorio@google.come3c8b6d2010-10-07 19:34:54 -040066 }
67 item = activitylist['items'][0]
68 comment = p.comments().insert(
69 userId=item['actor']['id'], postId=item['id'], body=comment_body
70 ).execute()
71 print 'Added a comment to the new activity'
72 pprint.pprint(comment)
73
74if __name__ == '__main__':
75 main()