/gdata/blogger/client.py

http://radioappz.googlecode.com/ · Python · 154 lines · 89 code · 33 blank · 32 comment · 4 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. """Contains a client to communicate with the Blogger servers.
  17. For documentation on the Blogger API, see:
  18. http://code.google.com/apis/blogger/
  19. """
  20. __author__ = 'j.s@google.com (Jeff Scudder)'
  21. import gdata.client
  22. import gdata.gauth
  23. import gdata.blogger.data
  24. import atom.data
  25. import atom.http_core
  26. # List user's blogs, takes a user ID, or 'default'.
  27. BLOGS_URL = 'http://www.blogger.com/feeds/%s/blogs'
  28. # Takes a blog ID.
  29. BLOG_POST_URL = 'http://www.blogger.com/feeds/%s/posts/default'
  30. # Takes a blog ID and post ID.
  31. BLOG_POST_COMMENTS_URL = 'http://www.blogger.com/feeds/%s/%s/comments/default'
  32. # Takes a blog ID.
  33. BLOG_COMMENTS_URL = 'http://www.blogger.com/feeds/%s/comments/default'
  34. # Takes a blog ID.
  35. BLOG_ARCHIVE_URL = 'http://www.blogger.com/feeds/%s/archive/full'
  36. class BloggerClient(gdata.client.GDClient):
  37. api_version = '2'
  38. auth_service = 'blogger'
  39. auth_scopes = gdata.gauth.AUTH_SCOPES['blogger']
  40. def get_blogs(self, user_id='default', auth_token=None,
  41. desired_class=gdata.blogger.data.BlogFeed, **kwargs):
  42. return self.get_feed(BLOGS_URL % user_id, auth_token=auth_token,
  43. desired_class=desired_class, **kwargs)
  44. GetBlogs = get_blogs
  45. def get_posts(self, blog_id, auth_token=None,
  46. desired_class=gdata.blogger.data.BlogPostFeed, query=None,
  47. **kwargs):
  48. return self.get_feed(BLOG_POST_URL % blog_id, auth_token=auth_token,
  49. desired_class=desired_class, query=query, **kwargs)
  50. GetPosts = get_posts
  51. def get_post_comments(self, blog_id, post_id, auth_token=None,
  52. desired_class=gdata.blogger.data.CommentFeed,
  53. query=None, **kwargs):
  54. return self.get_feed(BLOG_POST_COMMENTS_URL % (blog_id, post_id),
  55. auth_token=auth_token, desired_class=desired_class,
  56. query=query, **kwargs)
  57. GetPostComments = get_post_comments
  58. def get_blog_comments(self, blog_id, auth_token=None,
  59. desired_class=gdata.blogger.data.CommentFeed,
  60. query=None, **kwargs):
  61. return self.get_feed(BLOG_COMMENTS_URL % blog_id, auth_token=auth_token,
  62. desired_class=desired_class, query=query, **kwargs)
  63. GetBlogComments = get_blog_comments
  64. def get_blog_archive(self, blog_id, auth_token=None, **kwargs):
  65. return self.get_feed(BLOG_ARCHIVE_URL % blog_id, auth_token=auth_token,
  66. **kwargs)
  67. GetBlogArchive = get_blog_archive
  68. def add_post(self, blog_id, title, body, labels=None, draft=False,
  69. auth_token=None, title_type='text', body_type='html', **kwargs):
  70. # Construct an atom Entry for the blog post to be sent to the server.
  71. new_entry = gdata.blogger.data.BlogPost(
  72. title=atom.data.Title(text=title, type=title_type),
  73. content=atom.data.Content(text=body, type=body_type))
  74. if labels:
  75. for label in labels:
  76. new_entry.add_label(label)
  77. if draft:
  78. new_entry.control = atom.data.Control(draft=atom.data.Draft(text='yes'))
  79. return self.post(new_entry, BLOG_POST_URL % blog_id, auth_token=auth_token, **kwargs)
  80. AddPost = add_post
  81. def add_comment(self, blog_id, post_id, body, auth_token=None,
  82. title_type='text', body_type='html', **kwargs):
  83. new_entry = gdata.blogger.data.Comment(
  84. content=atom.data.Content(text=body, type=body_type))
  85. return self.post(new_entry, BLOG_POST_COMMENTS_URL % (blog_id, post_id),
  86. auth_token=auth_token, **kwargs)
  87. AddComment = add_comment
  88. def update(self, entry, auth_token=None, **kwargs):
  89. # The Blogger API does not currently support ETags, so for now remove
  90. # the ETag before performing an update.
  91. old_etag = entry.etag
  92. entry.etag = None
  93. response = gdata.client.GDClient.update(self, entry,
  94. auth_token=auth_token, **kwargs)
  95. entry.etag = old_etag
  96. return response
  97. Update = update
  98. def delete(self, entry_or_uri, auth_token=None, **kwargs):
  99. if isinstance(entry_or_uri, (str, unicode, atom.http_core.Uri)):
  100. return gdata.client.GDClient.delete(self, entry_or_uri,
  101. auth_token=auth_token, **kwargs)
  102. # The Blogger API does not currently support ETags, so for now remove
  103. # the ETag before performing a delete.
  104. old_etag = entry_or_uri.etag
  105. entry_or_uri.etag = None
  106. response = gdata.client.GDClient.delete(self, entry_or_uri,
  107. auth_token=auth_token, **kwargs)
  108. # TODO: if GDClient.delete raises and exception, the entry's etag may be
  109. # left as None. Should revisit this logic.
  110. entry_or_uri.etag = old_etag
  111. return response
  112. Delete = delete
  113. class Query(gdata.client.Query):
  114. def __init__(self, order_by=None, **kwargs):
  115. gdata.client.Query.__init__(self, **kwargs)
  116. self.order_by = order_by
  117. def modify_request(self, http_request):
  118. gdata.client._add_query_param('orderby', self.order_by, http_request)
  119. gdata.client.Query.modify_request(self, http_request)
  120. ModifyRequest = modify_request