def refresh_libraries_list(self, **kwargs): """ Refresh the PlexPy libraries list. """ data = pmsconnect.refresh_libraries() if data: self.result_type = 'success' else: self.result_type = 'failed' return data
def initialize(config_file): with INIT_LOCK: global CONFIG global CONFIG_FILE global _INITIALIZED global CURRENT_VERSION global LATEST_VERSION global UMASK global POLLING_FAILOVER CONFIG = plexpy.config.Config(config_file) CONFIG_FILE = config_file assert CONFIG is not None if _INITIALIZED: return False if CONFIG.HTTP_PORT < 21 or CONFIG.HTTP_PORT > 65535: plexpy.logger.warn( 'HTTP_PORT out of bounds: 21 < %s < 65535', CONFIG.HTTP_PORT) CONFIG.HTTP_PORT = 8181 if not CONFIG.HTTPS_CERT: CONFIG.HTTPS_CERT = os.path.join(DATA_DIR, 'server.crt') if not CONFIG.HTTPS_KEY: CONFIG.HTTPS_KEY = os.path.join(DATA_DIR, 'server.key') if not CONFIG.LOG_DIR: CONFIG.LOG_DIR = os.path.join(DATA_DIR, 'logs') if not os.path.exists(CONFIG.LOG_DIR): try: os.makedirs(CONFIG.LOG_DIR) except OSError: CONFIG.LOG_DIR = None if not QUIET: sys.stderr.write("Unable to create the log directory. " \ "Logging to screen only.\n") # Start the logger, disable console if needed logger.initLogger(console=not QUIET, log_dir=CONFIG.LOG_DIR, verbose=VERBOSE) if not CONFIG.BACKUP_DIR: CONFIG.BACKUP_DIR = os.path.join(DATA_DIR, 'backups') if not os.path.exists(CONFIG.BACKUP_DIR): try: os.makedirs(CONFIG.BACKUP_DIR) except OSError as e: logger.error("Could not create backup dir '%s': %s" % (CONFIG.BACKUP_DIR, e)) if not CONFIG.CACHE_DIR: CONFIG.CACHE_DIR = os.path.join(DATA_DIR, 'cache') if not os.path.exists(CONFIG.CACHE_DIR): try: os.makedirs(CONFIG.CACHE_DIR) except OSError as e: logger.error("Could not create cache dir '%s': %s" % (CONFIG.CACHE_DIR, e)) # Initialize the database logger.info('Checking to see if the database has all tables....') try: dbcheck() except Exception as e: logger.error("Can't connect to the database: %s" % e) # Check if PlexPy has a uuid if CONFIG.PMS_UUID == '' or not CONFIG.PMS_UUID: my_uuid = generate_uuid() CONFIG.__setattr__('PMS_UUID', my_uuid) CONFIG.write() # Get the currently installed version. Returns None, 'win32' or the git # hash. CURRENT_VERSION, CONFIG.GIT_BRANCH = versioncheck.getVersion() # Write current version to a file, so we know which version did work. # This allowes one to restore to that version. The idea is that if we # arrive here, most parts of PlexPy seem to work. if CURRENT_VERSION: version_lock_file = os.path.join(DATA_DIR, "version.lock") try: with open(version_lock_file, "w") as fp: fp.write(CURRENT_VERSION) except IOError as e: logger.error("Unable to write current version to file '%s': %s" % (version_lock_file, e)) # Check for new versions if CONFIG.CHECK_GITHUB_ON_STARTUP and CONFIG.CHECK_GITHUB: try: LATEST_VERSION = versioncheck.checkGithub() except: logger.exception("Unhandled exception") LATEST_VERSION = CURRENT_VERSION else: LATEST_VERSION = CURRENT_VERSION # Get the real PMS urls for SSL and remote access if CONFIG.PMS_TOKEN and CONFIG.PMS_IP and CONFIG.PMS_PORT: plextv.get_real_pms_url() pmsconnect.get_server_friendly_name() # Refresh the users list on startup if CONFIG.PMS_TOKEN and CONFIG.REFRESH_USERS_ON_STARTUP: plextv.refresh_users() # Refresh the libraries list on startup if CONFIG.PMS_IP and CONFIG.PMS_TOKEN and CONFIG.REFRESH_LIBRARIES_ON_STARTUP: pmsconnect.refresh_libraries() # Store the original umask UMASK = os.umask(0) os.umask(UMASK) _INITIALIZED = True return True
def get_details(self, section_id=None): default_return = { 'section_id': 0, 'section_name': 'Local', 'section_type': '', 'library_thumb': common.DEFAULT_COVER_THUMB, 'library_art': '', 'count': 0, 'parent_count': 0, 'child_count': 0, 'do_notify': 0, 'do_notify_created': 0, 'keep_history': 1 } if not section_id: return default_return def get_library_details(section_id=section_id): monitor_db = database.MonitorDatabase() try: if str(section_id).isdigit(): query = 'SELECT section_id, section_name, section_type, count, parent_count, child_count, ' \ 'thumb AS library_thumb, custom_thumb_url AS custom_thumb, art, ' \ 'do_notify, do_notify_created, keep_history ' \ 'FROM library_sections ' \ 'WHERE section_id = ? ' result = monitor_db.select(query, args=[section_id]) else: result = [] except Exception as e: logger.warn( u"PlexPy Libraries :: Unable to execute database query for get_details: %s." % e) result = [] library_details = {} if result: for item in result: if item['custom_thumb'] and item['custom_thumb'] != item[ 'library_thumb']: library_thumb = item['custom_thumb'] elif item['library_thumb']: library_thumb = item['library_thumb'] else: library_thumb = common.DEFAULT_COVER_THUMB library_details = { 'section_id': item['section_id'], 'section_name': item['section_name'], 'section_type': item['section_type'], 'library_thumb': library_thumb, 'library_art': item['art'], 'count': item['count'], 'parent_count': item['parent_count'], 'child_count': item['child_count'], 'do_notify': item['do_notify'], 'do_notify_created': item['do_notify_created'], 'keep_history': item['keep_history'] } return library_details library_details = get_library_details(section_id=section_id) if library_details: return library_details else: logger.warn( u"PlexPy Libraries :: Unable to retrieve library %s from database. Requesting library list refresh." % section_id) # Let's first refresh the libraries list to make sure the library isn't newly added and not in the db yet pmsconnect.refresh_libraries() library_details = get_library_details(section_id=section_id) if library_details: return library_details else: logger.warn( u"PlexPy Users :: Unable to retrieve library %s from database. Returning 'Local' library." % section_id) # If there is no library data we must return something return default_return
def refresh_libraries_list(self, **kwargs): """ Refresh the PlexPy libraries list.""" data = pmsconnect.refresh_libraries() self._api_result_type = 'success' if data else 'error' return data
def get_details(self, section_id=None): default_return = {'section_id': 0, 'section_name': 'Local', 'section_type': '', 'library_thumb': common.DEFAULT_COVER_THUMB, 'library_art': '', 'count': 0, 'parent_count': 0, 'child_count': 0, 'do_notify': 0, 'do_notify_created': 0, 'keep_history': 1 } if not section_id: return default_return def get_library_details(section_id=section_id): monitor_db = database.MonitorDatabase() try: if str(section_id).isdigit(): query = 'SELECT section_id, section_name, section_type, count, parent_count, child_count, ' \ 'thumb AS library_thumb, custom_thumb_url AS custom_thumb, art, ' \ 'do_notify, do_notify_created, keep_history ' \ 'FROM library_sections ' \ 'WHERE section_id = ? ' result = monitor_db.select(query, args=[section_id]) else: result = [] except Exception as e: logger.warn(u"PlexPy Libraries :: Unable to execute database query for get_details: %s." % e) result = [] library_details = {} if result: for item in result: if item['custom_thumb'] and item['custom_thumb'] != item['library_thumb']: library_thumb = item['custom_thumb'] elif item['library_thumb']: library_thumb = item['library_thumb'] else: library_thumb = common.DEFAULT_COVER_THUMB library_details = {'section_id': item['section_id'], 'section_name': item['section_name'], 'section_type': item['section_type'], 'library_thumb': library_thumb, 'library_art': item['art'], 'count': item['count'], 'parent_count': item['parent_count'], 'child_count': item['child_count'], 'do_notify': item['do_notify'], 'do_notify_created': item['do_notify_created'], 'keep_history': item['keep_history'] } return library_details library_details = get_library_details(section_id=section_id) if library_details: return library_details else: logger.warn(u"PlexPy Libraries :: Unable to retrieve library %s from database. Requesting library list refresh." % section_id) # Let's first refresh the libraries list to make sure the library isn't newly added and not in the db yet pmsconnect.refresh_libraries() library_details = get_library_details(section_id=section_id) if library_details: return library_details else: logger.warn(u"PlexPy Users :: Unable to retrieve library %s from database. Returning 'Local' library." % section_id) # If there is no library data we must return something return default_return