Skip to content

Instantly share code, notes, and snippets.

@N3MIS15
Created October 31, 2012 05:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save N3MIS15/3985121 to your computer and use it in GitHub Desktop.
Save N3MIS15/3985121 to your computer and use it in GitHub Desktop.
xbmc script
#!/usr/bin/env python
settings = {
'xbmc_hostname': '192.168.1.111',
'xbmc_port': '8080',
'xbmc_username': 'xbmc',
'xbmc_password': 'xbmc',
'xbmc_mac_address': '00:00:00:00:00:00',
'maraschino_url': 'http://127.0.0.1:7000/maraschino',
'maraschino_user': '',
'maraschino_pass': ''
}
xbmc_address = 'http://%s:%s/jsonrpc' % (settings['xbmc_hostname'], settings['xbmc_port'])
try:
import json
except ImportError:
import simplejson as json
import urllib2, base64, time, socket, struct
#Timeout
socket.setdefaulttimeout(10)
#XBMC JSON handler
class XBMCJSON:
def __init__(self, server):
self.server = server
self.version = '2.0'
def __call__(self, **kwargs):
method = '.'.join(map(str, self.n))
self.n = []
return XBMCJSON.__dict__['Request'](self, method, kwargs)
def __getattr__(self,name):
if not self.__dict__.has_key('n'):
self.n=[]
self.n.append(name)
return self
def Request(self, method, kwargs):
data = [{}]
data[0]['method'] = method
data[0]['params'] = kwargs
data[0]['jsonrpc'] = self.version
data[0]['id'] = 1
data = json.JSONEncoder().encode(data)
content_length = len(data)
content = {
'Content-Type': 'application/json',
'Content-Length': content_length,
}
try:
request = urllib2.Request(self.server, data, content)
base64string = base64.encodestring('%s:%s' % (settings['xbmc_username'], settings['xbmc_password'])).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
f = urllib2.urlopen(request)
response = f.read()
f.close()
response = json.JSONDecoder().decode(response)
except:
return False
try:
return response[0]['result']
except:
return response[0]['error']
xbmc = XBMCJSON(xbmc_address)
def wake_htpc():
mac_address = settings['xbmc_mac_address']
addr_byte = mac_address.split(':')
hw_addr = struct.pack('BBBBBB',
int(addr_byte[0], 16),
int(addr_byte[1], 16),
int(addr_byte[2], 16),
int(addr_byte[3], 16),
int(addr_byte[4], 16),
int(addr_byte[5], 16)
)
#Send wakeup packet
msg = '\xff' * 6 + hw_addr * 16
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto(msg, ("255.255.255.255", 9))
#Ping XBMC to see if its awake
if xbmc.JSONRPC.Ping() != 'pong':
wake_htpc()
#while booting ping XBMC api every 20 seconds until it responds
while xbmc.JSONRPC.Ping() != 'pong':
time.sleep(20)
def xbmc_is_scanning():
return xbmc.XBMC.GetInfoBooleans(booleans=['Library.IsScanningVideo'])['Library.IsScanningVideo']
if not xbmc_is_scanning():
#Send scan request
xbmc.VideoLibrary.Scan()
#check to see if XBMC is still scanning
while xbmc_is_scanning():
time.sleep(30)
def open_url(url, username, password):
request = urllib2.Request(url)
if username and password:
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
urllib2.urlopen(request)
#Poll Maraschino's recently added modules
#Episodes
open_url(
settings['maraschino_url'] + '/xhr/recently_added/',
settings['maraschino_user'],
settings['maraschino_user']
)
#Give Maraschino some time to do its thing
time.sleep(10)
#Movies
open_url(
settings['maraschino_url'] + '/xhr/recently_added_movies/',
settings['maraschino_user'],
settings['maraschino_user']
)
#Give Maraschino some time to do its thing
time.sleep(10)
#Shutdown XBMC
xbmc.System.Shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment