From 821c29ee73ed1e6aee8881ae7d921bebc6f7a6e3 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 12 Feb 2016 17:48:48 +0300 Subject: refactoring --- api.py | 17 +++++++++++++---- track_status.py | 3 +++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/api.py b/api.py index fb08ca4..4b337ff 100644 --- a/api.py +++ b/api.py @@ -81,11 +81,20 @@ class API: def _lang_is_specified(self): return self.lang != Language.DEFAULT - def _call_method(self, method, **kwargs): - get_args = '&'.join(map(lambda k: '{}={}'.format(k, kwargs[k]), kwargs)) + def _format_method_params(self, **kwargs): + params = '&'.join(map(lambda k: '{}={}'.format(k, kwargs[k]), kwargs)) if self._lang_is_specified(): - get_args += '&lang={}'.format(self.lang.value) - url = 'https://api.vk.com/method/{}?{}'.format(method, get_args) + if params: + params += '&' + params += 'lang={}'.format(self.lang) + return params + + def _build_method_url(self, method, **kwargs): + return 'https://api.vk.com/method/{}?{}'.format( + method, self._format_method_params(**kwargs)) + + def _call_method(self, method, **kwargs): + url = self._build_method_url(method, **kwargs) response = json.loads(urllib.request.urlopen(url).read().decode()) if 'response' not in response: raise self.Error(response) diff --git a/track_status.py b/track_status.py index 2c6776e..3268ab3 100644 --- a/track_status.py +++ b/track_status.py @@ -30,6 +30,9 @@ def format_user_went_online(user): def format_user_went_offline(user): return '{} went OFFLINE'.format(format_user(user)) +def user_is_online(user): + logging.info(format_user_is_online(user)) + def user_is_offline(user): logging.info(format_user_is_offline(user)) logging.info(format_user_last_seen(user)) -- cgit v1.2.3