Skip to content

Instantly share code, notes, and snippets.

@tell-k
Created March 18, 2013 05:26
Show Gist options
  • Save tell-k/5185237 to your computer and use it in GitHub Desktop.
Save tell-k/5185237 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Connpass API Wrapper
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Requirement
- Requests (http://docs.python-requests.org/en/latest/)
See Also
- http://connpass.com/about/api/
"""
import json
import requests
class ConnpassAPI(object):
BASE_API_URL = "http://connpass.com/api/%s/%s"
# can specify multiple parameter
MULTIPLE_PARAMS = ('event_id', 'keyword', 'keyword_or', 'ym',
'ymd', 'nickname', 'owner_nickname', 'series_id')
MULTIPLE_SPLIT_WORD = ","
def __init__(self, version=None, timeout=None):
self._timeout = timeout or 30
self._version = version or "v1"
def __getattr__(self, func):
def inner_func(**kwargs):
req_url = self._get_url(func)
resp = requests.get(req_url, params=self._normalize_params(kwargs), timeout=self._timeout)
return json.loads(resp.text)
return inner_func
def _get_url(self, func):
return self.BASE_API_URL % (self._version, func)
def _normalize_params(self, params):
ret_params = []
for key, value in params.iteritems():
if value == '' or value == None:
continue
if key in self.MULTIPLE_PARAMS and (isinstance(value, tuple) or isinstance(value, list)):
values = [(str(v) if self._is_numeric(v) else v) for v in value]
ret_params.append((key, self.MULTIPLE_SPLIT_WORD.join(values)))
else:
ret_params.append((key, value))
return dict(ret_params)
def _is_numeric(self, obj):
# see http://stackoverflow.com/questions/500328/identifying-numeric-and-array-types-in-numpy
attrs = ['__add__', '__sub__', '__mul__', '__div__', '__pow__']
return all(hasattr(obj, attr) for attr in attrs)
if __name__ == "__main__":
# Sample of Use
connpass = ConnpassAPI()
ret = connpass.event(keyword="Python")
# kewword ----------------------
#ret1 = connpass.event(keyword="Python,ボルダリング", count=100)
#
#ret2 = connpass.event(keyword=["Python", u"ボルダリング"], count=100)
#assert ret1['results_returned'] == ret2['results_returned']
#
#ret3 = connpass.event(keyword=("Python", u"ボルダリング"), count=100)
#assert ret1['results_returned'] == ret3['results_returned']
#
## event_id ----------------------
#ret1 = connpass.event(event_id="2042,1932")
#
#ret2 = connpass.event(event_id=[2042, 1932])
#assert ret1['results_returned'] == ret2['results_returned']
#
#ret3 = connpass.event(event_id=["2042", "1932"])
#assert ret1['results_returned'] == ret3['results_returned']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment