blob: 331f500785ef2793b930a5d65a0d5fe828b969b2 [file] [log] [blame]
Joe Gregorio077363d2011-10-31 09:12:56 -04001#!/usr/bin/python2.4
2# -*- coding: utf-8 -*-
3#
4# Copyright (C) 2010 Google Inc.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18"""Simple command-line sample for Blogger.
19
20Command-line application that retrieves the users blogs and posts.
21
22Usage:
23 $ python blogger.py
24
25You can also get help on all the command-line flags the program understands
26by running:
27
28 $ python blogger.py --help
29
30To get detailed log output run:
31
32 $ python blogger.py --logging_level=DEBUG
33"""
34
35__author__ = 'jcgregorio@google.com (Joe Gregorio)'
36
Joe Gregorio077363d2011-10-31 09:12:56 -040037import sys
Joe Gregorio077363d2011-10-31 09:12:56 -040038
Joe Gregorio652492b2013-06-30 23:19:25 -040039from oauth2client import client
John Asmuth864311d2014-04-24 15:46:08 -040040from googleapiclient import sample_tools
Joe Gregorio077363d2011-10-31 09:12:56 -040041
42
43def main(argv):
Joe Gregorio652492b2013-06-30 23:19:25 -040044 # Authenticate and construct service.
45 service, flags = sample_tools.init(
46 argv, 'plus', 'v1', __doc__, __file__,
47 scope='https://www.googleapis.com/auth/blogger')
Joe Gregorio077363d2011-10-31 09:12:56 -040048
Joe Gregorio652492b2013-06-30 23:19:25 -040049 service = build('blogger', 'v2', http=http)
Joe Gregorio077363d2011-10-31 09:12:56 -040050
51 try:
52
53 users = service.users()
54
55 # Retrieve this user's profile information
Joe Gregorio652492b2013-06-30 23:19:25 -040056 thisuser = users.get(userId='self').execute(http=http)
57 print 'This user\'s display name is: %s' % thisuser['displayName']
Joe Gregorio077363d2011-10-31 09:12:56 -040058
59 # Retrieve the list of Blogs this user has write privileges on
Joe Gregorio652492b2013-06-30 23:19:25 -040060 thisusersblogs = users.blogs().list(userId='self').execute()
Joe Gregorio077363d2011-10-31 09:12:56 -040061 for blog in thisusersblogs['items']:
Joe Gregorio652492b2013-06-30 23:19:25 -040062 print 'The blog named \'%s\' is at: %s' % (blog['name'], blog['url'])
Joe Gregorio077363d2011-10-31 09:12:56 -040063
64 posts = service.posts()
65
66 # List the posts for each blog this user has
67 for blog in thisusersblogs['items']:
Joe Gregorio652492b2013-06-30 23:19:25 -040068 print 'The posts for %s:' % blog['name']
Joe Gregorio077363d2011-10-31 09:12:56 -040069 request = posts.list(blogId=blog['id'])
70 while request != None:
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040071 posts_doc = request.execute(http=http)
Joe Gregorio077363d2011-10-31 09:12:56 -040072 if 'items' in posts_doc and not (posts_doc['items'] is None):
73 for post in posts_doc['items']:
Joe Gregorio652492b2013-06-30 23:19:25 -040074 print ' %s (%s)' % (post['title'], post['url'])
Joe Gregorio077363d2011-10-31 09:12:56 -040075 request = posts.list_next(request, posts_doc)
76
Joe Gregorio652492b2013-06-30 23:19:25 -040077 except client.AccessTokenRefreshError:
78 print ('The credentials have been revoked or expired, please re-run'
79 'the application to re-authorize')
Joe Gregorio077363d2011-10-31 09:12:56 -040080
81if __name__ == '__main__':
82 main(sys.argv)