def save_to_db(items, url): if not items or not url: return False url2 = clean_url(url) koding.reset_db() test_spec = {"columns": {"value": "TEXT", "created": "TEXT"}} koding.Create_Table(url2, test_spec) set_val = base64.b64encode(pickle.dumps(items)) koding.Add_To_Table(url2, {"value": set_val, "created": time.time()}) table_name_spec = { "columns": { "name": "TEXT", } } koding.Create_Table("Table_names", table_name_spec) plugin_table_name_spec = { "columns": { "name": "TEXT", } } koding.Create_Table("Plugin_table_names", plugin_table_name_spec) if "PLugin" in url: koding.Add_To_Table("Plugin_table_names", { "name": url2, }) else: koding.Add_To_Table("Table_names", { "name": url2, })
def search(): """ Open root search directory """ versionspec = {"columns": {"version": "TEXT"}} koding.Create_Table("version", versionspec) search_spec = {"columns": {"term": "TEXT"}} koding.Create_Table("search", search_spec) terms = koding.Get_All_From_Table("search") if terms: koding.Add_Dir(name=_("Clear Search"), mode="clear_search", folder=True, icon=icon, fanart=fanart) for term in terms: label = term["term"] context_menu = [(_("Remove Search"), "RunPlugin({0})".format( get_addon_url(mode="remove_search", url=label)))] koding.Add_Dir(name=label, url=label, mode="do_search", folder=True, icon=icon, fanart=fanart, context_items=context_menu) koding.Add_Dir(name=_("Add Search"), mode="add_search", folder=True, icon=icon, fanart=fanart)
def fetch_episode_from_db(identifier, season, episode, provider, lang): """ get episode's meta info from database Args: identifier: identifier of the item season: season number of the episode episode: episode number of the episode provider: metadata provider to fetch info for lang: language to fetch info for Returns: item's metadata """ import koding versionspec = {"columns": {"version": "TEXT"}} koding.Create_Table("version", versionspec) if not koding.Get_All_From_Table("version"): koding.Add_To_Table("version", {"version": "0.0.1"}) episode_meta_spec = { "columns": { "identifier": "TEXT", "season": "TEXT", "episode": "TEXT", "provider": "TEXT", "lang": "TEXT", "meta": "TEXT", "created": "TEXT" }, "constraints": { "unique": "identifier, provider, lang", } } koding.Create_Table("episode_meta", episode_meta_spec) match = koding.Get_From_Table( "episode_meta", { "identifier": identifier, "provider": provider, "season": season, "episode": episode, "lang": lang }) if match: match = match[0] if not match["meta"]: return None, match["created"] match_meta = match["meta"].replace("'", "\"") try: match_meta = match_meta.encode('ascii', 'ignore') except: match_meta = match_meta.decode('utf-8').encode('ascii', 'ignore') return pickle.loads(match_meta), match["created"] else: return [], None
def fetch_from_db(url): koding.reset_db() pcastone_plugin_spec = { "columns": { "url": "TEXT", "item": "TEXT", "created": "TEXT" }, "constraints": { "unique": "url" } } koding.Create_Table("pcastone_com_plugin", pcastone_plugin_spec) match = koding.Get_From_Table( "pcastone_com_plugin", {"url": url}) if match: match = match[0] if not match["item"]: return None created_time = match["created"] if created_time and float(created_time) + CACHE_TIME >= time.time(): match_item = match["item"] try: result = base64.b64decode(match_item) except: return None return result else: return else: return
def keep_alive(self): koding.dolog("staying alive") # keep connection alive self.totalTime = 0 self.currentTime = 0 if self.resume: koding.Create_Table("watched", self.tablespec) match = koding.Get_From_Table("watched", { "identifier": self.identifier, "season": self.season, "episode": self.episode }) if match: match = match[0] if match["currentTime"]: seconds = float(match["currentTime"]) xbmc.Player().seekTime(seconds) for i in range(0, 240): if self.isPlaying(): break xbmc.sleep(1000) while self.isPlaying(): try: self.totalTime = self.getTotalTime() self.currentTime = self.getTime() except: pass xbmc.sleep(2000) xbmc.sleep(5000)
def fetch_from_db(url): koding.reset_db() lastfm_plugin_spec = { "columns": { "url": "TEXT", "item": "TEXT", "created": "TEXT" }, "constraints": { "unique": "url" } } koding.Create_Table("lastfm_plugin", lastfm_plugin_spec) match = koding.Get_From_Table("lastfm_plugin", {"url": url}) if match: match = match[0] if not match["item"]: return None created_time = match["created"] if created_time and float(created_time) + float( CACHE_TIME) >= time.time(): match_item = match["item"].replace("'", "\"") return pickle.loads(match_item) else: return [] else: return []
def fetch_from_db(url): koding.reset_db() tmdb_plugin_spec = { "columns": { "url": "TEXT", "item": "TEXT", "created": "TEXT" }, "constraints": { "unique": "url" } } koding.Create_Table("tmdb_plugin", tmdb_plugin_spec) match = koding.Get_From_Table( "tmdb_plugin", {"url": url}) if match: match = match[0] if not match["item"]: return None created_time = match["created"] if created_time and float(created_time) + CACHE_TIME >= time.time(): match_item = match["item"].replace("'", "\"") try: match_item = match_item.encode('ascii', 'ignore') except: match_item = match_item.decode('utf-8').encode('ascii', 'ignore') return pickle.loads(match_item) else: return [] else: return []
def get_xml(link): import time xml_cache_spec = { "columns": { "xml": "TEXT", "link": "TEXT", "created": "TEXT", "changed": "TEXT" }, "constraints": { "unique": "link" } } koding.Create_Table("xml_cache", xml_cache_spec) url = replace_url(link) req = requests.get(url, verify=False) changed = req.headers["Last-Modified"] result = koding.Get_From_Table("xml_cache", {"link": link}) if result: if result[0]["changed"] == changed: return result[0]["xml"] else: koding.Remove_From_Table("xml_cache", {"link": link}) xml = req.content koding.Add_To_Table("xml_cache", { "xml": xml, "link": link, "created": time.time(), "changed": changed }) return xml
def CreateSession(apikey): Auth, SessionId, StatusMessageSession = BYBAPI.tmdb_session(apikey) columns = { "columns": { "auth": "TEXT", "sessionid": "TEXT", "statusmessage": "TEXT", "sessiontime": "TEXT" } } koding.Create_Table(table, columns) if Auth == False: BYB.Notify(title=str(addon_name), message='Failed to create session\nBecause ' + str(StatusMessageSession) + '\nPlease check settings if using own API Key') else: add = { "auth": Auth, "sessionid": SessionId, "statusmessage": StatusMessageSession, "sessiontime": DateTimeNow } koding.Add_To_Table(table, add) return Auth, SessionId, StatusMessageSession
def onPlayBackStopped(self): koding.dolog("playback stopped") if self.identifier == "0": return if not self.currentTime > 1: return koding.Create_Table("watched", self.tablespec) try: koding.Remove_From_Table( "watched", { "identifier": self.identifier, "season": self.season, "episode": self.episode }) except: pass koding.Add_To_Table( "watched", { "identifier": self.identifier, "season": self.season, "episode": self.episode, "watched": "0", "currentTime": self.currentTime }) return True
def save_view_mode(content): viewid = get_view_id() skin = xbmc.getSkinDir() koding.Create_Table("addonviews", view_spec) koding.Remove_From_Table("addonviews", {"skin": skin, "content": content}) koding.Add_To_Table("addonviews", { "skin": skin, "content": content, "viewid": viewid, }) icon = xbmcaddon.Addon().getAddonInfo('icon') xbmcgui.Dialog().notification(xbmcaddon.Addon().getAddonInfo('name'), "View set for %s" % content, icon)
def fetch_from_db(url): koding.reset_db() trakt_plugin_spec = { "columns": { "url": "TEXT", "item": "TEXT", "content_type": "TEXT", "created": "TEXT" }, "constraints": { "unique": "url" } } koding.Create_Table("trakt_plugin", trakt_plugin_spec) match = koding.Get_From_Table("trakt_plugin", {"url": url}) if match: match = match[0] if not match["item"]: return None created_time = match["created"] if "tmdb" in url: if created_time and float( created_time) + CACHE_TMDB_TIME >= time.time(): match_item = match["item"].replace("'", "\"") try: match_item = match_item.encode('ascii', 'ignore') except: match_item = match_item.decode('utf-8').encode( 'ascii', 'ignore') result = pickle.loads(match_item) if type(result) == str and result.startswith("{"): result = eval(result) return result if created_time and float(created_time) + float( CACHE_TIME) >= time.time(): match_item = match["item"].replace("'", "\"") content_type = match["content_type"] try: match_item = match_item.encode('ascii', 'ignore') except: match_item = match_item.decode('utf-8').encode( 'ascii', 'ignore') return (pickle.loads(match_item), content_type) else: return [] else: return []
# -*- coding: utf-8 -*-
def get_cached(self, url, cached=True): if not url.startswith("http"): return if __builtin__.BOB_BASE_DOMAIN not in url and "norestrictions" not in url: return requests.get(url).content xml_cache_spec = { "columns": { "url": "TEXT", "xml": "TEXT", "cache_time": "TEXT", "created": "TEXT" }, "constraints": { "unique": "url" } } koding.Create_Table("xml_cache", xml_cache_spec) if not cached: koding.dolog("uncached requested") response = requests.get(url, verify=False) xml = response.content response.close() else: match = koding.Get_From_Table("xml_cache", {"url": url}) if match: koding.dolog("match: " + repr(match)) match = match[0] created_time = float(match["created"]) cache_time = int(match["cache_time"]) koding.dolog("expire time: " + repr(created_time + cache_time)) koding.dolog("created_time: " + repr(created_time)) koding.dolog("now: " + repr(time.mktime(time.gmtime()))) if time.mktime(time.gmtime()) <= created_time + cache_time: koding.dolog("loading from cache, cache time not reached") return pickle.loads(match["xml"]) else: try: response = requests.get(url, verify=False, timeout=10) changed = response.headers["Last-Modified"] changed_struct = time.strptime( changed, "%a, %d %b %Y %H:%M:%S GMT") epoch_changed = int(time.mktime(changed_struct)) if epoch_changed < created_time: koding.dolog( "loading from cache, list not changed") #xml = pickle.loads(match["xml"]) xml = response.content response.close() else: koding.dolog("refreshing content") xml = response.content response.close() except Exception as e: koding.dolog("cache error: " + repr(e)) return pickle.loads(match["xml"]) else: koding.dolog("initial load") response = requests.get(url, verify=False) xml = response.content response.close() if not xml: xbmcgui.Dialog().notification(ADDON.getAddonInfo("name"), "Server under high load, try again") return "" info = JenItem(xml.split('<item>')[0].split('<dir>')[0]) cache_time = int(info.get("cache", 21600)) koding.dolog("cache_time: " + repr(cache_time)) created_time = time.mktime(time.gmtime()) try: koding.Remove_From_Table("xml_cache", { "url": url, }) except Exception, e: koding.dolog("Database error: " + repr(e))