/gdata/blogger/data.py

http://radioappz.googlecode.com/ · Python · 146 lines · 70 code · 16 blank · 60 comment · 0 complexity · db03bc5c44ceee3cc8529f4021d9a9b4 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. """Data model classes for parsing and generating XML for the Blogger API."""
  17. __author__ = 'j.s@google.com (Jeff Scudder)'
  18. import re
  19. import atom.core
  20. import gdata.data
  21. LABEL_SCHEME = 'http://www.blogger.com/atom/ns#'
  22. THR_TEMPLATE = '{http://purl.org/syndication/thread/1.0}%s'
  23. BLOG_NAME_PATTERN = re.compile('(http://)(\w*)')
  24. BLOG_ID_PATTERN = re.compile('(tag:blogger.com,1999:blog-)(\w*)')
  25. BLOG_ID2_PATTERN = re.compile('tag:blogger.com,1999:user-(\d+)\.blog-(\d+)')
  26. POST_ID_PATTERN = re.compile(
  27. '(tag:blogger.com,1999:blog-)(\w*)(.post-)(\w*)')
  28. COMMENT_ID_PATTERN = re.compile('.*-(\w*)$')
  29. class BloggerEntry(gdata.data.GDEntry):
  30. """Adds convenience methods inherited by all Blogger entries."""
  31. def get_blog_id(self):
  32. """Extracts the Blogger id of this blog.
  33. This method is useful when contructing URLs by hand. The blog id is
  34. often used in blogger operation URLs. This should not be confused with
  35. the id member of a BloggerBlog. The id element is the Atom id XML element.
  36. The blog id which this method returns is a part of the Atom id.
  37. Returns:
  38. The blog's unique id as a string.
  39. """
  40. if self.id.text:
  41. match = BLOG_ID_PATTERN.match(self.id.text)
  42. if match:
  43. return match.group(2)
  44. else:
  45. return BLOG_ID2_PATTERN.match(self.id.text).group(2)
  46. return None
  47. GetBlogId = get_blog_id
  48. def get_blog_name(self):
  49. """Finds the name of this blog as used in the 'alternate' URL.
  50. An alternate URL is in the form 'http://blogName.blogspot.com/'. For an
  51. entry representing the above example, this method would return 'blogName'.
  52. Returns:
  53. The blog's URL name component as a string.
  54. """
  55. for link in self.link:
  56. if link.rel == 'alternate':
  57. return BLOG_NAME_PATTERN.match(link.href).group(2)
  58. return None
  59. GetBlogName = get_blog_name
  60. class Blog(BloggerEntry):
  61. """Represents a blog which belongs to the user."""
  62. class BlogFeed(gdata.data.GDFeed):
  63. entry = [Blog]
  64. class BlogPost(BloggerEntry):
  65. """Represents a single post on a blog."""
  66. def add_label(self, label):
  67. """Adds a label to the blog post.
  68. The label is represented by an Atom category element, so this method
  69. is shorthand for appending a new atom.Category object.
  70. Args:
  71. label: str
  72. """
  73. self.category.append(atom.data.Category(scheme=LABEL_SCHEME, term=label))
  74. AddLabel = add_label
  75. def get_post_id(self):
  76. """Extracts the postID string from the entry's Atom id.
  77. Returns: A string of digits which identify this post within the blog.
  78. """
  79. if self.id.text:
  80. return POST_ID_PATTERN.match(self.id.text).group(4)
  81. return None
  82. GetPostId = get_post_id
  83. class BlogPostFeed(gdata.data.GDFeed):
  84. entry = [BlogPost]
  85. class InReplyTo(atom.core.XmlElement):
  86. _qname = THR_TEMPLATE % 'in-reply-to'
  87. href = 'href'
  88. ref = 'ref'
  89. source = 'source'
  90. type = 'type'
  91. class Comment(BloggerEntry):
  92. """Blog post comment entry in a feed listing comments on a post or blog."""
  93. in_reply_to = InReplyTo
  94. def get_comment_id(self):
  95. """Extracts the commentID string from the entry's Atom id.
  96. Returns: A string of digits which identify this post within the blog.
  97. """
  98. if self.id.text:
  99. return COMMENT_ID_PATTERN.match(self.id.text).group(1)
  100. return None
  101. GetCommentId = get_comment_id
  102. class CommentFeed(gdata.data.GDFeed):
  103. entry = [Comment]