PageRenderTime 23ms CodeModel.GetById 9ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/gdata/blogger/client.py

http://radioappz.googlecode.com/
Python | 154 lines | 89 code | 33 blank | 32 comment | 5 complexity | dcb3670d63c853632ff33061d3741f8a MD5 | raw file
  1#!/usr/bin/env python
  2#
  3# Copyright (C) 2009 Google Inc.
  4#
  5# Licensed under the Apache License, Version 2.0 (the "License");
  6# you may not use this file except in compliance with the License.
  7# You may obtain a copy of the License at
  8#
  9#      http://www.apache.org/licenses/LICENSE-2.0
 10#
 11# Unless required by applicable law or agreed to in writing, software
 12# distributed under the License is distributed on an "AS IS" BASIS,
 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14# See the License for the specific language governing permissions and
 15# limitations under the License.
 16
 17
 18"""Contains a client to communicate with the Blogger servers.
 19
 20For documentation on the Blogger API, see:
 21http://code.google.com/apis/blogger/
 22"""
 23
 24
 25__author__ = 'j.s@google.com (Jeff Scudder)'
 26
 27
 28import gdata.client
 29import gdata.gauth
 30import gdata.blogger.data
 31import atom.data
 32import atom.http_core
 33
 34
 35# List user's blogs, takes a user ID, or 'default'.
 36BLOGS_URL = 'http://www.blogger.com/feeds/%s/blogs'
 37# Takes a blog ID.
 38BLOG_POST_URL = 'http://www.blogger.com/feeds/%s/posts/default'
 39# Takes a blog ID and post ID.
 40BLOG_POST_COMMENTS_URL = 'http://www.blogger.com/feeds/%s/%s/comments/default'
 41# Takes a blog ID.
 42BLOG_COMMENTS_URL = 'http://www.blogger.com/feeds/%s/comments/default'
 43# Takes a blog ID.
 44BLOG_ARCHIVE_URL = 'http://www.blogger.com/feeds/%s/archive/full'
 45
 46
 47class BloggerClient(gdata.client.GDClient):
 48  api_version = '2'
 49  auth_service = 'blogger'
 50  auth_scopes = gdata.gauth.AUTH_SCOPES['blogger']
 51
 52  def get_blogs(self, user_id='default', auth_token=None,
 53                desired_class=gdata.blogger.data.BlogFeed, **kwargs):
 54    return self.get_feed(BLOGS_URL % user_id, auth_token=auth_token,
 55                         desired_class=desired_class, **kwargs)
 56
 57  GetBlogs = get_blogs
 58
 59  def get_posts(self, blog_id, auth_token=None,
 60                desired_class=gdata.blogger.data.BlogPostFeed, query=None,
 61                **kwargs):
 62    return self.get_feed(BLOG_POST_URL % blog_id, auth_token=auth_token,
 63                         desired_class=desired_class, query=query, **kwargs)
 64
 65  GetPosts = get_posts
 66
 67  def get_post_comments(self, blog_id, post_id,  auth_token=None,
 68                        desired_class=gdata.blogger.data.CommentFeed,
 69                        query=None, **kwargs):
 70    return self.get_feed(BLOG_POST_COMMENTS_URL % (blog_id, post_id),
 71                         auth_token=auth_token, desired_class=desired_class,
 72                         query=query, **kwargs)
 73
 74  GetPostComments = get_post_comments
 75
 76  def get_blog_comments(self, blog_id, auth_token=None,
 77                        desired_class=gdata.blogger.data.CommentFeed,
 78                        query=None, **kwargs):
 79    return self.get_feed(BLOG_COMMENTS_URL % blog_id, auth_token=auth_token,
 80                         desired_class=desired_class, query=query, **kwargs)
 81
 82  GetBlogComments = get_blog_comments
 83
 84  def get_blog_archive(self, blog_id, auth_token=None, **kwargs):
 85    return self.get_feed(BLOG_ARCHIVE_URL % blog_id, auth_token=auth_token,
 86                         **kwargs)
 87
 88  GetBlogArchive = get_blog_archive
 89
 90  def add_post(self, blog_id, title, body, labels=None, draft=False,
 91               auth_token=None, title_type='text', body_type='html', **kwargs):
 92    # Construct an atom Entry for the blog post to be sent to the server.
 93    new_entry = gdata.blogger.data.BlogPost(
 94        title=atom.data.Title(text=title, type=title_type),
 95        content=atom.data.Content(text=body, type=body_type))
 96    if labels:
 97      for label in labels:
 98        new_entry.add_label(label)
 99    if draft:
100      new_entry.control = atom.data.Control(draft=atom.data.Draft(text='yes'))
101    return self.post(new_entry, BLOG_POST_URL % blog_id, auth_token=auth_token, **kwargs)
102
103  AddPost = add_post
104
105  def add_comment(self, blog_id, post_id, body, auth_token=None,
106                  title_type='text', body_type='html', **kwargs):
107    new_entry = gdata.blogger.data.Comment(
108        content=atom.data.Content(text=body, type=body_type))
109    return self.post(new_entry, BLOG_POST_COMMENTS_URL % (blog_id, post_id),
110                     auth_token=auth_token, **kwargs)
111
112  AddComment = add_comment
113
114  def update(self, entry, auth_token=None, **kwargs):
115    # The Blogger API does not currently support ETags, so for now remove
116    # the ETag before performing an update.
117    old_etag = entry.etag
118    entry.etag = None
119    response = gdata.client.GDClient.update(self, entry,
120                                            auth_token=auth_token, **kwargs)
121    entry.etag = old_etag
122    return response
123
124  Update = update
125
126  def delete(self, entry_or_uri, auth_token=None, **kwargs):
127    if isinstance(entry_or_uri, (str, unicode, atom.http_core.Uri)):
128      return gdata.client.GDClient.delete(self, entry_or_uri,
129                                          auth_token=auth_token, **kwargs)
130    # The Blogger API does not currently support ETags, so for now remove
131    # the ETag before performing a delete.
132    old_etag = entry_or_uri.etag
133    entry_or_uri.etag = None
134    response = gdata.client.GDClient.delete(self, entry_or_uri,
135                                            auth_token=auth_token, **kwargs)
136    # TODO: if GDClient.delete raises and exception, the entry's etag may be
137    # left as None. Should revisit this logic.
138    entry_or_uri.etag = old_etag
139    return response
140
141  Delete = delete
142
143
144class Query(gdata.client.Query):
145
146  def __init__(self, order_by=None, **kwargs):
147    gdata.client.Query.__init__(self, **kwargs)
148    self.order_by = order_by
149
150  def modify_request(self, http_request):
151    gdata.client._add_query_param('orderby', self.order_by, http_request)
152    gdata.client.Query.modify_request(self, http_request)
153
154  ModifyRequest = modify_request