Permalink
Browse files

expand endpoints for user info

  • Loading branch information...
1 parent 92dcde4 commit 65d3da4b4dba64af561af6d55eb5b6b6c809ce76 @jehiah jehiah committed Oct 1, 2011
Showing with 71 additions and 27 deletions.
  1. +70 −26 bitly_api/bitly_api.py
  2. +1 −1 setup.py
View
@@ -141,49 +141,66 @@ def clicks_by_minute(self, hash=None, shortUrl=None):
def link_clicks(self, link, **kwargs):
params = dict(link=link)
- data = self._call_oauth2_metrics("v3/link/clicks", params)
+ data = self._call_oauth2_metrics("v3/link/clicks", params, **kwargs)
return data["link_clicks"]
def link_referring_domains(self, link, **kwargs):
params = dict(link=link)
- data = self._call_oauth2_metrics("v3/link/referring_domains", params)
+ data = self._call_oauth2_metrics("v3/link/referring_domains", params, **kwargs)
return data["referring_domains"]
def link_referrers(self, link, **kwargs):
params = dict(link=link)
- data = self._call_oauth2_metrics("v3/link/referrers", params)
+ data = self._call_oauth2_metrics("v3/link/referrers", params, **kwargs)
return data["referrers"]
def link_countries(self, link, **kwargs):
params = dict(link=link)
- data = self._call_oauth2_metrics("v3/link/countries", params)
+ data = self._call_oauth2_metrics("v3/link/countries", params, **kwargs)
return data["countries"]
- def _call_oauth2_metrics(self, endpoint, params, unit=None, units=None, tz_offset=None, rollup=None, limit=None):
- assert self.access_token, "This %s endpoint requires OAuth" % endpoint
- if unit is not None:
- assert unit in ("minute", "hour", "day", "week", "mweek", "month")
- params["unit"] = unit
- if units is not None:
- assert isinstance(units, int), "Unit (%r) must be integer" % units
- params["units"] = units
- if tz_offset is not None:
- # tz_offset can either be a hour offset, or a timezone like North_America/New_York
- if isinstance(tz_offset, int):
- assert -12 <= tz_offset <= 12, "integer tz_offset must be between -12 and 12"
- else:
- assert isinstance(tz_offset, (str, unicode))
- params["tz_offset"] = tz_offset
- if rollup is not None:
- assert isinstance(rollup, bool)
- params["rollup"] = "true" if rollup else "false"
+ def user_popular_links(self, **kwargs):
+ data = self._call_oauth2_metrics("v3/user/popular_links", dict(), **kwargs)
+ return data["popular_links"]
+
+ def user_shorten_counts(self, **kwargs):
+ data = self._call_oauth2_metrics("v3/user/shorten_counts", dict(), **kwargs)
+ return data["shorten_counts"]
+
+ def user_tracking_domain_list(self):
+ data = self._call_oauth2("v3/user/tracking_domain_list", dict())
+ return data["tracking_domains"]
+
+ def user_tracking_domain_clicks(self, domain, **kwargs):
+ params = dict(domain=domain)
+ data = self._call_oauth2_metrics("v3/user/tracking_domain_clicks", params, **kwargs)
+ return data["tracking_domain_clicks"]
+
+ def user_tracking_domain_shorten_counts(self, domain, **kwargs):
+ params = dict(domain=domain)
+ data = self._call_oauth2_metrics("v3/user/tracking_domain_shorten_counts", params, **kwargs)
+ return data["tracking_domain_shorten_counts"]
+
+ def user_link_history(self, created_before=None, created_after=None, archived=None, limit=None, offset=None):
+ params = dict()
+ if created_before is not None:
+ assert isinstance(limit, int)
+ params["created_before"] = created_before
+ if created_after is not None:
+ assert isinstance(limit, int)
+ params["created_after"] = created_after
+ if archived is not None:
+ assert isinstance(archived, bool)
+ params["archived"] = "true" if archived else "false"
if limit is not None:
assert isinstance(limit, int)
params["limit"] = str(limit)
-
- return self._call(self.host, endpoint, params)["data"]
-
-
+ if offset is not None:
+ assert isinstance(offset, int)
+ params["offset"] = str(offset)
+ data = self._call_oauth2("v3/user/link_history", params)
+ return data["link_history"]
+
def info(self, hash=None, shortUrl=None):
""" return the page title for a given bit.ly link """
if not hash and not shortUrl:
@@ -238,6 +255,33 @@ def _generateSignature(self, params, secret):
hash_string += secret
signature = hashlib.md5(hash_string).hexdigest()[:10]
return signature
+
+ def _call_oauth2_metrics(self, endpoint, params, unit=None, units=None, tz_offset=None, rollup=None, limit=None):
+ if unit is not None:
+ assert unit in ("minute", "hour", "day", "week", "mweek", "month")
+ params["unit"] = unit
+ if units is not None:
+ assert isinstance(units, int), "Unit (%r) must be integer" % units
+ params["units"] = units
+ if tz_offset is not None:
+ # tz_offset can either be a hour offset, or a timezone like North_America/New_York
+ if isinstance(tz_offset, int):
+ assert -12 <= tz_offset <= 12, "integer tz_offset must be between -12 and 12"
+ else:
+ assert isinstance(tz_offset, (str, unicode))
+ params["tz_offset"] = tz_offset
+ if rollup is not None:
+ assert isinstance(rollup, bool)
+ params["rollup"] = "true" if rollup else "false"
+ if limit is not None:
+ assert isinstance(limit, int)
+ params["limit"] = str(limit)
+
+ return self._call_oauth2(endpoint, params)
+
+ def _call_oauth2(self, endpoint, params):
+ assert self.access_token, "This %s endpoint requires OAuth" % endpoint
+ return self._call(self.host, endpoint, params)["data"]
def _call(self, host, method, params, secret=None, timeout=5000):
params['format'] = params.get('format', 'json') # default to json
View
@@ -1,6 +1,6 @@
from setuptools import setup
-version = '0.1.c'
+version = '0.2.0'
setup(name='bitly_api',
version=version,

0 comments on commit 65d3da4

Please sign in to comment.