Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

song search Python Script for xbmc

Questions and comments specific to a particular plugin should go here.
Post Reply
lokey
Posts: 2
Joined: Tue Nov 11, 2014 5:15 pm

song search Python Script for xbmc

Post by lokey »

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]
Last edited by lokey on Wed Nov 19, 2014 4:30 pm, edited 1 time in total.
jonib
Plugin Developer
Posts: 1328
Joined: Thu Mar 26, 2009 9:33 pm
Location: Sweden

Re: song search Python Script for xbmc

Post by jonib »

Put the code in a code block or at least I won't bother to first fix the formatting so I can check for your error. Python requires indentation to be correct.

jonib
XBMC2 plugin to control XBMC. If you want to flatter me Image
Post Reply