PageRenderTime 51ms CodeModel.GetById 15ms app.highlight 30ms RepoModel.GetById 1ms app.codeStats 0ms

/gdata/blogger/service.py

http://radioappz.googlecode.com/
Python | 142 lines | 112 code | 7 blank | 23 comment | 2 complexity | b5afbdb6aa6d138e84efe589d4c78bc6 MD5 | raw file
  1#!/usr/bin/python
  2#
  3# Copyright (C) 2007 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"""Classes to interact with the Blogger server."""
 18
 19__author__ = 'api.jscudder (Jeffrey Scudder)'
 20
 21import gdata.service
 22import gdata.blogger
 23
 24
 25class BloggerService(gdata.service.GDataService):
 26
 27  def __init__(self, email=None, password=None, source=None,
 28               server='www.blogger.com', **kwargs):
 29    """Creates a client for the Blogger service.
 30
 31    Args:
 32      email: string (optional) The user's email address, used for
 33          authentication.
 34      password: string (optional) The user's password.
 35      source: string (optional) The name of the user's application.
 36      server: string (optional) The name of the server to which a connection
 37          will be opened. Default value: 'www.blogger.com'.
 38      **kwargs: The other parameters to pass to gdata.service.GDataService
 39          constructor.
 40    """
 41    gdata.service.GDataService.__init__(
 42        self, email=email, password=password, service='blogger', source=source,
 43        server=server, **kwargs)
 44
 45  def GetBlogFeed(self, uri=None):
 46    """Retrieve a list of the blogs to which the current user may manage."""
 47    if not uri:
 48      uri = '/feeds/default/blogs'
 49    return self.Get(uri, converter=gdata.blogger.BlogFeedFromString)
 50
 51  def GetBlogCommentFeed(self, blog_id=None, uri=None):
 52    """Retrieve a list of the comments for this blog."""
 53    if blog_id:
 54      uri = '/feeds/%s/comments/default' % blog_id
 55    return self.Get(uri, converter=gdata.blogger.CommentFeedFromString)
 56
 57  def GetBlogPostFeed(self, blog_id=None, uri=None):
 58    if blog_id:
 59      uri = '/feeds/%s/posts/default' % blog_id
 60    return self.Get(uri, converter=gdata.blogger.BlogPostFeedFromString)
 61
 62  def GetPostCommentFeed(self, blog_id=None, post_id=None, uri=None):
 63    """Retrieve a list of the comments for this particular blog post."""
 64    if blog_id and post_id:
 65      uri = '/feeds/%s/%s/comments/default' % (blog_id, post_id)
 66    return self.Get(uri, converter=gdata.blogger.CommentFeedFromString)
 67
 68  def AddPost(self, entry, blog_id=None, uri=None):
 69    if blog_id:
 70      uri = '/feeds/%s/posts/default' % blog_id
 71    return self.Post(entry, uri, 
 72                     converter=gdata.blogger.BlogPostEntryFromString)
 73
 74  def UpdatePost(self, entry, uri=None):
 75    if not uri:
 76      uri = entry.GetEditLink().href
 77    return self.Put(entry, uri, 
 78                    converter=gdata.blogger.BlogPostEntryFromString)
 79
 80  def DeletePost(self, entry=None, uri=None):
 81    if not uri:
 82      uri = entry.GetEditLink().href
 83    return self.Delete(uri)
 84
 85  def AddComment(self, comment_entry, blog_id=None, post_id=None, uri=None):
 86    """Adds a new comment to the specified blog post."""
 87    if blog_id and post_id:
 88      uri = '/feeds/%s/%s/comments/default' % (blog_id, post_id)
 89    return self.Post(comment_entry, uri, 
 90                     converter=gdata.blogger.CommentEntryFromString)
 91
 92  def DeleteComment(self, entry=None, uri=None):
 93    if not uri:
 94      uri = entry.GetEditLink().href
 95    return self.Delete(uri)
 96    
 97
 98class BlogQuery(gdata.service.Query):
 99
100  def __init__(self, feed=None, params=None, categories=None, blog_id=None):
101    """Constructs a query object for the list of a user's Blogger blogs.
102    
103    Args:
104      feed: str (optional) The beginning of the URL to be queried. If the
105          feed is not set, and there is no blog_id passed in, the default
106          value is used ('/feeds/default/blogs').
107      params: dict (optional)
108      categories: list (optional)
109      blog_id: str (optional)
110    """
111    if not feed and blog_id:
112      feed = '/feeds/default/blogs/%s' % blog_id
113    elif not feed:
114      feed = '/feeds/default/blogs'
115    gdata.service.Query.__init__(self, feed=feed, params=params, 
116        categories=categories)
117
118
119class BlogPostQuery(gdata.service.Query):
120
121  def __init__(self, feed=None, params=None, categories=None, blog_id=None, 
122      post_id=None):
123    if not feed and blog_id and post_id:
124      feed = '/feeds/%s/posts/default/%s' % (blog_id, post_id)
125    elif not feed and blog_id:
126      feed = '/feeds/%s/posts/default' % blog_id
127    gdata.service.Query.__init__(self, feed=feed, params=params,
128        categories=categories)
129
130
131class BlogCommentQuery(gdata.service.Query):
132
133  def __init__(self, feed=None, params=None, categories=None, blog_id=None, 
134      post_id=None, comment_id=None):
135    if not feed and blog_id and comment_id:
136      feed = '/feeds/%s/comments/default/%s' % (blog_id, comment_id)
137    elif not feed and blog_id and post_id:
138      feed = '/feeds/%s/%s/comments/default' % (blog_id, post_id)
139    elif not feed and blog_id:
140      feed = '/feeds/%s/comments/default' % blog_id
141    gdata.service.Query.__init__(self, feed=feed, params=params,
142        categories=categories)