song search Python Script for xbmc
Posted: Wed Nov 19, 2014 1:50 am
can anybody tell me whats wrong keep getting this error : SyntaxError: invalid syntax (168, line 76)
Code: Select all
[code][code]import urllib, urllib2
import json
from collections import namedtuple
import re
class JsonObject:
def jsonable(self):
return self.__dict__
class JsonRpc(JsonObject):
def __init__(self, id, method):
self.jsonrpc="2.0"
self.id = id
self.method = method
class JsonFilter(JsonObject):
def __init__(self, field, operator, value):
self.field = field
self.operator = operator
self.value = value
def ComplexHandler(Obj):
if hasattr(Obj, 'jsonable'):
return Obj.jsonable()
else:
raise TypeError, 'Object of type %s with value of %s is not JSON serializable' % (type(Obj), repr(Obj))
url = 'http://localhost:8080/jsonrpc'
title = None
artist = eg.event.payload.arcomm[1]
year = None
genre = None
album = None
sort = None
sortorder = None
play = "true"
doc=JsonRpc("dynamicQuery", "AudioLibrary."+"GetSongs")
doc.params = JsonObject()
doc.params.filter = JsonObject()
doc.params.filter.jsonable()['and'] = []
if title is not None:
doc.params.filter.jsonable()['and'].append(JsonFilter("title", "contains", title))
if artist is not None:
doc.params.filter.jsonable()['and'].append(JsonFilter("artist", "contains", artist))
if year is not None:
doc.params.filter.jsonable()['and'].append(JsonFilter("year", "is", year))
if genre is not None:
doc.params.filter.jsonable()['and'].append(JsonFilter("genre", "contains", genre))
if album is not None:
doc.params.filter.jsonable()['and'].append(JsonFilter("album", "contains", album))
#sorting
if sort is not None:
doc.params.sort = JsonObject()
doc.params.sort.method = sort
doc.params.sort.order = sortorder
jsonForRequest = json.dumps(doc, default=ComplexHandler)
#print jsonForRequest
reqQuery = urllib2.Request(url, data=jsonForRequest, headers={'Content-type': 'application/json'})
resultQuery = urllib2.urlopen(reqQuery).read()
resultObject = json.loads(resultQuery, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
SearchResults = str(resultObject)
SearchResults = re.sub("u'","",SearchResults)
SearchResults = re.sub('[[]',"",SearchResults)
SearchResults = re.sub('[]]',"",SearchResults)
SearchResults = re.sub('[()]',"",SearchResults)
SearchResults = re.sub("[']","",SearchResults)
SearchResults = re.sub("Xsongid","songid",SearchResults)
SearchResults = re.sub("label=","",SearchResults)
SearchResults = re.sub("Xjsonrpc=2.0, id=dynamicQuery, result=Xlimits=Xstart=0, ","",SearchResults)
NumberSongs = SearchResults.split(",")[0]
NumberSongs = re.sub("total=","",NumberSongs)
firstResult = resultObject.result.songs[0]
firstResultId = firstResult.songid
Playing = firstResult.label + "..."
jsonPlaySongById='{"jsonrpc": "2.0", "id": 2, "method": "Player.Open", "params": {"item": {"songid": ' + str(firstResultId) + '}, "options" : {"resume":true}}}'
reqPlaySong = urllib2.Request(url, data=jsonPlaySongById, headers={'Content-type': 'application/json'})
resultPlaySong = urllib2.urlopen(reqPlaySong).read()[code]