Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 28 additions & 29 deletions abakaffe/lib.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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"):
Expand All @@ -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"
Expand All @@ -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']
Expand All @@ -108,33 +111,29 @@ 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.
'''
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
6 changes: 5 additions & 1 deletion abakaffe/script.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
2 changes: 1 addition & 1 deletion abakaffe/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# Program version number is declared here.
#

__version__ = '0.2.6'
__version__ = '0.2.7'
Binary file added dist/abakaffe_cli-0.2.7-py2.7.egg
Binary file not shown.
Binary file added dist/abakaffe_cli-0.2.7-py3.5.egg
Binary file not shown.
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
-e git+https://github.com/oyvindrobertsen/abakaffe-cli.git@1193ecf05b311133e8347431e4932ba7c7c83255#egg=abakaffe_cli-refactor
nose==1.3.0
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oyvindrobertsen Hva ble denne egentlig brukt til, fucket opp ting litt hos meg.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usikker, hva skjer om du fjerner den?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Det er du som har lagt den til for lenge siden. Den la en kopi av koden i src/ som var fra refactor-branchen eller noe, og pip laget noen klage-filer. Men tror det skal funke fint uten.

simplejson==3.3.1
wsgiref==0.1.2
future==0.15.2
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)