summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Baumann <derflob@derflob.de>2016-07-14 20:12:21 +1200
committerFlorian Baumann <derflob@derflob.de>2016-07-14 20:12:21 +1200
commitc28be82e8aea6cd2434e5bfe03977ec4a02d17ae (patch)
tree06648f52e54a9a4aff6555998ea64eab3381e786
parentae9df80edaf0db2481d8e3272426c9bcc59f573d (diff)
downloadxmpp-http_upload.tar.gz
xmpp-http_upload.tar.bz2
Make HTTP Upload actually workhttp_upload
-rw-r--r--__init__.py76
1 files changed, 44 insertions, 32 deletions
diff --git a/__init__.py b/__init__.py
index 2286005..7a252be 100644
--- a/__init__.py
+++ b/__init__.py
@@ -2,20 +2,26 @@
from __future__ import absolute_import
import io
import requests
-from sleekxmpp import ClientXMPP
+import sleekxmpp
+
from octoprint.plugin import EventHandlerPlugin, SettingsPlugin
class OctoXMPP(EventHandlerPlugin, SettingsPlugin):
+
def initialize(self):
- jid = self._settings.get(["xmpp", "jid"])
- resource = self._settings.get(["xmpp", "resource"])
- passwd = self._settings.get(["xmpp", "password"])
+ jid = self._settings.get(['xmpp', 'jid'])
+ resource = self._settings.get(['xmpp', 'resource'])
+ passwd = self._settings.get(['xmpp', 'password'])
full_jid = sleekxmpp.JID(jid, resource=resource)
- self._xmpp = ClientXMPP(full_jid, passwd)
+ self._xmpp = sleekxmpp.ClientXMPP(full_jid, passwd)
+
+ def __session_start(event):
+ self._xmpp.send_presence()
+ self._join_groupchats()
- self._xmpp.add_event_handler("session_start", self._xmpp.send_presence)
+ self._xmpp.add_event_handler('session_start', __session_start)
self._xmpp.register_plugin('xep_0030') # disco
self._xmpp.register_plugin('xep_0045') # muc
@@ -28,48 +34,54 @@ class OctoXMPP(EventHandlerPlugin, SettingsPlugin):
def get_settings_defaults(self):
return {
'xmpp': {
- jid: "rupert@derflob.de",
- resource: "Rupert",
- password: "KosselMini",
- receivers: [("groupchat", "m3k@conference.m3k.cc")]
+ 'jid': "rupert@m3k.cc",
+ 'resource': "Rupert",
+ 'password': "KosselMini",
+ 'recipients': [("groupchat", "3k@conference.m3k.cc"), ("chat", "derflob@derflob.de")]
}
}
- def _msg_receivers(self, msg, send_image=True):
- for (t, r) in self._settings.get(["xmpp", "receivers"]):
+ def _msg_recipients(self, msg, send_image=True):
+ for (chat_type, recipient) in self._settings.get(['xmpp', 'recipients']):
+ self._xmpp.send_message(mto=recipient, mbody=msg, mtype=chat_type)
- if (t == "groupchat"):
- nick = self._settings.get(["xmpp", "resource"])
- self._xmpp["xep_0045"].joinMUC(r, nick, wait=True)
+ if (send_image):
+ snapshot = self._settings.global_get(['webcam', 'snapshot'])
+ if (snapshot):
+ req = requests.get(snapshot)
- self._xmpp.send_message(mto=r, mbody=msg, mtype=t)
+ if (req.ok):
+ i = self._xmpp['xep_0363'].start_upload(
+ 'rupert.jpg', io.BytesIO(req.content), len(req.content), req.headers.get('content-type', None))
- if (send_image):
- snapshot = self._settings.get(["webcam", "snapshot"])
- if (snapshot):
- r = requests.get(snapshot)
- if (r.ok):
- i = self._xmpp['xep_0363'].start_upload(
- 'rupert.jpg', io.BytesIO(r.content), len(r.content), r.headers.get('content-type', None))
-
- def __send_get_url(get_url):
- m = self._xmpp.make_message(mto=r, mbody=get_url, mtype=t)
+ def __send_get_url(chat_type, recipient):
+ for (chat_type, recipient) in self._settings.get(['xmpp', 'recipients']):
+ m = self._xmpp.make_message(mto=recipient, mbody=get_url, mtype=chat_type)
m['oob']['url'] = get_url
m.send()
- self._xmpp.add_event_handler("http_upload::{}:finished".format(i), __send_get_url)
+ self._xmpp.add_event_handler("http_upload::{}::finished".format(i),
+ __send_get_url, disposable=True)
+
+ def _join_groupchats(self):
+ nick = self._settings.get(['xmpp', 'resource'])
+ for (chat_type, recipient) in self._settings.get(['xmpp', 'recipients']):
+ if (chat_type = "groupchat"):
+ self._xmpp['xep_0045'].joinMUC(recipient, nick)
def on_event(self, event, data):
if (event == "PrintDone"):
- self._msg_receivers("Zack, Feditsch!")
+ self._msg_recipients("Zack, Feditsch!")
elif (event == "PrintFailed"):
- self._msg_receivers("Da ging was schief!")
+ self._msg_recipients("Da ging was schief!")
elif (event == "PrintPaused"):
- self._msg_receivers("Druck pausiert.", False)
+ self._msg_recipients("Druck pausiert.", False)
elif (event == "PrintResumed"):
- self._msg_receivers("Druck resümiert.", False)
+ self._msg_recipients("Druck resümiert.", False)
elif (event == "PrintCancelled"):
- self._msg_receivers("Druck abgebrochen!")
+ self._msg_recipients("Druck abgebrochen!")
+ elif (event == "Connected"):
+ self._msg_recipients("Connected to Rupert.")
__plugin_name__ = "OctoXMPP"
__plugin_version__ = "0.2.0"