Last active
November 24, 2016 20:00
-
-
Save marxjohnson/32ec4252755bea84a6d6 to your computer and use it in GitHub Desktop.
Post-record shutdown script for TVheadEnd/XBMC PVR
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
#!/bin/bash | |
ruby /usr/local/bin/post-record.rb >> /home/xbmc/wakeup.log 2>&1 |
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
require 'net/http' | |
require 'json' | |
uri = URI 'http://localhost:8080/jsonrpc' | |
jsonrpc = Net::HTTP::Post.new uri | |
jsonrpc.add_field 'Content-Type', 'application/json' | |
jsonrpc.add_field 'Accept', 'application/json' | |
get_active_players = '{"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}' | |
shutdown = '{"jsonrpc": "2.0", "method": "System.Shutdown", "id": 1}' | |
begin | |
activeplayers = nil | |
Net::HTTP.start uri.host, uri.port do |http| | |
jsonrpc.body = get_active_players | |
activeplayers = JSON.parse http.request(jsonrpc).body | |
end | |
logged_in = `users`.split | |
current_user = `whoami`.strip | |
xbmc_user = "xbmc" | |
browser = `ps ax | grep chrome | grep -v grep` | |
logged_in.delete current_user | |
logged_in.delete xbmc_user | |
if activeplayers["result"].length > 0 | |
puts "Kodi Player Active, not shutting down" | |
exit | |
end | |
if !browser.empty? | |
puts "Browser active, not shutting down" | |
exit | |
end | |
if logged_in.length > 0 | |
puts "Users logged in, not shutting down" | |
end | |
sleep 60 | |
Net::HTTP.start uri.host, uri.port do |http| | |
jsonrpc.body = shutdown | |
http.request(jsonrpc) | |
end | |
rescue Timeout::Error => e | |
puts "Timeout, not shutting down" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment