Since I'm touching on a few things Octoprint this week I thought I'd also post my method of getting regular updates from my printer when it's printing.

At the time of posting I'm currently writing an update to the Octoprint plugin to do this all internally, but in the meantime this will have to do.

EDIT:

Pull request merged! You should use the plugin instead of the script below.


Cron this up as often as you'd like to get notifications, I personally chose once an hour.

It doesn't send notifications unless the printer is printing.

import json
import urllib

from pushover import Client
from urllib2 import Request, urlopen


def octoprint(path):
    q = Request("http://10.0.0.60/api/{}".format(path))
    q.add_header("X-Api-Key", "[OCTOPRINT API KEY]")
    a = urlopen(q).read()

    return json.loads(a)


def sendSnapshot():
    client = Client("[PUSHOVER CLIENT KEY]", api_token="[PUSHOVER API TOKEN]")
    urllib.urlretrieve("http://10.0.0.60/webcam/?action=snapshot", "/tmp/snapshop.jpg")

    job = octoprint("job")

    message = "{}% Complete".format(round(job['progress']['completion'], 2))

    with open("/tmp/snapshop.jpg", "rb") as image:
        client.send_message(message, attachment=image)


if __name__ == "__main__":

    printer = octoprint("printer?history=false&limit=0")

    if printer["state"]["text"] == "Printing":
        sendSnapshot()