def thread_google_monitor(): global google logger.info("Starting Google Drive changes monitor in 30 seconds...") time.sleep(30) # load access tokens google = Gdrive(conf.configs, conf.settings['tokenfile'], conf.settings['cachefile']) if not google.first_run(): logger.error("Failed to retrieve Google Drive access tokens...") exit(1) else: logger.info("Google Drive access tokens were successfully loaded") try: logger.info("Google Drive changes monitor started") while True: if not google.token['page_token']: # we have no page_token, likely this is first run, lets retrieve a starting page token if not google.get_changes_first_page_token(): logger.error("Failed to retrieve starting Google Drive changes page token...") return else: logger.info("Retrieved starting Google Drive changes page token: %s", google.token['page_token']) time.sleep(conf.configs['GDRIVE']['POLL_INTERVAL']) # get page changes changes = [] changes_attempts = 0 while True: try: success, page = google.get_changes() changes_attempts = 0 except Exception: changes_attempts += 1 logger.exception( "Exception occurred while polling Google Drive for changes on page %s on attempt %d/12: ", str(google.token['page_token']), changes_attempts) if changes_attempts < 12: logger.warning("Sleeping for 5 minutes before trying to poll Google Drive for changes again...") time.sleep(60 * 5) continue else: logger.error("Failed to poll Google Drive changes after 12 consecutive attempts, " "aborting...") return if not success: logger.error("Failed to retrieve Google Drive changes for page: %s, aborting...", str(google.token['page_token'])) return else: # successfully retrieved some changes if 'changes' in page: changes.extend(page['changes']) # page logic if page is not None and 'nextPageToken' in page: # there are more pages to retrieve logger.debug("There are more Google Drive changes pages to retrieve, retrieving next page...") continue elif page is not None and 'newStartPageToken' in page: # there are no more pages to retrieve break else: logger.error("There was an unexpected outcome when polling Google Drive for changes, " "aborting future polls...") return # process changes if len(changes): logger.info("There's %d Google Drive change(s) to process", len(changes)) process_google_changes(changes) # sleep before polling for changes again time.sleep(conf.configs['GDRIVE']['POLL_INTERVAL']) except Exception: logger.exception("Fatal Exception occurred while monitoring Google Drive for changes, page = %s: ", google.token['page_token'])
logger.info(""" PG Scan Started! """) if conf.args['cmd'] == 'sections': plex.show_sections(conf.configs) exit(0) elif conf.args['cmd'] == 'update_sections': plex.updateSectionMappings(conf) elif conf.args['cmd'] == 'authorize': if not conf.configs['GDRIVE']['ENABLED']: logger.error("You must enable the ENABLED setting in the GDRIVE config section...") exit(1) else: google = Gdrive(conf.configs, conf.settings['tokenfile'], conf.settings['cachefile']) if not google.first_run(): logger.error("Failed to retrieve access tokens...") exit(1) else: logger.info("Access tokens were successfully retrieved!") exit(0) elif conf.args['cmd'] == 'server': if conf.configs['SERVER_USE_SQLITE']: start_queue_reloader() if conf.configs['GDRIVE']['ENABLED']: if not os.path.exists(conf.settings['tokenfile']): logger.error("You must authorize your Google Drive account with the authorize option...") exit(1) start_google_monitor()