kod_db unica connessione per tutto l'addon
This commit is contained in:
@@ -10,3 +10,55 @@ try:
|
||||
import core
|
||||
except:
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
# Connect to database
|
||||
from . import filetools
|
||||
from platformcode import config
|
||||
import sqlite3, threading
|
||||
|
||||
db_name = filetools.join(config.get_data_path(), "kod_db.sqlite")
|
||||
db_semaphore = threading.Semaphore()
|
||||
|
||||
|
||||
class safeConn(sqlite3.Connection):
|
||||
"""thread-safe sqlite3.Connection"""
|
||||
def commit(self):
|
||||
db_semaphore.acquire()
|
||||
try:
|
||||
super(safeConn, self).commit()
|
||||
finally:
|
||||
db_semaphore.release()
|
||||
|
||||
|
||||
class safeCur(sqlite3.Cursor):
|
||||
"""thread-safe sqlite3.Cursor"""
|
||||
def execute(self, *args, **kwargs):
|
||||
db_semaphore.acquire()
|
||||
try:
|
||||
super(safeCur, self).execute(*args, **kwargs)
|
||||
finally:
|
||||
db_semaphore.release()
|
||||
|
||||
def executescript(self, *args, **kwargs):
|
||||
db_semaphore.acquire()
|
||||
try:
|
||||
super(safeCur, self).executescript(*args, **kwargs)
|
||||
finally:
|
||||
db_semaphore.release()
|
||||
|
||||
def executemany(self, *args, **kwargs):
|
||||
db_semaphore.acquire()
|
||||
try:
|
||||
super(safeCur, self).executemany(*args, **kwargs)
|
||||
finally:
|
||||
db_semaphore.release()
|
||||
|
||||
|
||||
db_conn = sqlite3.connect(db_name, factory=safeConn, timeout=15, check_same_thread=False)
|
||||
db = db_conn.cursor(safeCur)
|
||||
|
||||
# Create tables if not already exists
|
||||
db.execute('CREATE TABLE IF NOT EXISTS tmdb_cache (url TEXT PRIMARY KEY, response TEXT, added TEXT);')
|
||||
db.execute('CREATE TABLE IF NOT EXISTS viewed (tmdb_id TEXT PRIMARY KEY, season INT, episode INT, played_time REAL);')
|
||||
db.execute('CREATE TABLE IF NOT EXISTS dnscache(domain TEXT NOT NULL UNIQUE, ip TEXT NOT NULL, PRIMARY KEY(domain));')
|
||||
db_conn.commit()
|
||||
|
||||
@@ -12,16 +12,11 @@ else:
|
||||
|
||||
from lib.requests_toolbelt.adapters import host_header_ssl
|
||||
from lib import doh
|
||||
from platformcode import logger, config
|
||||
from platformcode import logger
|
||||
import requests
|
||||
from core import scrapertools
|
||||
from core import db, db_conn
|
||||
|
||||
try:
|
||||
import _sqlite3 as sql
|
||||
except:
|
||||
import sqlite3 as sql
|
||||
|
||||
db = os.path.join(config.get_data_path(), 'kod_db.sqlite')
|
||||
if 'PROTOCOL_TLS' in ssl.__dict__:
|
||||
protocol = ssl.PROTOCOL_TLS
|
||||
elif 'PROTOCOL_SSLv23' in ssl.__dict__:
|
||||
@@ -48,8 +43,6 @@ class CustomContext(ssl.SSLContext):
|
||||
class CipherSuiteAdapter(host_header_ssl.HostHeaderSSLAdapter):
|
||||
|
||||
def __init__(self, domain, CF=False, *args, **kwargs):
|
||||
self.conn = sql.connect(db)
|
||||
self.cur = self.conn.cursor()
|
||||
self.ssl_context = CustomContext(protocol, domain)
|
||||
self.CF = CF # if cloudscrape is in action
|
||||
self.cipherSuite = kwargs.pop('cipherSuite', DEFAULT_CIPHERS)
|
||||
@@ -57,15 +50,15 @@ class CipherSuiteAdapter(host_header_ssl.HostHeaderSSLAdapter):
|
||||
super(CipherSuiteAdapter, self).__init__(**kwargs)
|
||||
|
||||
def flushDns(self, request, domain, **kwargs):
|
||||
self.cur.execute('delete from dnscache where domain=?', (domain,))
|
||||
self.conn.commit()
|
||||
db.execute('delete from dnscache where domain=?', (domain,))
|
||||
db_conn.commit()
|
||||
return self.send(request, flushedDns=True, **kwargs)
|
||||
|
||||
def getIp(self, domain):
|
||||
ip = None
|
||||
try:
|
||||
self.cur.execute('select ip from dnscache where domain=?', (domain,))
|
||||
ip = self.cur.fetchall()[0][0]
|
||||
db.execute('select ip from dnscache where domain=?', (domain,))
|
||||
ip = db.fetchall()[0][0]
|
||||
logger.info('Cache DNS: ' + domain + ' = ' + str(ip))
|
||||
except:
|
||||
pass
|
||||
@@ -82,14 +75,10 @@ class CipherSuiteAdapter(host_header_ssl.HostHeaderSSLAdapter):
|
||||
|
||||
def writeToCache(self, domain, ip):
|
||||
try:
|
||||
self.cur.execute('insert into dnscache values(?,?)', (domain, ip))
|
||||
db.execute('insert into dnscache values(?,?)', (domain, ip))
|
||||
db_conn.commit()
|
||||
except:
|
||||
self.cur.execute("""CREATE TABLE IF NOT EXISTS dnscache(
|
||||
"domain" TEXT NOT NULL UNIQUE,
|
||||
"ip" TEXT NOT NULL,
|
||||
PRIMARY KEY("domain")
|
||||
);""")
|
||||
self.conn.commit()
|
||||
pass
|
||||
|
||||
def init_poolmanager(self, *args, **kwargs):
|
||||
kwargs['ssl_context'] = self.ssl_context
|
||||
|
||||
34
core/tmdb.py
34
core/tmdb.py
@@ -62,27 +62,7 @@ def_lang = info_language[config.get_setting("info_language", "videolibrary")]
|
||||
# ------------------------------------------------- -------------------------------------------------- -----------
|
||||
|
||||
otmdb_global = None
|
||||
fname = filetools.join(config.get_data_path(), "kod_db.sqlite")
|
||||
|
||||
def create_bd():
|
||||
conn = sqlite3.connect(fname)
|
||||
c = conn.cursor()
|
||||
c.execute('CREATE TABLE IF NOT EXISTS tmdb_cache (url TEXT PRIMARY KEY, response TEXT, added TEXT)')
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
def drop_bd():
|
||||
conn = sqlite3.connect(fname)
|
||||
c = conn.cursor()
|
||||
c.execute('DROP TABLE IF EXISTS tmdb_cache')
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return True
|
||||
|
||||
|
||||
create_bd()
|
||||
from core import db, db_conn
|
||||
|
||||
|
||||
# The function name is the name of the decorator and receives the function that decorates.
|
||||
@@ -148,13 +128,11 @@ def cache_response(fn):
|
||||
result = fn(*args)
|
||||
else:
|
||||
|
||||
conn = sqlite3.connect(fname, timeout=15)
|
||||
c = conn.cursor()
|
||||
url = re.sub('&year=-', '', args[0])
|
||||
if PY3: url = str.encode(url)
|
||||
url_base64 = base64.b64encode(url)
|
||||
c.execute("SELECT response, added FROM tmdb_cache WHERE url=?", (url_base64,))
|
||||
row = c.fetchone()
|
||||
db.execute("SELECT response, added FROM tmdb_cache WHERE url=?", (url_base64,))
|
||||
row = db.fetchone()
|
||||
|
||||
if row and check_expired(float(row[1])):
|
||||
result = eval(base64.b64decode(row[0]))
|
||||
@@ -165,12 +143,10 @@ def cache_response(fn):
|
||||
result = str(result)
|
||||
if PY3: result = str.encode(result)
|
||||
result_base64 = base64.b64encode(result)
|
||||
c.execute("INSERT OR REPLACE INTO tmdb_cache (url, response, added) VALUES (?, ?, ?)",
|
||||
db.execute("INSERT OR REPLACE INTO tmdb_cache (url, response, added) VALUES (?, ?, ?)",
|
||||
(url_base64, result_base64, time.time()))
|
||||
|
||||
conn.commit()
|
||||
|
||||
conn.close()
|
||||
db_conn.commit()
|
||||
|
||||
# elapsed_time = time.time() - start_time
|
||||
# logger.debug("TARDADO %s" % elapsed_time)
|
||||
|
||||
@@ -1370,7 +1370,9 @@ def get_platform():
|
||||
|
||||
|
||||
def get_played_time(item):
|
||||
# from core.support import dbg;dbg()
|
||||
logger.debug()
|
||||
from core import db
|
||||
|
||||
if not item.infoLabels:
|
||||
return 0
|
||||
ID = item.infoLabels.get('tmdb_id','')
|
||||
@@ -1380,39 +1382,30 @@ def get_played_time(item):
|
||||
S = item.infoLabels.get('season')
|
||||
E = item.infoLabels.get('episode')
|
||||
|
||||
import sqlite3
|
||||
from core import filetools
|
||||
|
||||
db_name = filetools.join(config.get_data_path(), "kod_db.sqlite")
|
||||
conn = sqlite3.connect(db_name, timeout=15)
|
||||
c = conn.cursor()
|
||||
c.execute('CREATE TABLE IF NOT EXISTS viewed (tmdb_id TEXT, season INT, episode INT, played_time REAL)')
|
||||
conn.commit()
|
||||
|
||||
if item.contentType == 'movie':
|
||||
c.execute("SELECT played_time FROM viewed WHERE tmdb_id=?", (ID,))
|
||||
db.execute("SELECT played_time FROM viewed WHERE tmdb_id=?", (ID,))
|
||||
|
||||
elif S and E:
|
||||
S = item.infoLabels['season']
|
||||
E = item.infoLabels['episode']
|
||||
c.execute("SELECT played_time FROM viewed WHERE tmdb_id=? AND season=? AND episode=?", (ID, S, E))
|
||||
db.execute("SELECT played_time FROM viewed WHERE tmdb_id=? AND season=? AND episode=?", (ID, S, E))
|
||||
|
||||
elif E:
|
||||
E = item.infoLabels['episode']
|
||||
c.execute("SELECT played_time FROM viewed WHERE tmdb_id=? AND episode=?", (ID, E))
|
||||
db.execute("SELECT played_time FROM viewed WHERE tmdb_id=? AND episode=?", (ID, E))
|
||||
|
||||
result = c.fetchone()
|
||||
result = db.fetchone()
|
||||
|
||||
if not result: played_time = 0
|
||||
else: played_time = result[0]
|
||||
|
||||
conn.close()
|
||||
|
||||
return played_time
|
||||
|
||||
|
||||
def set_played_time(item):
|
||||
# from core.support import dbg;dbg()
|
||||
logger.debug()
|
||||
from core import db, db_conn
|
||||
|
||||
played_time = item.played_time
|
||||
if not item.infoLabels:
|
||||
return
|
||||
@@ -1424,39 +1417,34 @@ def set_played_time(item):
|
||||
S = item.infoLabels.get('season')
|
||||
E = item.infoLabels.get('episode')
|
||||
|
||||
import sqlite3
|
||||
from core import filetools
|
||||
db_name = filetools.join(config.get_data_path(), "kod_db.sqlite")
|
||||
conn = sqlite3.connect(db_name, timeout=15)
|
||||
c = conn.cursor()
|
||||
|
||||
if item.contentType == 'movie':
|
||||
c.execute("SELECT played_time FROM viewed WHERE tmdb_id=?", (ID,))
|
||||
result = c.fetchone()
|
||||
db.execute("SELECT played_time FROM viewed WHERE tmdb_id=?", (ID,))
|
||||
result = db.fetchone()
|
||||
if result:
|
||||
if played_time > 0: c.execute("UPDATE viewed SET played_time=? WHERE tmdb_id=?", (played_time, ID))
|
||||
else: c.execute("DELETE from viewed WHERE tmdb_id=?", (ID,))
|
||||
else: c.execute("INSERT INTO viewed (tmdb_id, played_time) VALUES (?, ?)", (ID, played_time))
|
||||
if played_time > 0: db.execute("UPDATE viewed SET played_time=? WHERE tmdb_id=?", (played_time, ID))
|
||||
else: db.execute("DELETE from viewed WHERE tmdb_id=?", (ID,))
|
||||
else: db.execute("INSERT INTO viewed (tmdb_id, played_time) VALUES (?, ?)", (ID, played_time))
|
||||
|
||||
elif S and E:
|
||||
c.execute("SELECT played_time FROM viewed WHERE tmdb_id=? AND season = ? AND episode=?", (ID, S, E))
|
||||
result = c.fetchone()
|
||||
db.execute("SELECT played_time FROM viewed WHERE tmdb_id=? AND season = ? AND episode=?", (ID, S, E))
|
||||
result = db.fetchone()
|
||||
if result:
|
||||
if played_time > 0: c.execute("UPDATE viewed SET played_time=? WHERE tmdb_id=? AND season=? AND episode=?", (played_time, ID, S, E))
|
||||
else: c.execute("DELETE from viewed WHERE tmdb_id=? AND season=? AND episode=?", (ID, S, E))
|
||||
else: c.execute("INSERT INTO viewed (tmdb_id, season, episode, played_time) VALUES (?, ?, ?, ?)", (ID, S, E, played_time))
|
||||
if played_time > 0: db.execute("UPDATE viewed SET played_time=? WHERE tmdb_id=? AND season=? AND episode=?", (played_time, ID, S, E))
|
||||
else: db.execute("DELETE from viewed WHERE tmdb_id=? AND season=? AND episode=?", (ID, S, E))
|
||||
else: db.execute("INSERT INTO viewed (tmdb_id, season, episode, played_time) VALUES (?, ?, ?, ?)", (ID, S, E, played_time))
|
||||
|
||||
elif E:
|
||||
E = item.infoLabels['episode']
|
||||
c.execute("SELECT played_time FROM viewed WHERE tmdb_id=? AND episode=?", (ID, E))
|
||||
result = c.fetchone()
|
||||
db.execute("SELECT played_time FROM viewed WHERE tmdb_id=? AND episode=?", (ID, E))
|
||||
result = db.fetchone()
|
||||
if result:
|
||||
if played_time > 0: c.execute("UPDATE viewed SET played_time=? WHERE tmdb_id=? AND episode=?", (played_time, ID, E))
|
||||
else: c.execute("DELETE from viewed WHERE tmdb_id=? AND episode=?", (ID, E))
|
||||
else: c.execute("INSERT INTO viewed (tmdb_id, episode, played_time) VALUES (?, ?, ?)", (ID, E, played_time))
|
||||
if played_time > 0: db.execute("UPDATE viewed SET played_time=? WHERE tmdb_id=? AND episode=?", (played_time, ID, E))
|
||||
else: db.execute("DELETE from viewed WHERE tmdb_id=? AND episode=?", (ID, E))
|
||||
else: db.execute("INSERT INTO viewed (tmdb_id, episode, played_time) VALUES (?, ?, ?)", (ID, E, played_time))
|
||||
|
||||
db_conn.commit()
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def prevent_busy(item):
|
||||
logger.debug()
|
||||
|
||||
@@ -46,6 +46,7 @@ def mark_auto_as_watched(item):
|
||||
ND = next_dialogs[next_ep_type]
|
||||
try: next_episode = next_ep(item)
|
||||
except: next_episode = False
|
||||
logger.debug(next_episode)
|
||||
|
||||
while platformtools.is_playing():
|
||||
actual_time = xbmc.Player().getTime()
|
||||
|
||||
Reference in New Issue
Block a user