Created
November 25, 2016 11:28
-
-
Save jomat/91e4258f0bb6faf1ba4e7d0641caf90e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
# Copyright (c) 2016, Johannes Matheis | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# | |
# * Redistributions of source code must retain the above copyright notice, | |
# this list of conditions, and the following disclaimer. | |
# * Redistributions in binary form must reproduce the above copyright notice, | |
# this list of conditions, and the following disclaimer in the | |
# documentation and/or other materials provided with the distribution. | |
# * Neither the name of the author of this software nor the name of | |
# contributors to this software may be used to endorse or promote products | |
# derived from this software without specific prior written consent. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
# POSSIBILITY OF SUCH DAMAGE. | |
### | |
import supybot.utils as utils | |
from supybot.commands import * | |
import supybot.plugins as plugins | |
import supybot.ircutils as ircutils | |
import supybot.callbacks as callbacks | |
import supybot.ircmsgs as ircmsgs | |
import sys | |
if sys.version_info.major == 3: | |
from urllib.request import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, Request, build_opener | |
from urllib.parse import urlencode | |
else: | |
from urllib2 import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, Request, build_opener | |
from urllib import urlencode | |
def curl(url, params=None, auth=None, req_type="GET", data=None, headers=None): | |
post_req = ["POST", "PUT"] | |
get_req = ["GET", "DELETE"] | |
if params is not None: | |
url += "?" + urlencode(params) | |
if req_type not in post_req + get_req: | |
raise IOError("Wrong request type \"%s\" passed" % req_type) | |
_headers = {} | |
handler_chain = [] | |
if auth is not None: | |
manager = HTTPPasswordMgrWithDefaultRealm() | |
manager.add_password(None, url, auth["user"], auth["pass"]) | |
handler_chain.append(HTTPBasicAuthHandler(manager)) | |
if req_type in post_req and data is not None: | |
_headers["Content-Length"] = len(data) | |
if headers is not None: | |
_headers.update(headers) | |
director = build_opener(*handler_chain) | |
if req_type in post_req: | |
if sys.version_info.major == 3: | |
_data = bytes(data, encoding='utf8') | |
else: | |
_data = bytes(data) | |
req = Request(url, headers=_headers, data=_data) | |
else: | |
req = Request(url, headers=_headers) | |
req.get_method = lambda: req_type | |
result = director.open(req) | |
return { | |
"httpcode": result.code, | |
"headers": result.info(), | |
"content": result.read() | |
} | |
url='https://spaceapi.sbg.chaostreff.at/sensor/set' | |
try: | |
from supybot.i18n import PluginInternationalization | |
_ = PluginInternationalization('SpaceAPI') | |
except ImportError: | |
# Placeholder that allows to run the plugin on a bot | |
# without the i18n module | |
_ = lambda x: x | |
class SpaceAPI(callbacks.Plugin): | |
"""Sets SpaceAPI sensors and channel topics""" | |
pass | |
def up(self, irc, msg, args, channel): | |
"""takes no arguments | |
Sets space status up | |
""" | |
curl('https://spaceapi.sbg.chaostreff.at/sensor/set',data='sensors=%7B%22state%22%3A%7B%22open%22%3Atrue%7D%7D&key=redacted',req_type="POST") | |
topics = irc.state.getTopic(channel).split("|") | |
topics[0] = "Space up " | |
irc.queueMsg(ircmsgs.topic(channel, "|".join(topics))) | |
up = wrap(up,['channel']) | |
def down(self, irc, msg, args, channel): | |
"""takes no arguments | |
Sets space status down | |
""" | |
curl('https://spaceapi.sbg.chaostreff.at/sensor/set',data='sensors=%7B%22state%22%3A%7B%22open%22%3Afalse%7D%7D&key=redacted',req_type="POST") | |
topic = irc.state.getTopic(channel) | |
topics=topic.split("|") | |
topics[0] = "Space zu " | |
topic = "|".join(topics) | |
irc.queueMsg(ircmsgs.topic(channel, topic)) | |
down = wrap(down,['channel']) | |
Class = SpaceAPI | |
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment