/gdata/analytics/__init__.py

http://radioappz.googlecode.com/ · Python · 223 lines · 123 code · 42 blank · 58 comment · 8 complexity · 9bec4cbe54b6041b7911b335594e820a MD5 · raw file

  1. #!/usr/bin/python
  2. #
  3. # Original Copyright (C) 2006 Google Inc.
  4. # Refactored in 2009 to work for Google Analytics by Sal Uryasev at Juice Inc.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. # Note that this module will not function without specifically adding
  19. # 'analytics': [ #Google Analytics
  20. # 'https://www.google.com/analytics/feeds/'],
  21. # to CLIENT_LOGIN_SCOPES in the gdata/service.py file
  22. """Contains extensions to Atom objects used with Google Analytics."""
  23. __author__ = 'api.suryasev (Sal Uryasev)'
  24. import atom
  25. import gdata
  26. GAN_NAMESPACE = 'http://schemas.google.com/analytics/2009'
  27. class TableId(gdata.GDataEntry):
  28. """tableId element."""
  29. _tag = 'tableId'
  30. _namespace = GAN_NAMESPACE
  31. class Property(gdata.GDataEntry):
  32. _tag = 'property'
  33. _namespace = GAN_NAMESPACE
  34. _children = gdata.GDataEntry._children.copy()
  35. _attributes = gdata.GDataEntry._attributes.copy()
  36. _attributes['name'] = 'name'
  37. _attributes['value'] = 'value'
  38. def __init__(self, name=None, value=None, *args, **kwargs):
  39. self.name = name
  40. self.value = value
  41. super(Property, self).__init__(*args, **kwargs)
  42. def __str__(self):
  43. return self.value
  44. def __repr__(self):
  45. return self.value
  46. class AccountListEntry(gdata.GDataEntry):
  47. """The Google Documents version of an Atom Entry"""
  48. _tag = 'entry'
  49. _namespace = atom.ATOM_NAMESPACE
  50. _children = gdata.GDataEntry._children.copy()
  51. _attributes = gdata.GDataEntry._attributes.copy()
  52. _children['{%s}tableId' % GAN_NAMESPACE] = ('tableId',
  53. [TableId])
  54. _children['{%s}property' % GAN_NAMESPACE] = ('property',
  55. [Property])
  56. def __init__(self, tableId=None, property=None,
  57. *args, **kwargs):
  58. self.tableId = tableId
  59. self.property = property
  60. super(AccountListEntry, self).__init__(*args, **kwargs)
  61. def AccountListEntryFromString(xml_string):
  62. """Converts an XML string into an AccountListEntry object.
  63. Args:
  64. xml_string: string The XML describing a Document List feed entry.
  65. Returns:
  66. A AccountListEntry object corresponding to the given XML.
  67. """
  68. return atom.CreateClassFromXMLString(AccountListEntry, xml_string)
  69. class AccountListFeed(gdata.GDataFeed):
  70. """A feed containing a list of Google Documents Items"""
  71. _tag = 'feed'
  72. _namespace = atom.ATOM_NAMESPACE
  73. _children = gdata.GDataFeed._children.copy()
  74. _attributes = gdata.GDataFeed._attributes.copy()
  75. _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry',
  76. [AccountListEntry])
  77. def AccountListFeedFromString(xml_string):
  78. """Converts an XML string into an AccountListFeed object.
  79. Args:
  80. xml_string: string The XML describing an AccountList feed.
  81. Returns:
  82. An AccountListFeed object corresponding to the given XML.
  83. All properties are also linked to with a direct reference
  84. from each entry object for convenience. (e.g. entry.AccountName)
  85. """
  86. feed = atom.CreateClassFromXMLString(AccountListFeed, xml_string)
  87. for entry in feed.entry:
  88. for pro in entry.property:
  89. entry.__dict__[pro.name.replace('ga:','')] = pro
  90. for td in entry.tableId:
  91. td.__dict__['value'] = td.text
  92. return feed
  93. class Dimension(gdata.GDataEntry):
  94. _tag = 'dimension'
  95. _namespace = GAN_NAMESPACE
  96. _children = gdata.GDataEntry._children.copy()
  97. _attributes = gdata.GDataEntry._attributes.copy()
  98. _attributes['name'] = 'name'
  99. _attributes['value'] = 'value'
  100. _attributes['type'] = 'type'
  101. _attributes['confidenceInterval'] = 'confidence_interval'
  102. def __init__(self, name=None, value=None, type=None,
  103. confidence_interval = None, *args, **kwargs):
  104. self.name = name
  105. self.value = value
  106. self.type = type
  107. self.confidence_interval = confidence_interval
  108. super(Dimension, self).__init__(*args, **kwargs)
  109. def __str__(self):
  110. return self.value
  111. def __repr__(self):
  112. return self.value
  113. class Metric(gdata.GDataEntry):
  114. _tag = 'metric'
  115. _namespace = GAN_NAMESPACE
  116. _children = gdata.GDataEntry._children.copy()
  117. _attributes = gdata.GDataEntry._attributes.copy()
  118. _attributes['name'] = 'name'
  119. _attributes['value'] = 'value'
  120. _attributes['type'] = 'type'
  121. _attributes['confidenceInterval'] = 'confidence_interval'
  122. def __init__(self, name=None, value=None, type=None,
  123. confidence_interval = None, *args, **kwargs):
  124. self.name = name
  125. self.value = value
  126. self.type = type
  127. self.confidence_interval = confidence_interval
  128. super(Metric, self).__init__(*args, **kwargs)
  129. def __str__(self):
  130. return self.value
  131. def __repr__(self):
  132. return self.value
  133. class AnalyticsDataEntry(gdata.GDataEntry):
  134. """The Google Analytics version of an Atom Entry"""
  135. _tag = 'entry'
  136. _namespace = atom.ATOM_NAMESPACE
  137. _children = gdata.GDataEntry._children.copy()
  138. _attributes = gdata.GDataEntry._attributes.copy()
  139. _children['{%s}dimension' % GAN_NAMESPACE] = ('dimension',
  140. [Dimension])
  141. _children['{%s}metric' % GAN_NAMESPACE] = ('metric',
  142. [Metric])
  143. def __init__(self, dimension=None, metric=None, *args, **kwargs):
  144. self.dimension = dimension
  145. self.metric = metric
  146. super(AnalyticsDataEntry, self).__init__(*args, **kwargs)
  147. class AnalyticsDataFeed(gdata.GDataFeed):
  148. """A feed containing a list of Google Analytics Data Feed"""
  149. _tag = 'feed'
  150. _namespace = atom.ATOM_NAMESPACE
  151. _children = gdata.GDataFeed._children.copy()
  152. _attributes = gdata.GDataFeed._attributes.copy()
  153. _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry',
  154. [AnalyticsDataEntry])
  155. """
  156. Data Feed
  157. """
  158. def AnalyticsDataFeedFromString(xml_string):
  159. """Converts an XML string into an AccountListFeed object.
  160. Args:
  161. xml_string: string The XML describing an AccountList feed.
  162. Returns:
  163. An AccountListFeed object corresponding to the given XML.
  164. Each metric and dimension is also referenced directly from
  165. the entry for easier access. (e.g. entry.keyword.value)
  166. """
  167. feed = atom.CreateClassFromXMLString(AnalyticsDataFeed, xml_string)
  168. if feed.entry:
  169. for entry in feed.entry:
  170. for met in entry.metric:
  171. entry.__dict__[met.name.replace('ga:','')] = met
  172. if entry.dimension is not None:
  173. for dim in entry.dimension:
  174. entry.__dict__[dim.name.replace('ga:','')] = dim
  175. return feed