Skip to content
This repository has been archived by the owner on May 15, 2020. It is now read-only.

Commit

Permalink
Removed all uses of httpapi for sql db access, replaced with smart (d…
Browse files Browse the repository at this point in the history
…b finding) direct access. Should solve #101, #61 the freezing of xbmc when syncing seen back to xbmc.
  • Loading branch information
othrayte committed Feb 12, 2012
1 parent 8a58d67 commit 628395e
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 28 deletions.
1 change: 1 addition & 0 deletions addon.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<requires>
<import addon="xbmc.python" version="1.0"/>
<import addon="script.module.simplejson" version="2.0.10"/>
<import addon="script.module.myconnpy" version="0.3.2"/>
</requires>
<extension point="xbmc.python.script" library="default.py">
<provides>video</provides>
Expand Down
90 changes: 90 additions & 0 deletions raw_xbmc_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import os, xbmc
from utilities import Debug
#provides access to the raw xbmc video database


global _RawXbmcDb__conn
_RawXbmcDb__conn = None
class RawXbmcDb():

# make a httpapi based XBMC db query (get data)
@staticmethod
def query(query):
global _RawXbmcDb__conn
if _RawXbmcDb__conn is None:
_RawXbmcDb__conn = _findXbmcDb()

Debug("[RawXbmcDb] query: "+query)
cursor = _RawXbmcDb__conn.cursor()
cursor.execute(query)

matches = []
for row in cursor:
matches.append(row)

Debug("[RawXbmcDb] matches: "+unicode(matches))

_RawXbmcDb__conn.commit()
cursor.close()
return matches

# execute a httpapi based XBMC db query (set data)
@staticmethod
def execute(query):
global _RawXbmcDb__conn
if _RawXbmcDb__conn is None:
_RawXbmcDb__conn = _findXbmcDb()
cursor = _RawXbmcDb__conn.cursor()
Debug("[RawXbmcDb] query: "+query)
cursor.execute(query)

def _findXbmcDb():
type = None
host = None
port = 3306
name = None
user = None
passwd = None
if not os.path.exists(xbmc.translatePath("special://userdata/advancedsettings.xml")):
type = 'sqlite3'
else:
from xml.etree.ElementTree import ElementTree
advancedsettings = ElementTree()
advancedsettings.parse(xbmc.translatePath("special://userdata/advancedsettings.xml"))
settings = advancedsettings.getroot().find("videodatabase")
if settings is not None:
for setting in settings:
if setting.tag == 'type':
type = setting.text
elif setting.tag == 'host':
host = setting.text
elif setting.tag == 'port':
port = setting.text
elif setting.tag == 'name':
name = setting.text
elif setting.tag == 'user':
user = setting.text
elif setting.tag == 'pass':
passwd = setting.text
else:
type = 'sqlite3'

if type == 'sqlite3':
if host is None:
path = xbmc.translatePath("special://userdata/database")

This comment has been minimized.

Copy link
@azgul

azgul Feb 12, 2012

Seems database needs to have upper case d on XBMCbuntu beta 3.

This comment has been minimized.

Copy link
@othrayte

othrayte Feb 12, 2012

Author Contributor

Thanks, will do

files = os.listdir(path)
latest = ""
for file in files:
if file[:8] == 'MyVideos':
if file > latest:
latest = file
Debug(latest)
host = os.path.join(path,latest)
else:
host += ".db"
import sqlite3
return sqlite3.connect(host)
if type == 'mysql':
import mysql.connector
return mysql.connector.Connect(host = host, port = port, database = name, user = user, password = passwd)

37 changes: 9 additions & 28 deletions utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def Debug(msg, force=False):
except UnicodeEncodeError:
print "Trakt Utilities: " + msg.encode( "utf-8", "ignore" )

#This class needs debug
from raw_xbmc_database import RawXbmcDb

def notification( header, message, time=5000, icon=__settings__.getAddonInfo( "icon" ) ):
xbmc.executebuiltin( "XBMC.Notification(%s,%s,%i,%s)" % ( header, message, time, icon ) )

Expand Down Expand Up @@ -85,25 +88,6 @@ def checkSettings(daemon=False):
def xcp(s):
return re.sub('''(['])''', r"''", unicode(s))

# make a httpapi based XBMC db query (get data)
def xbmcHttpapiQuery(query):
Debug("[httpapi-sql] query: "+query)

xml_data = xbmc.executehttpapi( "QueryVideoDatabase(%s)" % urllib.quote_plus(query), )
match = re.findall( "<field>((?:[^<]|<(?!/))*)</field>", xml_data,)

Debug("[httpapi-sql] responce: "+xml_data)
Debug("[httpapi-sql] matches: "+unicode(match))

return match

# execute a httpapi based XBMC db query (set data)
def xbmcHttpapiExec(query):
Debug("[httpapi-sql] query: "+query)
xml_data = xbmc.executehttpapi( "ExecVideoDatabase(%s)" % urllib.quote_plus(query), )
Debug("[httpapi-sql] responce: "+xml_data)
return xml_data

# get a connection to trakt
def getTraktConnection():
try:
Expand Down Expand Up @@ -418,15 +402,15 @@ def setXBMCMoviePlaycount(imdb_id, playcount):

# httpapi till jsonrpc supports playcount update
# c09 => IMDB ID
match = xbmcHttpapiQuery(
match = RawXbmcDb.query(
"SELECT movie.idFile FROM movie"+
" WHERE movie.c09='%(imdb_id)s'" % {'imdb_id':xcp(imdb_id)})

if not match:
#add error message here
return

result = xbmcHttpapiExec(
RawXbmcDb.execute(
"UPDATE files"+
" SET playcount=%(playcount)d" % {'playcount':int(playcount)}+
" WHERE idFile=%(idFile)s" % {'idFile':xcp(match[0])})
Expand All @@ -436,7 +420,7 @@ def setXBMCMoviePlaycount(imdb_id, playcount):
# sets the playcount of a given episode by tvdb_id
def setXBMCEpisodePlaycount(tvdb_id, seasonid, episodeid, playcount):
# httpapi till jsonrpc supports playcount update
responce = xbmcHttpapiExec(
RawXbmcDb.execute(
"UPDATE files"+
" SET playcount=%(playcount)s" % {'playcount':xcp(playcount)}+
" WHERE idFile IN ("+
Expand All @@ -448,8 +432,6 @@ def setXBMCEpisodePlaycount(tvdb_id, seasonid, episodeid, playcount):
" AND episode.c12='%(seasonid)s'" % {'seasonid':xcp(seasonid)}+
" AND episode.c13='%(episodeid)s'" % {'episodeid':xcp(episodeid)}+
" )")

Debug("xml answer: " + str(responce))

# get current video being played from XBMC
def getCurrentPlayingVideoFromXBMC():
Expand Down Expand Up @@ -536,7 +518,7 @@ def getMovieIdFromXBMC(imdb_id, title):
# Get id of movie by movies IMDB
Debug("Searching for movie: "+imdb_id+", "+title)

match = xbmcHttpapiQuery(
match = RawXbmcDb.query(
" SELECT idMovie FROM movie"+
" WHERE c09='%(imdb_id)s'" % {'imdb_id':imdb_id}+
" UNION"+
Expand All @@ -556,7 +538,7 @@ def getShowIdFromXBMC(tvdb_id, title):

Debug("Searching for show: "+str(tvdb_id)+", "+title)

match = xbmcHttpapiQuery(
match = RawXbmcDb.query(
" SELECT idShow FROM tvshow"+
" WHERE c12='%(tvdb_id)s'" % {'tvdb_id':xcp(tvdb_id)}+
" UNION"+
Expand Down Expand Up @@ -881,8 +863,7 @@ def scrobbleEpisodeOnTrakt(tvdb_id, title, year, season, episode, duration, perc
Debug("Error in request from 'scrobbleEpisodeOnTrakt()'")
return responce




"""
ToDo:
Expand Down

3 comments on commit 628395e

@azgul
Copy link

@azgul azgul commented on 628395e Feb 12, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Been trying to figure out for a while now, how to get rid of the trailing comma python leaves,

18:13:00 T:2931813232 NOTICE: Trakt Utilities: [RawXbmcDb] query: SELECT movie.idFile FROM movie WHERE movie.c09='tt0468569'
18:13:00 T:2931813232 NOTICE: Trakt Utilities: [RawXbmcDb] matches: [(507,)]
18:13:00 T:2931813232 NOTICE: Trakt Utilities: [RawXbmcDb] query: UPDATE files SET playcount=1 WHERE idFile=(507,)
18:13:00 T:2931813232 ERROR: Error Type: <class 'sqlite3.OperationalError'>
18:13:00 T:2931813232 ERROR: Error Contents: near ",": syntax error

any clues on how I can fix this? :)

@MartijnKaijser
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.replace(",","") would make the , disappear

@othrayte
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better so solution is I'll patch that bit of code tonight, xbmc is actually correct it is a list of tuples, I should be extracting only the first element so the whole, comma and brackets would disappear.

Please sign in to comment.