def _translate_discover_params(tmdb_type, params): lookup_keyword = None if params.get('with_id') and params.get('with_id') != 'False' else 'keyword' lookup_company = None if params.get('with_id') and params.get('with_id') != 'False' else 'company' lookup_person = None if params.get('with_id') and params.get('with_id') != 'False' else 'person' lookup_genre = None if params.get('with_id') and params.get('with_id') != 'False' else 'genre' with_separator = params.get('with_separator') if params.get('with_genres'): params['with_genres'] = TMDb().get_translated_list( split_items(params.get('with_genres')), lookup_genre, separator=with_separator) if params.get('without_genres'): params['without_genres'] = TMDb().get_translated_list( split_items(params.get('without_genres')), lookup_genre, separator=with_separator) if params.get('with_keywords'): params['with_keywords'] = TMDb().get_translated_list( split_items(params.get('with_keywords')), lookup_keyword, separator=with_separator) if params.get('without_keywords'): params['without_keywords'] = TMDb().get_translated_list( split_items(params.get('without_keywords')), lookup_keyword, separator=with_separator) if params.get('with_companies'): params['with_companies'] = TMDb().get_translated_list( split_items(params.get('with_companies')), lookup_company, separator='NONE') if params.get('with_people'): params['with_people'] = TMDb().get_translated_list( split_items(params.get('with_people')), lookup_person, separator=with_separator) if params.get('with_cast'): params['with_cast'] = TMDb().get_translated_list( split_items(params.get('with_cast')), lookup_person, separator=with_separator) if params.get('with_crew'): params['with_crew'] = TMDb().get_translated_list( split_items(params.get('with_crew')), lookup_person, separator=with_separator) if params.get('with_release_type'): params['with_release_type'] = TMDb().get_translated_list( split_items(params.get('with_release_type')), None, separator='OR') # Translate relative dates based upon today's date for i in RELATIVE_DATES: datecode = params.get(i, '') datecode = datecode.lower() if not datecode or all(x not in datecode for x in ['t-', 't+']): continue # No value or not a relative date so skip elif 't-' in datecode: days = try_int(datecode.replace('t-', '')) date = get_datetime_now() - get_timedelta(days=days) elif 't+' in datecode: days = try_int(datecode.replace('t+', '')) date = get_datetime_now() + get_timedelta(days=days) params[i] = date.strftime("%Y-%m-%d") return params
def get_detailed_item(tmdb_type, tmdb_id, season=None, episode=None, details=None): details = details or get_item_details(tmdb_type, tmdb_id, season, episode) if not details: return None item = defaultdict(lambda: '+') item['id'] = item['tmdb'] = tmdb_id item['imdb'] = details.unique_ids.get('imdb') item['tvdb'] = details.unique_ids.get('tvdb') item['trakt'] = details.unique_ids.get('trakt') item['slug'] = details.unique_ids.get('slug') item['season'] = season item['episode'] = episode item['originaltitle'] = details.infolabels.get('originaltitle') item['title'] = details.infolabels.get( 'tvshowtitle') or details.infolabels.get('title') item['showname'] = item['clearname'] = item['tvshowtitle'] = item.get( 'title') item['year'] = details.infolabels.get('year') item['name'] = u'{} ({})'.format(item.get('title'), item.get('year')) item['premiered'] = item['firstaired'] = item[ 'released'] = details.infolabels.get('premiered') item['plot'] = details.infolabels.get('plot') item['cast'] = item['actors'] = " / ".join( [i.get('name') for i in details.cast if i.get('name')]) item['thumbnail'] = details.art.get('thumb') item['poster'] = details.art.get('poster') item['fanart'] = details.art.get('fanart') item['now'] = get_datetime_now().strftime('%Y%m%d%H%M%S%f') if tmdb_type == 'tv' and season is not None and episode is not None: item['id'] = item['epid'] = item['eptvdb'] = item.get('tvdb') item['title'] = details.infolabels.get('title') # Set Episode Title item['name'] = u'{0} S{1:02d}E{2:02d}'.format(item.get('showname'), try_int(season), try_int(episode)) item['season'] = season item['episode'] = episode item['showpremiered'] = details.infoproperties.get('tvshow.premiered') item['showyear'] = details.infoproperties.get('tvshow.year') item['eptmdb'] = details.unique_ids.get('tmdb') item['epimdb'] = details.unique_ids.get('imdb') item['eptrakt'] = details.unique_ids.get('trakt') item['epslug'] = details.unique_ids.get('slug') item['tmdb'] = details.unique_ids.get('tvshow.tmdb') item['imdb'] = details.unique_ids.get('tvshow.imdb') item['tvdb'] = details.unique_ids.get('tvshow.tvdb') item['trakt'] = details.unique_ids.get('tvshow.trakt') item['slug'] = details.unique_ids.get('tvshow.slug') return _url_encode_item(item)
def set(self, endpoint, data, checksum="", cache_days=30): ''' set data in cache ''' with self.busy_tasks(u'set.{}'.format(endpoint)): checksum = self._get_checksum(checksum) expires = self._get_timestamp(get_datetime_now() + get_timedelta(days=cache_days)) self._set_mem_cache(endpoint, checksum, expires, data) if not self._mem_only: self._set_db_cache(endpoint, checksum, expires, data)
def get_daily_list(self, export_list, sorting=None, reverse=False): if not export_list: return datestamp = get_datetime_now() - get_timedelta(days=2) datestamp = datestamp.strftime("%m_%d_%Y") # Pickle results rather than cache due to being such a large list return use_pickle( self._get_downloaded_list, export_list=export_list, sorting=sorting, reverse=reverse, datestamp=datestamp, cache_name=u'TMDb.Downloaded.List.v2.{}.{}.{}'.format( export_list, sorting, reverse, datestamp))
def check_cleanup(self): '''check if cleanup is needed - public method, may be called by calling addon''' if self._mem_only: return cur_time = get_datetime_now() lastexecuted = self._win.getProperty(u"{}.clean.lastexecuted".format( self._sc_name)) if not lastexecuted: self._win.setProperty( u"{}.clean.lastexecuted".format(self._sc_name), repr(tuple(cur_time.timetuple()[:6]))) elif (get_datetime_datetime(*eval(lastexecuted)) + get_timedelta(hours=self._auto_clean_interval)) < cur_time: self._do_cleanup()
def set_pickle(my_object, cache_name, cache_days=14): if not my_object: return cache_name = get_pickle_name(cache_name) if not cache_name: return timestamp = get_datetime_now() + get_timedelta(days=cache_days) cache_obj = { 'my_object': my_object, 'expires': timestamp.strftime("%Y-%m-%dT%H:%M:%S") } with open(os.path.join(_get_write_path('pickle'), cache_name), 'wb') as file: _pickle.dump(cache_obj, file) return my_object
def get(self, endpoint, checksum=""): ''' get object from cache and return the results endpoint: the (unique) name of the cache object as reference checkum: optional argument to check if the checksum in the cacheobject matches the checkum provided ''' checksum = self._get_checksum(checksum) cur_time = self._get_timestamp(get_datetime_now()) result = self._get_mem_cache(endpoint, checksum, cur_time) # Try from memory first if result is not None or self._mem_only: return result return self._get_db_cache( endpoint, checksum, cur_time) # Fallback to checking database if not in memory
def _do_delete(self): '''perform cleanup task''' if self._exit or self._monitor.abortRequested(): return with self.busy_tasks(__name__): cur_time = get_datetime_now() kodi_log("CACHE: Deleting {}...".format(self._sc_name)) self._win.setProperty(u"{}.cleanbusy".format(self._sc_name), "busy") query = 'DELETE FROM simplecache' self._execute_sql(query) self._execute_sql("VACUUM") # Washup self._win.setProperty(u"{}.clean.lastexecuted".format(self._sc_name), repr(tuple(cur_time.timetuple()[:6]))) self._win.clearProperty(u"{}.cleanbusy".format(self._sc_name)) kodi_log("CACHE: Delete {} done".format(self._sc_name))
def run(self): self.xbmc_monitor.waitForAbort(600) # Wait 10 minutes before doing updates to give boot time if self.xbmc_monitor.abortRequested(): del self.xbmc_monitor return self.next_time = get_datetime_combine(get_datetime_today(), get_datetime_time(try_int(self.update_hour))) # Get today at hour self.last_time = xbmc.getInfoLabel('Skin.String(TMDbHelper.AutoUpdate.LastTime)') # Get last update self.last_time = convert_timestamp(self.last_time) if self.last_time else None if self.last_time and self.last_time > self.next_time: self.next_time += get_timedelta(hours=24) # Already updated today so set for tomorrow while not self.xbmc_monitor.abortRequested() and not self.exit and self.poll_time: if ADDON.getSettingBool('library_autoupdate'): if get_datetime_now() > self.next_time: # Scheduled time has past so lets update xbmc.executebuiltin('RunScript(plugin.video.themoviedb.helper,library_autoupdate)') xbmc.executebuiltin('Skin.SetString(TMDbHelper.AutoUpdate.LastTime,{})'.format(get_datetime_now().strftime("%Y-%m-%dT%H:%M:%S"))) self.next_time += get_timedelta(hours=24) # Set next update for tomorrow self.xbmc_monitor.waitForAbort(self.poll_time) del self.xbmc_monitor
def _do_cleanup(self, force=False): '''perform cleanup task''' if self._exit or self._monitor.abortRequested(): return with self.busy_tasks(__name__): cur_time = get_datetime_now() cur_timestamp = self._get_timestamp(cur_time) kodi_log("CACHE: Running cleanup...") if self._win.getProperty(u"{}.cleanbusy".format(self._sc_name)): return self._win.setProperty(u"{}.cleanbusy".format(self._sc_name), "busy") query = "SELECT id, expires FROM simplecache" for cache_data in self._execute_sql(query).fetchall(): cache_id = cache_data[0] cache_expires = cache_data[1] if self._exit or self._monitor.abortRequested(): return # always cleanup all memory objects on each interval self._win.clearProperty(cache_id) # clean up db cache object only if expired if force or cache_expires < cur_timestamp: query = 'DELETE FROM simplecache WHERE id = ?' self._execute_sql(query, (cache_id, )) kodi_log(u"CACHE: delete from db {}".format(cache_id)) # compact db self._execute_sql("VACUUM") # Washup self._win.setProperty(u"{}.clean.lastexecuted".format(self._sc_name), repr(tuple(cur_time.timetuple()[:6]))) self._win.clearProperty(u"{}.cleanbusy".format(self._sc_name)) kodi_log("CACHE: Auto cleanup done")