Joe Gregorio | 077363d | 2011-10-31 09:12:56 -0400 | [diff] [blame] | 1 | #!/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 | |
| 20 | Command-line application that retrieves the users blogs and posts. |
| 21 | |
| 22 | Usage: |
| 23 | $ python blogger.py |
| 24 | |
| 25 | You can also get help on all the command-line flags the program understands |
| 26 | by running: |
| 27 | |
| 28 | $ python blogger.py --help |
| 29 | |
| 30 | To get detailed log output run: |
| 31 | |
| 32 | $ python blogger.py --logging_level=DEBUG |
| 33 | """ |
| 34 | |
| 35 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 36 | |
Joe Gregorio | 077363d | 2011-10-31 09:12:56 -0400 | [diff] [blame] | 37 | import sys |
Joe Gregorio | 077363d | 2011-10-31 09:12:56 -0400 | [diff] [blame] | 38 | |
Joe Gregorio | 652492b | 2013-06-30 23:19:25 -0400 | [diff] [blame] | 39 | from oauth2client import client |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 40 | from googleapiclient import sample_tools |
Joe Gregorio | 077363d | 2011-10-31 09:12:56 -0400 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def main(argv): |
Joe Gregorio | 652492b | 2013-06-30 23:19:25 -0400 | [diff] [blame] | 44 | # 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 Gregorio | 077363d | 2011-10-31 09:12:56 -0400 | [diff] [blame] | 48 | |
Joe Gregorio | 652492b | 2013-06-30 23:19:25 -0400 | [diff] [blame] | 49 | service = build('blogger', 'v2', http=http) |
Joe Gregorio | 077363d | 2011-10-31 09:12:56 -0400 | [diff] [blame] | 50 | |
| 51 | try: |
| 52 | |
| 53 | users = service.users() |
| 54 | |
| 55 | # Retrieve this user's profile information |
Joe Gregorio | 652492b | 2013-06-30 23:19:25 -0400 | [diff] [blame] | 56 | thisuser = users.get(userId='self').execute(http=http) |
| 57 | print 'This user\'s display name is: %s' % thisuser['displayName'] |
Joe Gregorio | 077363d | 2011-10-31 09:12:56 -0400 | [diff] [blame] | 58 | |
| 59 | # Retrieve the list of Blogs this user has write privileges on |
Joe Gregorio | 652492b | 2013-06-30 23:19:25 -0400 | [diff] [blame] | 60 | thisusersblogs = users.blogs().list(userId='self').execute() |
Joe Gregorio | 077363d | 2011-10-31 09:12:56 -0400 | [diff] [blame] | 61 | for blog in thisusersblogs['items']: |
Joe Gregorio | 652492b | 2013-06-30 23:19:25 -0400 | [diff] [blame] | 62 | print 'The blog named \'%s\' is at: %s' % (blog['name'], blog['url']) |
Joe Gregorio | 077363d | 2011-10-31 09:12:56 -0400 | [diff] [blame] | 63 | |
| 64 | posts = service.posts() |
| 65 | |
| 66 | # List the posts for each blog this user has |
| 67 | for blog in thisusersblogs['items']: |
Joe Gregorio | 652492b | 2013-06-30 23:19:25 -0400 | [diff] [blame] | 68 | print 'The posts for %s:' % blog['name'] |
Joe Gregorio | 077363d | 2011-10-31 09:12:56 -0400 | [diff] [blame] | 69 | request = posts.list(blogId=blog['id']) |
| 70 | while request != None: |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 71 | posts_doc = request.execute(http=http) |
Joe Gregorio | 077363d | 2011-10-31 09:12:56 -0400 | [diff] [blame] | 72 | if 'items' in posts_doc and not (posts_doc['items'] is None): |
| 73 | for post in posts_doc['items']: |
Joe Gregorio | 652492b | 2013-06-30 23:19:25 -0400 | [diff] [blame] | 74 | print ' %s (%s)' % (post['title'], post['url']) |
Joe Gregorio | 077363d | 2011-10-31 09:12:56 -0400 | [diff] [blame] | 75 | request = posts.list_next(request, posts_doc) |
| 76 | |
Joe Gregorio | 652492b | 2013-06-30 23:19:25 -0400 | [diff] [blame] | 77 | except client.AccessTokenRefreshError: |
| 78 | print ('The credentials have been revoked or expired, please re-run' |
| 79 | 'the application to re-authorize') |
Joe Gregorio | 077363d | 2011-10-31 09:12:56 -0400 | [diff] [blame] | 80 | |
| 81 | if __name__ == '__main__': |
| 82 | main(sys.argv) |