Skip to content

Instantly share code, notes, and snippets.

@matbor
Last active August 29, 2015 14:15
Show Gist options
  • Save matbor/17e69db89855a953bffc to your computer and use it in GitHub Desktop.
Save matbor/17e69db89855a953bffc to your computer and use it in GitHub Desktop.
A quick python script using Sickbeard api to get what TV has downloaded today and format for mqttwarn (for mqtt message/topic). I run this via cron at 7pm (when i sit down for the night, hopefully!) and i can quickly see what to watch. If you are very clever you could have this delivered to your phone when you turn your TV on at night. (aka open…
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import urllib2
import time
import datetime
import paho.mqtt.publish as publish
# Show's the last 10 episodes that have been downloaded using sickbeard recently
# working example; http://localhost:9002/api/xxxxxxxxxxxxxxxxxxxxxxxxxxx/?cmd=history&limit=10&type=downloaded
# Edit Settings
ip = "192.168.4.22:8081"
api = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
cmd = "history&limit=10&type=downloaded"
#mqtt broker settings
hostname = "127.0.0.1"
topic = "/house/messages"
#End Settings
# make url
sb = "http://%s/api/%s/?cmd=%s" % (ip, api, cmd)
response = urllib2.urlopen(sb)
jsonResponse = json.loads(response.read())
jsonData = jsonResponse["data"]
listofshows = ""
for item in jsonData:
show_name = item.get("show_name")
date_object = datetime.datetime.strptime(item.get("date"), '%Y-%m-%d %H:%M')
if date_object.timetuple().tm_mday == datetime.date.today().day:
#print("%s" % (show_name))
listofshows = show_name + "\n" + listofshows #add one per line
print(listofshows)
#format for mqttwarn message delivery
msgjson = json.dumps(dict(
lvl = "4",
sub = "NEW TV to Catch up on tonight",
txt = listofshows[:-1].encode('utf8'),
delay = "20000",
img = "info.png",
url = "http://" + ip
))
#print("JSON msg: %s" % (msgjson))
publish.single(topic, msgjson, hostname=hostname)
@sumnerboy12
Copy link

Great idea @matbor - what config are you using in mqttwarn for these messages?

@sumnerboy12
Copy link

Here is my openHAB rule for triggering the display;

rule "Living room AV activity"
when
    // 0="Off", 1="XBMC", 2="Blu-Ray", 3="Music", 4="AUX", 5="Sky"
    Item AvActivity_Living received command
then
    switch (receivedCommand) {
        case 1: {            
            // if it is in the evening (i.e. after 9pm) then reduce volume a bit
            var Number volumeDb = -30.0
            if (DayEvening.state == ON)
                volumeDb = -35.0

            YamahaMainZone_Power.sendCommand(ON)
            YamahaMainZone_Input.sendCommand("AV3")
            YamahaMainZone_VolumeDb.sendCommand(volumeDb)
            RedEyeCommand_TvPower.sendCommand(ON)
            RedEyeCommand_BlurayPower.sendCommand(OFF)

            if (AvActivity_Outside.state != 3)
                SqueezeboxLiving_Power.sendCommand(OFF)             

            // scene: normal (we adjust based on XBMC state - play/pause/stop)
            if (DayMorning.state != ON)
                Scene_Living.sendCommand(1)

            // clear the shutdown timer since there has been some activity
            if (xbmcLivingShutdownTimer != null)
                xbmcLivingShutdownTimer.cancel()

            // start a timer to shutdown the AV gear
            xbmcLivingShutdownTimer = createTimer(now.plusMinutes(10)) [|
                // check we are still in the XBMC activity
                if (AvActivity_Living.state == 1) {
                    Notify_Trace.postUpdate("Shutting down living room XBMC after 10 mins of no activity")
                    AvActivity_Living.sendCommand(0)
                }
            ]

            // after 10s get our script to publish any shows downloaded today so they are displayed
            createTimer(now.plusSeconds(10)) [|
                var String command = "/home/ben/openhab/publish_sickbeard_daily.py"
                executeCommandLine(command, 10000)
            ]
        }
    }
end

@sumnerboy12
Copy link

I have removed the code for the other scenes as it was a bit too much.

@matbor
Copy link
Author

matbor commented Feb 11, 2015

mqttwarn.ini settings;

[messages]
topic = /house/messages
targets = log:debug,pushover:message
title = {sub}
format = {txt}

also compatible with my mqtt2chrome notifications popups with this setup.

@sumnerboy12
Copy link

BTW - the wife loves this new 'feature'. Every time she fires up XBMC she gets the little notification listing the shows are have downloaded. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment