Browse files

Working out some tests and setting up a config file parser

  • Loading branch information...
1 parent 21967b5 commit f14c8ed3daf1b0a4414099c430f3019d30509dc6 Clinton Ecker committed Apr 21, 2009
Showing with 50 additions and 21 deletions.
  1. +1 −0 .gitignore
  2. +5 −1 buildout.cfg
  3. +2 −2 setup.py
  4. +13 −0 src/googleanalytics/config.py
  5. +29 −18 src/googleanalytics/connection.py
View
1 .gitignore
@@ -8,3 +8,4 @@ downloads
eggs
parts
src/*.egg-info
+tests.py
View
6 buildout.cfg
@@ -1,9 +1,13 @@
[buildout]
-parts = python
+parts = python test
develop = .
eggs = python-googleanalytics
[python]
recipe = zc.recipe.egg
interpreter = python
+eggs = ${buildout:eggs}
+
+[test]
+recipe = zc.recipe.testrunner
eggs = ${buildout:eggs}
View
4 setup.py
@@ -7,7 +7,7 @@
license = 'BSD',
description = "A python library for talking to the Google Analytics API",
author = 'Clint Ecker',
- packages = find_packages('src'),
- package_dir = {'': 'src'},
+ packages=['googleanalytics'],
+ package_dir={'': 'src'},
install_requires = ['setuptools'],
)
View
13 src/googleanalytics/config.py
@@ -0,0 +1,13 @@
+import ConfigParser
+import os.path
+
+def get_google_credentials():
+ home_directory = os.path.expanduser("~")
+ config_file = os.path.join(home_directory,'.pythongoogleanalytics')
+ if not os.path.exists(config_file):
+ return None, None
+ config = ConfigParser.RawConfigParser()
+ config.read(config_file)
+ google_account_email = config.get('Credentials', 'google_account_email')
+ google_account_password = config.get('Credentials', 'google_account_password')
+ return google_account_email, google_account_password
View
47 src/googleanalytics/connection.py
@@ -1,49 +1,60 @@
from googleanalytics.exception import *
-from googleanalytics import handler
+from googleanalytics import handler, config
import xml.sax
import pprint
import re
import urllib
import urllib2
+DEBUG = False
class GAConnection():
default_host = 'https://www.google.com'
user_agent = 'python-gapi-1.0'
- query_string = {
- 'accountType': 'GOOGLE',
- 'Email': None,
- 'Passwd': None,
- 'service': 'analytics',
- 'source': user_agent,
- }
auth_token = None
- def __init__(self, google_email, google_password):
- self.query_string['Email'] = google_email
- self.query_string['Passwd'] = google_password
-
+ def __init__(self, google_email, google_password):
authtoken_pat = re.compile(r"Auth=(.*)")
path = '/accounts/ClientLogin'
+
+
+ data = "accountType=GOOGLE&Email=%s&Passwd=%s&service=analytics&source=%s"
+ data = data % (google_email, google_password, self.user_agent)
- response = self.make_request(path=path, data=self.query_string)
+ response = self.make_request('POST', path=path, data=data)
auth_token = authtoken_pat.search(response.read())
self.auth_token = auth_token.groups(0)[0]
- def make_request(self, path, headers=None, data=''):
+ def get_accounts(self, start_index=1, max_results=None):
+ path = '/analytics/feeds/accounts/default'
+ data = { 'start-index': start_index,}
+ if max_results:
+ data['max-results'] = max_results
+ data = urllib.urlencode(data)
+ response = self.make_request('GET', path, data=data)
+ print response.read()
+
+ def make_request(self, method, path, headers=None, data=''):
if headers == None:
headers = {
'User-Agent': self.user_agent,
'Authorization': 'GoogleLogin auth=%s' % self.auth_token
}
else:
- headers = headers.copy()
-
- if data != '':
- data = urllib.urlencode(data)
+ headers = headers.copy()
+
if not headers.has_key('Content-Length'):
headers['Content-Length'] = str(len(data))
+
+ if DEBUG:
+ print "** Headers: %s" % (headers,)
+
+ if method == 'GET':
+ path = '%s?%s' % (path, data)
+ data = ''
+
request = urllib2.Request(self.default_host + path, data)
+
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError, e:

0 comments on commit f14c8ed

Please sign in to comment.