Skip to content

Instantly share code, notes, and snippets.

@marxjohnson
Last active November 24, 2016 19:58
Show Gist options
  • Save marxjohnson/11356234 to your computer and use it in GitHub Desktop.
Save marxjohnson/11356234 to your computer and use it in GitHub Desktop.
XBMC/Tvheadend post-record shutdown script
#!/usr/bin/env ruby
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