diff --git a/abakaffe/lib.py b/abakaffe/lib.py index 791498b..b6612f9 100644 --- a/abakaffe/lib.py +++ b/abakaffe/lib.py @@ -1,9 +1,13 @@ -# -*- coding: latin-1 -*- -import urllib2 +# encoding: utf-8 +from __future__ import unicode_literals +from future.standard_library import install_aliases +install_aliases() +from urllib.parse import urljoin +from urllib.request import urlopen, Request +from urllib.error import URLError import simplejson -from urlparse import urljoin from datetime import datetime -from version import __version__ +from .version import __version__ class Abakaffe(): @@ -32,15 +36,15 @@ def get_file(api_base, api_module): Returns a file object of the server response. ''' url = urljoin(api_base, api_module) - headers = {'User-Agent': 'abakaffe-cli'} - req = urllib2.Request(url, headers=headers) - opener = urllib2.build_opener() + req = Request(url, headers={'User-Agent' : "Magic Browser"}) try: - f = opener.open(req) - except IOError: - print('Kunne ikke koble til %s' % url) - exit(0) - return f + con = urlopen(req) + data = con.read() + except URLError: + print("Could not read data from {}".format(url)) + exit() + return data + @staticmethod def get_status(time_delta, organization="Abakus"): @@ -59,8 +63,7 @@ def get_status(time_delta, organization="Abakus"): return "Kaffen til {organization} ble nettopp traktet! \ LØØØP!!!".format(organization=organization) - message += "Kaffen til {organization} ble sist traktet for " - message = message.format(organization=organization) + message += "Kaffen til {organization} ble sist traktet for ".format(organization=organization) if hours: if hours == 1: message += "én time" @@ -84,7 +87,7 @@ def abakus(args=None): ''' message = "" f = Abakaffe.get_file(Abakaffe.ABA_API_URL, 'status') - status_json = simplejson.load(f) + status_json = simplejson.loads(f) coffee = status_json['coffee'] on = coffee['status'] last_start = coffee['last_start'] @@ -108,19 +111,16 @@ def abakus_stats(): ''' message = "" f = Abakaffe.get_file(Abakaffe.ABA_API_URL, 'stats') - stats_json = simplejson.load(f) + stats_json = simplejson.loads(f) stats = stats_json['stats'] - for date in sorted(stats.keys())[-5:]: + for date in sorted(stats.keys()): value = int(stats[date]) - message += u"{date} {graph} {value}\n".format( - date=date, - graph=value * u"\u2588", - value=value) - message = message.rstrip() # Remove trailing whitespace + message += "%s %s %s \n" % (date, value * u"\u2588", value) return message @staticmethod def online(): + raise NotImplementedError ''' Returns a message with info from the Online coffee API, total pots brewed today, and last time brewed. @@ -128,13 +128,12 @@ def online(): message = "" f = Abakaffe.get_file(Abakaffe.ONLINE_API_URL, "coffee.txt") total_today = int(f.readline()) + if total_today > 0: + message += "Online har traktet %s kanner i dag.\n" % total_today + else: + message += "Online har ikke traktet kaffe i dag.\n" last_start = f.readline() last_start = datetime.strptime(last_start, "%d. %B %Y %H:%M:%S") - if last_start.date() == datetime.today().date() and total_today > 0: - message += "Online har traktet {count} kanner i dag.\n".format( - count=total_today) - time_delta = datetime.now() - last_start - message += Abakaffe.get_status(time_delta, "Online") - else: - message += "Online har ikke traktet kaffe i dag." + time_delta = datetime.now() - last_start + message += Abakaffe.get_status(time_delta, "Online") return message diff --git a/abakaffe/script.py b/abakaffe/script.py index cb75926..6c42f47 100644 --- a/abakaffe/script.py +++ b/abakaffe/script.py @@ -1,8 +1,12 @@ #!/usr/bin/python # -*- coding: latin-1 -*- from __future__ import print_function +from future.standard_library import install_aliases + +install_aliases() + from argparse import ArgumentParser -from lib import Abakaffe +from .lib import Abakaffe def get_args(): diff --git a/abakaffe/version.py b/abakaffe/version.py index d582a4d..f8223c3 100644 --- a/abakaffe/version.py +++ b/abakaffe/version.py @@ -2,4 +2,4 @@ # Program version number is declared here. # -__version__ = '0.2.6' +__version__ = '0.2.7' diff --git a/dist/abakaffe_cli-0.2.7-py2.7.egg b/dist/abakaffe_cli-0.2.7-py2.7.egg new file mode 100644 index 0000000..a58cd13 Binary files /dev/null and b/dist/abakaffe_cli-0.2.7-py2.7.egg differ diff --git a/dist/abakaffe_cli-0.2.7-py3.5.egg b/dist/abakaffe_cli-0.2.7-py3.5.egg new file mode 100644 index 0000000..23e39b0 Binary files /dev/null and b/dist/abakaffe_cli-0.2.7-py3.5.egg differ diff --git a/requirements.txt b/requirements.txt index 57c93d7..eec6f9d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ --e git+https://github.com/oyvindrobertsen/abakaffe-cli.git@1193ecf05b311133e8347431e4932ba7c7c83255#egg=abakaffe_cli-refactor nose==1.3.0 simplejson==3.3.1 -wsgiref==0.1.2 +future==0.15.2 \ No newline at end of file diff --git a/setup.py b/setup.py index f40dcfc..7db75c4 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ author='Øyvind Robertsen, Martin Hallén', author_email=['oyvindrobertsen@gmail.com', 'marthall@outlook.com'], url='http://github.com/oyvindrobertsen/abakaffe-cli', - install_requires=['simplejson', 'nose'], + install_requires=['simplejson', 'nose', 'future'], license='MIT', keywords='Abakus coffee API', )