Skip to content
Browse files

Merge pull request #431 from cancan101/master

Fixes to HTTP API querying XBMC Movie Database
  • Loading branch information...
2 parents 6770922 + 033f625 commit f15d7bffec75271faf5f0743f8c0c5f1ba7a7ef6 @RuudBurger RuudBurger committed
Showing with 76 additions and 19 deletions.
  1. +1 −1 app/controllers/config.py
  2. +31 −15 app/controllers/movie.py
  3. +44 −3 app/lib/xbmc.py
View
2 app/controllers/config.py
@@ -63,7 +63,7 @@ def save(self, **data):
bools = filter(lambda s: not data.get(s),
[
'global.launchbrowser', 'global.updater',
- 'XBMC.enabled', 'XBMC.onSnatch',
+ 'XBMC.enabled', 'XBMC.onSnatch', 'XBMC.useWebAPIExistingCheck',
'NMJ.enabled',
'PLEX.enabled',
'PROWL.enabled', 'PROWL.onSnatch',
View
46 app/controllers/movie.py
@@ -155,6 +155,10 @@ def imdbAdd(self, **data):
return self.render({'id':id, 'result':result, 'success':success, 'year':data.get('year')})
+ @staticmethod
+ def _generateSQLQuery(movie):
+ return "select c09 from movie where c09='%s'" % (movie.imdb)
+
def _checkMovieExists(self, movie):
if cherrypy.config.get('config').get('XBMC', 'dbpath'):
dbfile = None
@@ -170,7 +174,9 @@ def _checkMovieExists(self, movie):
log.debug('Checking if movie exists in XBMC by IMDB id:' + movie.imdb)
connXbmc.row_factory = MySqlite.Row
cXbmc = connXbmc.cursor()
- cXbmc.execute('select c09 from movie where c09="' + movie.imdb + '"')
+ #sqlQuery = 'select c09 from movie where c09="' + movie.imdb + '"'
+ sqlQuery = self._generateSQLQuery(movie)
+ cXbmc.execute(sqlQuery)
#------End of Opening connection to XBMC DB------
inXBMC = False
for rowXbmc in cXbmc: # do a final check just to be sure
@@ -191,26 +197,36 @@ def _checkMovieExists(self, movie):
log.info('Could not find the XBMC MyVideos db at ' + cherrypy.config.get('config').get('XBMC', 'dbpath'))
if cherrypy.config.get('config').get('XBMC', 'useWebAPIExistingCheck'):
- xbmc = XBMC()
- xbmcResultsHosts = xbmc.queryVideoDatabase('select c09 from movie where c09="' + movie.imdb + '"')
-
- if xbmcResultsHosts:
- for xmbcResults in xbmcResultsHosts:
- for xmbcResult in [x.strip() for x in xmbcResults.strip().split('\n')]:
- if xmbcResult == "":
- continue
-
- c09 = xmbcResult.replace("<record><field>", "").replace("</field></record>", "").replace("<field>", "").replace("</field>", "").strip()
- if c09==movie.imdb:
- log.info('Movie already exists in XBMC (web API call), skipping.')
- return True
+ xbmc = XBMC()
+ #sqlQuery = 'select c09 from movie where c09="' + movie.imdb + '"'
+ sqlQuery = self._generateSQLQuery(movie)
+ xbmcResultsHosts = xbmc.queryVideoDatabase(sqlQuery)
+
+ if xbmcResultsHosts:
+ for xmbcResults in xbmcResultsHosts:
+ records = xmbcResults.strip().split("<record>")
+ for xmbcResult in records:
+# xmbcResult = xmbcResult.strip()
+ xmbcResult = xmbcResult.replace("</record>", "")
+# xmbcResult = xmbcResult.strip()
+
+ if xmbcResult == "":
+ continue
+
+ fields = filter(lambda x: x != "", [field.replace("</field>", "") for field in xmbcResult.split("<field>")])
+
+ log.debug("fields = %s" % fields)
+ c09 = fields[0]
+ if c09==movie.imdb:
+ log.info('Movie already exists in XBMC (web API call), skipping.')
+ return True
return False
def _addMovie(self, movie, quality, year = None):
if self._checkMovieExists(movie=movie):
- return
+ return
log.info('Adding movie to database: %s' % movie.name)
View
47 app/lib/xbmc.py
@@ -49,18 +49,59 @@ def notify(self, message):
for curHost in self.hosts:
command = {'command': 'ExecBuiltIn', 'parameter': 'Notification(CouchPotato, %s)' % message}
self.send(command, curHost)
-
+
+ def setResponseFormat(self, WebHeader=None, WebFooter=None, Header=None, Footer=None, OpenTag=None, CloseTag=None, CloseFinalTag=None, OpenRecordSet=None, CloseRecordSet=None, OpenRecord=None, CloseRecord=None, OpenField=None, CloseField=None):
+ '''See: http://wiki.xbmc.org/index.php?title=Web_Server_HTTP_API#Commands_that_Retrieve_Information'''
+ if not self.enabled:
+ return
+
+ arguments = locals()
+
+ booleans = ["WebHeader", "WebFooter", "CloseFinalTag"]
+ setResponseFormatCmds = []
+ while arguments:
+ k,v = arguments.popitem()
+ if k == 'self':
+ continue
+ elif k in booleans:
+ if v is not None and type(v) is bool:
+ setResponseFormatCmds += [k, " %s" % str(v)]
+ else:
+ if v is not None:
+ setResponseFormatCmds += [k, " %s" % str(v)] # XBMC expects space after ;
+
+ MAX_PARAS = 20 # We have to page here because XBMC has a cap on the # of params it can handle. This number better be even!
+ for page in xrange(0, (len(setResponseFormatCmds)+(MAX_PARAS-1))/MAX_PARAS):
+ start = page * MAX_PARAS
+ end = min((page + 1) * MAX_PARAS, len(setResponseFormatCmds))
+ setResponseFormatCmdStr = ";".join(setResponseFormatCmds[start:end])
+
+ for curHost in self.hosts:
+ command = {'command': 'SetResponseFormat(%s)' % setResponseFormatCmdStr}
+ response = self.send(command, curHost)
+
+ log.debug("%s: %s -> %s" % (curHost, setResponseFormatCmdStr, response))
+
+ def resetResponseFormat(self):
+ '''SetResponseFormat without any parameters returns the formatting to the default values (as would restarting xbmc).'''
+ self.setResponseFormat()
+
def queryVideoDatabase(self, query):
if not self.enabled:
return
+
+ self.resetResponseFormat()
+ self.setResponseFormat(WebHeader=False, WebFooter=False, Header="", Footer="", OpenTag="", CloseTag="", CloseFinalTag=True, OpenRecordSet="", CloseRecordSet="", OpenRecord="<record>", CloseRecord="</record>", OpenField="<field>", CloseField="</field>")
ret = []
for curHost in self.hosts:
command = {'command': 'QueryVideoDatabase(%s)' % query}
+
rawResponse = self.send(command, curHost)
if rawResponse:
- response = rawResponse.replace("<html>", "").replace("</html>", "")
- ret.append(response)
+ ret.append(rawResponse)
+
+ self.resetResponseFormat()
return ret

0 comments on commit f15d7bf

Please sign in to comment.
Something went wrong with that request. Please try again.