# Path: resources/lib/constants.py
# ADDON = xbmcaddon.Addon(id=ADDON_ID)
# 
# class KodiDbMap:
#     MyVideos = {16: 99, 17: 107, 18: 116, 19: 119, 20: 119}
#     MyMusic = {16: 56, 17: 60, 18: 72, 19: 82, 20: 82}
#     Textures = {16: 13, 17: 13, 18: 13, 19: 13, 20: 13}
#     Addons = {16: 20, 17: 27, 18: 27, 19: 33, 20: 33}
#     ViewModes = {16: 6, 17: 6, 18: 6, 19: 6, 20: 6}
# 
# ADDON_ID = 'plugin.video.stream-cinema'
# 
# class SC:
#     ACTION = 'action'
#     ACTION_ADD2HP = 'add2hp'
#     ACTION_ADD_CUSTOM_FILTER = 'add_custom_filter'
#     ACTION_ADD_TO_LIBRARY = 'add_to_library'
#     ACTION_ADD_TO_LIBRARY_WITH_SUBSCRIPTION = 'add_to_library_sub'
#     ACTION_ANDROID = 'android'
#     ACTION_BUFFER = 'buffer'
#     ACTION_CMD = 'cmd'
#     ACTION_CSEARCH = 'csearch'
#     ACTION_DEBUG = 'debug'
#     ACTION_DEL2HP = 'del2hp'
#     ACTION_DEL_CUSTOM_FILTER = 'del_custom_filter'
#     ACTION_DEL_PREFERRED_LANGUAGE = 'sel_preferred_language'
#     ACTION_DOWNLOAD = 'down'
#     ACTION_LAST = 'last'
#     ACTION_PIN = 'pin'
#     ACTION_PLAY_URL = 'playUrl'
#     ACTION_REMOVE_FROM_LIST = 'remove_from_list'
#     ACTION_REMOVE_FROM_SUBSCRIPTION = 'remove_from_sub'
#     ACTION_SELECT_STREAM = 'selectStream'
#     ACTION_SET_PREFERRED_LANGUAGE = 'set_preferred_language'
#     ACTION_UPDATE_ADDON = 'update_addon'
#     CMD = 'cmd'
#     DEFAULT_LANG = 'cs'
#     ITEM_ACTION = 'action'
#     ITEM_ART = 'art'
#     ITEM_AUDIO_INFO = 'ainfo'
#     ITEM_BITRATE = 'bitrate'
#     ITEM_CMD = 'cmd'
#     ITEM_CUSTOM_FILTER = 'custom'
#     ITEM_DIR = 'dir'
#     ITEM_DOWNLOAD = 'down'
#     ITEM_FOCUS = 'focus'
#     ITEM_HPDIR = 'hpdir'
#     ITEM_I18N_ART = 'i18n_art'
#     ITEM_I18N_INFO = 'i18n_info'
#     ITEM_ID = 'id'
#     ITEM_IDENT = 'ident'
#     ITEM_INFO = 'info'
#     ITEM_LIBRARY = 'library'
#     ITEM_LIBRARY_SUB = 'library_sub'
#     ITEM_LANG = 'lang'
#     ITEM_MENU = 'menu'
#     ITEM_NEXT = 'next'
#     ITEM_PAGE = 'page'
#     ITEM_PREFERRED_LANG = 'pref_lang.{}'
#     ITEM_PROVIDER = 'provider'
#     ITEM_QUALITY = 'quality'
#     ITEM_SIZE = 'size'
#     ITEM_STRMS = 'strms'
#     ITEM_SUBS = 'subs'
#     ITEM_SYSTEM = 'system'
#     ITEM_TITLE = 'title'
#     ITEM_TYPE = 'type'
#     ITEM_URL = 'url'
#     ITEM_UIDS = 'unique_ids'
#     ITEM_VIDEO = 'video'
#     ITEM_VIDEO_INFO = 'vinfo'
#     ITEM_VISIBLE = 'visible'
#     ITEM_WIDGET = 'widget'
#     MEDIA_TYPE = 'mediatype'
#     MEDIA_TYPE_AUDIO = 'audio'
#     MEDIA_TYPE_EPISODE = 'episode'
#     MEDIA_TYPE_FILE = 'file'
#     MEDIA_TYPE_SEASON = 'season'
#     MEDIA_TYPE_TV_SHOW = 'tvshow'
#     MEDIA_TYPE_VIDEO = 'video'
#     NEXT = 'next'
#     PROVIDER = 'kraska'
#     RUN_PLUGIN = 'RunPlugin({})'
#     TXT_PINNED = 'p-{}'
#     TXT_CUSTOM_FORMAT = 'custom-{}'
#     TRAKT_CACHE_WATCHET_ACTIVITY = 'SC:watched:activity'
# 
# Path: resources/lib/common/logger.py
# def debug(text):
#     xbmc.log('[{}] {}'.format(APP, text), xbmc.LOGDEBUG)
# 
# Path: resources/lib/gui/dialog.py
# def dok(heading, message=None):
#     if SYSTEM_VERSION > 18:
#         return dialog().ok(heading=heading, message=message)
#     return dialog().ok(heading=heading, line1=message)
# 
# def dprogressgb():
#     return xbmcgui.DialogProgressBG()
# 
# Path: resources/lib/kodiutils.py
# def translate_path(path):
#     try:
#         return xbmcvfs.translatePath(path)
#     except:
#         return xbmc.translatePath(path)
# 
# def get_skin_name():
#     return xbmc.getSkinDir()
# 
# Path: resources/lib/system.py
# SYSTEM_VERSION = int(SYSTEM_BUILD_VERSION.split(".")[0])


import os
import sqlite3
import xbmcvfs
from json import loads, dumps
from resources.lib.constants import ADDON, KodiDbMap, ADDON_ID, SC
from resources.lib.common.logger import debug
from resources.lib.gui.dialog import dok, dprogressgb
from resources.lib.kodiutils import translate_path, get_skin_name
from resources.lib.system import SYSTEM_VERSION
            from resources.lib.language import Strings
        self._path = translate_path(path)
        self._connection = None
        # debug('db file: {}'.format(self._path))

    def _get_conn(self):
        if self._connection is None:
            self._connection = sqlite3.Connection(self._path, timeout=60)
        try:
            self._connection.cursor()
        except sqlite3.ProgrammingError:
            self._connection = None
            return self._get_conn()
        return self._connection

    def execute(self, query, *args):
        # debug('SQL: {} <- {}'.format(query, args))
        # debug('SQL')
        with self._get_conn() as conn:
            c = conn.cursor()
            c.execute(query, args)
            return c


class KodiAddonsDb:
    def __init__(self):
        path = 'special://database/Addons{}.db'.format(KodiDbMap.Addons[SYSTEM_VERSION])
        self._db = Sqlite(path)

    def check_repo(self):
        query = 'select id from repo where id in (' \

# Path: resources/lib/constants.py
# ADDON = xbmcaddon.Addon(id=ADDON_ID)
# 
# class KodiDbMap:
#     MyVideos = {16: 99, 17: 107, 18: 116, 19: 119, 20: 119}
#     MyMusic = {16: 56, 17: 60, 18: 72, 19: 82, 20: 82}
#     Textures = {16: 13, 17: 13, 18: 13, 19: 13, 20: 13}
#     Addons = {16: 20, 17: 27, 18: 27, 19: 33, 20: 33}
#     ViewModes = {16: 6, 17: 6, 18: 6, 19: 6, 20: 6}
# 
# ADDON_ID = 'plugin.video.stream-cinema'
# 
# class SC:
#     ACTION = 'action'
#     ACTION_ADD2HP = 'add2hp'
#     ACTION_ADD_CUSTOM_FILTER = 'add_custom_filter'
#     ACTION_ADD_TO_LIBRARY = 'add_to_library'
#     ACTION_ADD_TO_LIBRARY_WITH_SUBSCRIPTION = 'add_to_library_sub'
#     ACTION_ANDROID = 'android'
#     ACTION_BUFFER = 'buffer'
#     ACTION_CMD = 'cmd'
#     ACTION_CSEARCH = 'csearch'
#     ACTION_DEBUG = 'debug'
#     ACTION_DEL2HP = 'del2hp'
#     ACTION_DEL_CUSTOM_FILTER = 'del_custom_filter'
#     ACTION_DEL_PREFERRED_LANGUAGE = 'sel_preferred_language'
#     ACTION_DOWNLOAD = 'down'
#     ACTION_LAST = 'last'
#     ACTION_PIN = 'pin'
#     ACTION_PLAY_URL = 'playUrl'
#     ACTION_REMOVE_FROM_LIST = 'remove_from_list'
#     ACTION_REMOVE_FROM_SUBSCRIPTION = 'remove_from_sub'
#     ACTION_SELECT_STREAM = 'selectStream'
#     ACTION_SET_PREFERRED_LANGUAGE = 'set_preferred_language'
#     ACTION_UPDATE_ADDON = 'update_addon'
#     CMD = 'cmd'
#     DEFAULT_LANG = 'cs'
#     ITEM_ACTION = 'action'
#     ITEM_ART = 'art'
#     ITEM_AUDIO_INFO = 'ainfo'
#     ITEM_BITRATE = 'bitrate'
#     ITEM_CMD = 'cmd'
#     ITEM_CUSTOM_FILTER = 'custom'
#     ITEM_DIR = 'dir'
#     ITEM_DOWNLOAD = 'down'
#     ITEM_FOCUS = 'focus'
#     ITEM_HPDIR = 'hpdir'
#     ITEM_I18N_ART = 'i18n_art'
#     ITEM_I18N_INFO = 'i18n_info'
#     ITEM_ID = 'id'
#     ITEM_IDENT = 'ident'
#     ITEM_INFO = 'info'
#     ITEM_LIBRARY = 'library'
#     ITEM_LIBRARY_SUB = 'library_sub'
#     ITEM_LANG = 'lang'
#     ITEM_MENU = 'menu'
#     ITEM_NEXT = 'next'
#     ITEM_PAGE = 'page'
#     ITEM_PREFERRED_LANG = 'pref_lang.{}'
#     ITEM_PROVIDER = 'provider'
#     ITEM_QUALITY = 'quality'
#     ITEM_SIZE = 'size'
#     ITEM_STRMS = 'strms'
#     ITEM_SUBS = 'subs'
#     ITEM_SYSTEM = 'system'
#     ITEM_TITLE = 'title'
#     ITEM_TYPE = 'type'
#     ITEM_URL = 'url'
#     ITEM_UIDS = 'unique_ids'
#     ITEM_VIDEO = 'video'
#     ITEM_VIDEO_INFO = 'vinfo'
#     ITEM_VISIBLE = 'visible'
#     ITEM_WIDGET = 'widget'
#     MEDIA_TYPE = 'mediatype'
#     MEDIA_TYPE_AUDIO = 'audio'
#     MEDIA_TYPE_EPISODE = 'episode'
#     MEDIA_TYPE_FILE = 'file'
#     MEDIA_TYPE_SEASON = 'season'
#     MEDIA_TYPE_TV_SHOW = 'tvshow'
#     MEDIA_TYPE_VIDEO = 'video'
#     NEXT = 'next'
#     PROVIDER = 'kraska'
#     RUN_PLUGIN = 'RunPlugin({})'
#     TXT_PINNED = 'p-{}'
#     TXT_CUSTOM_FORMAT = 'custom-{}'
#     TRAKT_CACHE_WATCHET_ACTIVITY = 'SC:watched:activity'
# 
# Path: resources/lib/common/logger.py
# def debug(text):
#     xbmc.log('[{}] {}'.format(APP, text), xbmc.LOGDEBUG)
# 
# Path: resources/lib/gui/dialog.py
# def dok(heading, message=None):
#     if SYSTEM_VERSION > 18:
#         return dialog().ok(heading=heading, message=message)
#     return dialog().ok(heading=heading, line1=message)
# 
# def dprogressgb():
#     return xbmcgui.DialogProgressBG()
# 
# Path: resources/lib/kodiutils.py
# def translate_path(path):
#     try:
#         return xbmcvfs.translatePath(path)
#     except:
#         return xbmc.translatePath(path)
# 
# def get_skin_name():
#     return xbmc.getSkinDir()
# 
# Path: resources/lib/system.py
# SYSTEM_VERSION = int(SYSTEM_BUILD_VERSION.split(".")[0])


# Path: resources/lib/common/storage.py
import os
import sqlite3
import xbmcvfs
from json import loads, dumps
from resources.lib.constants import ADDON, KodiDbMap, ADDON_ID, SC
from resources.lib.common.logger import debug
from resources.lib.gui.dialog import dok, dprogressgb
from resources.lib.kodiutils import translate_path, get_skin_name
from resources.lib.system import SYSTEM_VERSION
            from resources.lib.language import Strings

        self._path = translate_path(path)
        self._connection = None
        # debug('db file: {}'.format(self._path))

    def _get_conn(self):
        if self._connection is None:
            self._connection = sqlite3.Connection(self._path, timeout=60)
        try:
            self._connection.cursor()
        except sqlite3.ProgrammingError:
            self._connection = None
            return self._get_conn()
        return self._connection

    def execute(self, query, *args):
        # debug('SQL: {} <- {}'.format(query, args))
        # debug('SQL')
        with self._get_conn() as conn:
            c = conn.cursor()
            c.execute(query, args)
            return c


class KodiAddonsDb:
    def __init__(self):
        path = 'special://database/Addons{}.db'.format(KodiDbMap.Addons[SYSTEM_VERSION])
        self._db = Sqlite(path)

    def check_repo(self):
        query = 'select id from repo where id in (' \

                'select idRepo from addonlinkrepo where idAddon in (' \