PageRenderTime 29ms CodeModel.GetById 12ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/gdata/analytics/__init__.py

http://radioappz.googlecode.com/
Python | 223 lines | 160 code | 20 blank | 43 comment | 4 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
 23"""Contains extensions to Atom objects used with Google Analytics."""
 24
 25__author__ = 'api.suryasev (Sal Uryasev)'
 26
 27import atom
 28import gdata
 29
 30GAN_NAMESPACE = 'http://schemas.google.com/analytics/2009'
 31
 32class TableId(gdata.GDataEntry):
 33  """tableId element."""
 34  _tag = 'tableId'
 35  _namespace = GAN_NAMESPACE  
 36
 37class Property(gdata.GDataEntry):
 38  _tag = 'property'
 39  _namespace = GAN_NAMESPACE
 40  _children = gdata.GDataEntry._children.copy()
 41  _attributes = gdata.GDataEntry._attributes.copy()
 42  
 43  _attributes['name'] = 'name'
 44  _attributes['value'] = 'value'
 45  
 46  def __init__(self, name=None, value=None, *args, **kwargs):
 47    self.name = name
 48    self.value = value
 49    super(Property, self).__init__(*args, **kwargs)
 50    
 51  def __str__(self):
 52    return self.value
 53      
 54  def __repr__(self):
 55    return self.value
 56
 57class AccountListEntry(gdata.GDataEntry):
 58  """The Google Documents version of an Atom Entry"""
 59  
 60  _tag = 'entry'
 61  _namespace = atom.ATOM_NAMESPACE
 62  _children = gdata.GDataEntry._children.copy()
 63  _attributes = gdata.GDataEntry._attributes.copy()
 64  _children['{%s}tableId' % GAN_NAMESPACE] = ('tableId', 
 65                          [TableId])
 66  _children['{%s}property' % GAN_NAMESPACE] = ('property', 
 67                          [Property])
 68
 69  def __init__(self, tableId=None, property=None,
 70         *args, **kwargs):
 71    self.tableId = tableId
 72    self.property = property
 73    super(AccountListEntry, self).__init__(*args, **kwargs)
 74
 75
 76def AccountListEntryFromString(xml_string):
 77  """Converts an XML string into an AccountListEntry object.
 78
 79  Args:
 80  xml_string: string The XML describing a Document List feed entry.
 81
 82  Returns:
 83  A AccountListEntry object corresponding to the given XML.
 84  """
 85  return atom.CreateClassFromXMLString(AccountListEntry, xml_string)
 86
 87
 88class AccountListFeed(gdata.GDataFeed):
 89  """A feed containing a list of Google Documents Items"""
 90  
 91  _tag = 'feed'
 92  _namespace = atom.ATOM_NAMESPACE
 93  _children = gdata.GDataFeed._children.copy()
 94  _attributes = gdata.GDataFeed._attributes.copy()
 95  _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', 
 96                          [AccountListEntry])
 97
 98
 99def AccountListFeedFromString(xml_string):
100  """Converts an XML string into an AccountListFeed object.
101
102  Args:
103  xml_string: string The XML describing an AccountList feed.
104
105  Returns:
106  An AccountListFeed object corresponding to the given XML.
107  All properties are also linked to with a direct reference
108  from each entry object for convenience. (e.g. entry.AccountName)
109  """
110  feed = atom.CreateClassFromXMLString(AccountListFeed, xml_string)
111  for entry in feed.entry:
112    for pro in entry.property:
113      entry.__dict__[pro.name.replace('ga:','')] = pro
114    for td in entry.tableId:
115      td.__dict__['value'] = td.text
116  return feed
117
118class Dimension(gdata.GDataEntry):
119  _tag = 'dimension'
120  _namespace = GAN_NAMESPACE
121  _children = gdata.GDataEntry._children.copy()
122  _attributes = gdata.GDataEntry._attributes.copy()
123  
124  _attributes['name'] = 'name'
125  _attributes['value'] = 'value'
126  _attributes['type'] = 'type'
127  _attributes['confidenceInterval'] = 'confidence_interval'
128  
129  def __init__(self, name=None, value=None, type=None, 
130               confidence_interval = None, *args, **kwargs):
131    self.name = name
132    self.value = value
133    self.type = type
134    self.confidence_interval = confidence_interval
135    super(Dimension, self).__init__(*args, **kwargs)   
136    
137  def __str__(self):
138    return self.value
139
140  def __repr__(self):
141    return self.value 
142    
143class Metric(gdata.GDataEntry):
144  _tag = 'metric'
145  _namespace = GAN_NAMESPACE
146  _children = gdata.GDataEntry._children.copy()
147  _attributes = gdata.GDataEntry._attributes.copy()
148  
149  _attributes['name'] = 'name'
150  _attributes['value'] = 'value'
151  _attributes['type'] = 'type'
152  _attributes['confidenceInterval'] = 'confidence_interval'
153  
154  def __init__(self, name=None, value=None, type=None, 
155               confidence_interval = None, *args, **kwargs):
156    self.name = name
157    self.value = value
158    self.type = type
159    self.confidence_interval = confidence_interval
160    super(Metric, self).__init__(*args, **kwargs)
161    
162  def __str__(self):
163    return self.value
164
165  def __repr__(self):
166    return self.value
167  
168class AnalyticsDataEntry(gdata.GDataEntry):
169  """The Google Analytics version of an Atom Entry"""
170  
171  _tag = 'entry'
172  _namespace = atom.ATOM_NAMESPACE
173  _children = gdata.GDataEntry._children.copy()
174  _attributes = gdata.GDataEntry._attributes.copy()
175  
176  _children['{%s}dimension' % GAN_NAMESPACE] = ('dimension', 
177                          [Dimension])
178                          
179  _children['{%s}metric' % GAN_NAMESPACE] = ('metric', 
180                         [Metric])
181                         
182  def __init__(self, dimension=None, metric=None, *args, **kwargs):
183    self.dimension = dimension
184    self.metric = metric
185    
186    super(AnalyticsDataEntry, self).__init__(*args, **kwargs)
187
188class AnalyticsDataFeed(gdata.GDataFeed):
189  """A feed containing a list of Google Analytics Data Feed"""
190  
191  _tag = 'feed'
192  _namespace = atom.ATOM_NAMESPACE
193  _children = gdata.GDataFeed._children.copy()
194  _attributes = gdata.GDataFeed._attributes.copy()
195  _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', 
196                          [AnalyticsDataEntry])
197                    
198                         
199"""
200Data Feed
201"""
202
203def AnalyticsDataFeedFromString(xml_string):
204  """Converts an XML string into an AccountListFeed object.
205
206  Args:
207  xml_string: string The XML describing an AccountList feed.
208
209  Returns:
210  An AccountListFeed object corresponding to the given XML.
211  Each metric and dimension is also referenced directly from
212  the entry for easier access. (e.g. entry.keyword.value)
213  """
214  feed = atom.CreateClassFromXMLString(AnalyticsDataFeed, xml_string)
215  if feed.entry:
216    for entry in feed.entry:
217      for met in entry.metric:
218        entry.__dict__[met.name.replace('ga:','')] = met
219      if entry.dimension is not None:
220        for dim in entry.dimension:
221          entry.__dict__[dim.name.replace('ga:','')] = dim
222        
223  return feed