From 85766d465aefacafea132e118986e61593521728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20P=C3=A9chard?= Date: Sun, 7 Apr 2019 19:31:01 +0200 Subject: [PATCH] Update stathat.py to be used with Python3 --- python/stathat.py | 57 ++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/python/stathat.py b/python/stathat.py index 9b126ab..386cf14 100644 --- a/python/stathat.py +++ b/python/stathat.py @@ -1,35 +1,36 @@ -import urllib -import urllib2 +import urllib.error +import urllib.parse +import urllib.request -class StatHat: - def http_post(self, path, data): - pdata = urllib.urlencode(data) - req = urllib2.Request('http://api.stathat.com' + path, pdata) - resp = urllib2.urlopen(req) - return resp.read() +class StatHat: - def post_value(self, user_key, stat_key, value, timestamp=None): - args = {'key': stat_key, 'ukey': user_key, 'value': value} - if timestamp is not None: - args['t'] = timestamp - return self.http_post('/v', args) + def http_post(self, path, data): + pdata = urllib.parse.urlencode(data).encode("utf-8") + req = urllib.request.Request('http://api.stathat.com' + path, pdata) + resp = urllib.request.urlopen(req) + return resp.read() - def post_count(self, user_key, stat_key, count, timestamp=None): - args = {'key': stat_key, 'ukey': user_key, 'count': count} - if timestamp is not None: - args['t'] = timestamp - return self.http_post('/c', args) + def post_value(self, user_key, stat_key, value, timestamp=None): + args = {'key': stat_key, 'ukey': user_key, 'value': value} + if timestamp is not None: + args['t'] = timestamp + return self.http_post('/v', args) - def ez_post_value(self, ezkey, stat_name, value, timestamp=None): - args = {'ezkey': ezkey, 'stat': stat_name, 'value': value} - if timestamp is not None: - args['t'] = timestamp - return self.http_post('/ez', args) + def post_count(self, user_key, stat_key, count, timestamp=None): + args = {'key': stat_key, 'ukey': user_key, 'count': count} + if timestamp is not None: + args['t'] = timestamp + return self.http_post('/c', args) - def ez_post_count(self, ezkey, stat_name, count, timestamp=None): - args = {'ezkey': ezkey, 'stat': stat_name, 'count': count} - if timestamp is not None: - args['t'] = timestamp - return self.http_post('/ez', args) + def ez_post_value(self, ezkey, stat_name, value, timestamp=None): + args = {'ezkey': ezkey, 'stat': stat_name, 'value': value} + if timestamp is not None: + args['t'] = timestamp + return self.http_post('/ez', args) + def ez_post_count(self, ezkey, stat_name, count, timestamp=None): + args = {'ezkey': ezkey, 'stat': stat_name, 'count': count} + if timestamp is not None: + args['t'] = timestamp + return self.http_post('/ez', args)