From a609fdf9acf0a63f3e2fceba74b719b472d5384a Mon Sep 17 00:00:00 2001 From: Jurkov Date: Tue, 22 Nov 2016 20:16:39 +0100 Subject: [PATCH 1/3] adding new plugin for betterplace --- bytebot_config.py.example | 3 +++ plugins/betterplace.py | 52 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 plugins/betterplace.py diff --git a/bytebot_config.py.example b/bytebot_config.py.example index 5e42921..8efddf9 100644 --- a/bytebot_config.py.example +++ b/bytebot_config.py.example @@ -100,4 +100,7 @@ BYTEBOT_PLUGIN_CONFIG = { 'url': 'https://de.wikipedia.org/w/api.php?&action=query&format=json&prop=extracts&exintro=&explaintext=&titles=', 'length_of_abstract': 400 }, + 'betterplace': { + 'id': 24759, + }, } diff --git a/plugins/betterplace.py b/plugins/betterplace.py new file mode 100644 index 0000000..4cc8a59 --- /dev/null +++ b/plugins/betterplace.py @@ -0,0 +1,52 @@ +from irc3.plugins.command import command + +from bytebot_config import BYTEBOT_PLUGIN_CONFIG +from irc3 import asyncio +import aiohttp +import json + + +@command(permission="view") +@asyncio.coroutine +def betterplace(bot, mask, target, args): + """Show the current betterplace projects + + %%betterplace + """ + config = BYTEBOT_PLUGIN_CONFIG['betterplace'] + + if config['id'] == "betterplace_id": + return "I don't have your betterplace id!" + + bot.privmsg(target, 'betterplace projects:') + + try: + url = "https://api.betterplace.org/de/api_v4/organisations/" + \ + str(config['id']) + \ + "/projects.json" + + with aiohttp.Timeout(10): + with aiohttp.ClientSession(loop=bot.loop) as session: + resp = yield from session.get(url) + if resp.status != 200: + bot.privmsg( + target, "Error while retrieving " + __name__ + " data") + raise Exception() + r = yield from resp.read() + + projects = json.loads(r.decode('utf-8')) + + for d in projects["data"]: + if(d["closed_at"] == None): + bot.privmsg(target, + " {name:35} {got:3}€ von {want:3}€ gespendet".format( + name=d["title"], + got=int( + d["donated_amount_in_cents"]) / 100, + want=int(d["donated_amount_in_cents"] + + d["open_amount_in_cents"]) / 100 + )) + + except KeyError: + bot.privmsg(target, "Error while retrieving " + __name__ + " data") + raise Exception() From 6842523a86c2f8e957ea5365a9368b265dde6624 Mon Sep 17 00:00:00 2001 From: Jurkov Date: Tue, 22 Nov 2016 20:19:32 +0100 Subject: [PATCH 2/3] pep8fy the code --- plugins/betterplace.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/betterplace.py b/plugins/betterplace.py index 4cc8a59..e8a1ab9 100644 --- a/plugins/betterplace.py +++ b/plugins/betterplace.py @@ -39,7 +39,8 @@ def betterplace(bot, mask, target, args): for d in projects["data"]: if(d["closed_at"] == None): bot.privmsg(target, - " {name:35} {got:3}€ von {want:3}€ gespendet".format( + " {name:35}" + + " {got:3}€ von {want:3}€ gespendet".format( name=d["title"], got=int( d["donated_amount_in_cents"]) / 100, From 5c059f96a9a9b9c2ca7c90415a983228c415659f Mon Sep 17 00:00:00 2001 From: Jurkov Date: Tue, 22 Nov 2016 20:21:47 +0100 Subject: [PATCH 3/3] pep8fy one more time --- plugins/betterplace.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/betterplace.py b/plugins/betterplace.py index e8a1ab9..9a258ed 100644 --- a/plugins/betterplace.py +++ b/plugins/betterplace.py @@ -37,7 +37,7 @@ def betterplace(bot, mask, target, args): projects = json.loads(r.decode('utf-8')) for d in projects["data"]: - if(d["closed_at"] == None): + if(d["closed_at"] is None): bot.privmsg(target, " {name:35}" + " {got:3}€ von {want:3}€ gespendet".format(