/gdata/blogger/__init__.py

http://radioappz.googlecode.com/ · Python · 202 lines · 137 code · 16 blank · 49 comment · 1 complexity · 28871d5579cab9d637808bc30ffa10c0 MD5 · raw file

  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2007, 2008 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 extensions to Atom objects used with Blogger."""
  17. __author__ = 'api.jscudder (Jeffrey Scudder)'
  18. import atom
  19. import gdata
  20. import re
  21. LABEL_SCHEME = 'http://www.blogger.com/atom/ns#'
  22. THR_NAMESPACE = 'http://purl.org/syndication/thread/1.0'
  23. class BloggerEntry(gdata.GDataEntry):
  24. """Adds convenience methods inherited by all Blogger entries."""
  25. blog_name_pattern = re.compile('(http://)(\w*)')
  26. blog_id_pattern = re.compile('(tag:blogger.com,1999:blog-)(\w*)')
  27. blog_id2_pattern = re.compile('tag:blogger.com,1999:user-(\d+)\.blog-(\d+)')
  28. def GetBlogId(self):
  29. """Extracts the Blogger id of this blog.
  30. This method is useful when contructing URLs by hand. The blog id is
  31. often used in blogger operation URLs. This should not be confused with
  32. the id member of a BloggerBlog. The id element is the Atom id XML element.
  33. The blog id which this method returns is a part of the Atom id.
  34. Returns:
  35. The blog's unique id as a string.
  36. """
  37. if self.id.text:
  38. match = self.blog_id_pattern.match(self.id.text)
  39. if match:
  40. return match.group(2)
  41. else:
  42. return self.blog_id2_pattern.match(self.id.text).group(2)
  43. return None
  44. def GetBlogName(self):
  45. """Finds the name of this blog as used in the 'alternate' URL.
  46. An alternate URL is in the form 'http://blogName.blogspot.com/'. For an
  47. entry representing the above example, this method would return 'blogName'.
  48. Returns:
  49. The blog's URL name component as a string.
  50. """
  51. for link in self.link:
  52. if link.rel == 'alternate':
  53. return self.blog_name_pattern.match(link.href).group(2)
  54. return None
  55. class BlogEntry(BloggerEntry):
  56. """Describes a blog entry in the feed listing a user's blogs."""
  57. def BlogEntryFromString(xml_string):
  58. return atom.CreateClassFromXMLString(BlogEntry, xml_string)
  59. class BlogFeed(gdata.GDataFeed):
  60. """Describes a feed of a user's blogs."""
  61. _children = gdata.GDataFeed._children.copy()
  62. _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [BlogEntry])
  63. def BlogFeedFromString(xml_string):
  64. return atom.CreateClassFromXMLString(BlogFeed, xml_string)
  65. class BlogPostEntry(BloggerEntry):
  66. """Describes a blog post entry in the feed of a blog's posts."""
  67. post_id_pattern = re.compile('(tag:blogger.com,1999:blog-)(\w*)(.post-)(\w*)')
  68. def AddLabel(self, label):
  69. """Adds a label to the blog post.
  70. The label is represented by an Atom category element, so this method
  71. is shorthand for appending a new atom.Category object.
  72. Args:
  73. label: str
  74. """
  75. self.category.append(atom.Category(scheme=LABEL_SCHEME, term=label))
  76. def GetPostId(self):
  77. """Extracts the postID string from the entry's Atom id.
  78. Returns: A string of digits which identify this post within the blog.
  79. """
  80. if self.id.text:
  81. return self.post_id_pattern.match(self.id.text).group(4)
  82. return None
  83. def BlogPostEntryFromString(xml_string):
  84. return atom.CreateClassFromXMLString(BlogPostEntry, xml_string)
  85. class BlogPostFeed(gdata.GDataFeed):
  86. """Describes a feed of a blog's posts."""
  87. _children = gdata.GDataFeed._children.copy()
  88. _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [BlogPostEntry])
  89. def BlogPostFeedFromString(xml_string):
  90. return atom.CreateClassFromXMLString(BlogPostFeed, xml_string)
  91. class InReplyTo(atom.AtomBase):
  92. _tag = 'in-reply-to'
  93. _namespace = THR_NAMESPACE
  94. _attributes = atom.AtomBase._attributes.copy()
  95. _attributes['href'] = 'href'
  96. _attributes['ref'] = 'ref'
  97. _attributes['source'] = 'source'
  98. _attributes['type'] = 'type'
  99. def __init__(self, href=None, ref=None, source=None, type=None,
  100. extension_elements=None, extension_attributes=None, text=None):
  101. self.href = href
  102. self.ref = ref
  103. self.source = source
  104. self.type = type
  105. self.extension_elements = extension_elements or []
  106. self.extension_attributes = extension_attributes or {}
  107. self.text = text
  108. def InReplyToFromString(xml_string):
  109. return atom.CreateClassFromXMLString(InReplyTo, xml_string)
  110. class CommentEntry(BloggerEntry):
  111. """Describes a blog post comment entry in the feed of a blog post's
  112. comments."""
  113. _children = BloggerEntry._children.copy()
  114. _children['{%s}in-reply-to' % THR_NAMESPACE] = ('in_reply_to', InReplyTo)
  115. comment_id_pattern = re.compile('.*-(\w*)$')
  116. def __init__(self, author=None, category=None, content=None,
  117. contributor=None, atom_id=None, link=None, published=None, rights=None,
  118. source=None, summary=None, control=None, title=None, updated=None,
  119. in_reply_to=None, extension_elements=None, extension_attributes=None,
  120. text=None):
  121. BloggerEntry.__init__(self, author=author, category=category,
  122. content=content, contributor=contributor, atom_id=atom_id, link=link,
  123. published=published, rights=rights, source=source, summary=summary,
  124. control=control, title=title, updated=updated,
  125. extension_elements=extension_elements,
  126. extension_attributes=extension_attributes, text=text)
  127. self.in_reply_to = in_reply_to
  128. def GetCommentId(self):
  129. """Extracts the commentID string from the entry's Atom id.
  130. Returns: A string of digits which identify this post within the blog.
  131. """
  132. if self.id.text:
  133. return self.comment_id_pattern.match(self.id.text).group(1)
  134. return None
  135. def CommentEntryFromString(xml_string):
  136. return atom.CreateClassFromXMLString(CommentEntry, xml_string)
  137. class CommentFeed(gdata.GDataFeed):
  138. """Describes a feed of a blog post's comments."""
  139. _children = gdata.GDataFeed._children.copy()
  140. _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [CommentEntry])
  141. def CommentFeedFromString(xml_string):
  142. return atom.CreateClassFromXMLString(CommentFeed, xml_string)