/gdata/blogger/service.py

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