def client_pushed(): if request.content_type == 'application/json': data = request.get_json(silent=True) else: data = request.form.to_dict() if not data: logger.error("Invalid scan request from: %r", request.remote_addr) abort(400) logger.debug("Client %r request dump:\n%s", request.remote_addr, json.dumps(data, indent=4, sort_keys=True)) if ('eventType' in data and data['eventType'] == 'Test') or ( 'EventType' in data and data['EventType'] == 'Test'): logger.info("Client %r made a test request, event: '%s'", request.remote_addr, 'Test') elif 'eventType' in data and data['eventType'] == 'Manual': logger.info("Client %r made a manual scan request for: '%s'", request.remote_addr, data['filepath']) final_path = utils.map_pushed_path(config, data['filepath']) if start_scan(final_path, 'manual', 'Manual'): return "'%s' was added to scan backlog." % final_path else: return "Error adding '%s' to scan backlog." % data['filepath'] elif 'Movie' in data: logger.info("Client %r scan request for movie: '%s', event: '%s'", request.remote_addr, data['Movie']['FilePath'], data['EventType']) final_path = utils.map_pushed_path(config, data['Movie']['FilePath']) start_scan(final_path, 'radarr', data['EventType']) elif 'Series' in data: logger.info("Client %r scan request for series: '%s', event: '%s'", request.remote_addr, data['Series']['Path'], data['EventType']) final_path = utils.map_pushed_path(config, data['Series']['Path']) start_scan(final_path, 'sonarr', data['EventType']) elif 'series' and 'episodeFile' in data: # new sonarr webhook path = os.path.join(data['series']['path'], data['episodeFile']['relativePath']) logger.info("Client %r scan request for series: '%s', event: '%s'", request.remote_addr, path, "Upgrade" if data['isUpgrade'] else data['eventType']) final_path = utils.map_pushed_path(config, path) start_scan(final_path, 'sonarr_dev', "Upgrade" if data['isUpgrade'] else data['eventType']) else: logger.error("Unknown scan request from: %r", request.remote_addr) abort(400) return "OK"
def process_google_changes(items_added): new_file_paths = [] # process items added if not items_added: return True for file_id, file_paths in items_added.items(): for file_path in file_paths: if file_path in new_file_paths: continue new_file_paths.append(file_path) # remove files that already exist in the plex database removed_rejected_exists = utils.remove_files_exist_in_plex_database( new_file_paths, conf.configs['PLEX_DATABASE_PATH']) if removed_rejected_exists: logger.info( "Rejected %d file(s) from Google Drive changes for already being in Plex!", removed_rejected_exists) # process the file_paths list if len(new_file_paths): logger.info( "Proceeding with scan of %d file(s) from Google Drive changes: %s", len(new_file_paths), new_file_paths) # loop each file, remapping and starting a scan thread for file_path in new_file_paths: final_path = utils.map_pushed_path(conf.configs, file_path) start_scan(final_path, 'Google Drive', 'Download') return True
def client_pushed(): if request.content_type == 'application/json': data = request.get_json(silent=True) else: data = request.form.to_dict() if not data: logger.error("Invalid scan request from: %r", request.remote_addr) abort(400) logger.debug("Client %r request dump:\n%s", request.remote_addr, json.dumps(data, indent=4, sort_keys=True)) if ('eventType' in data and data['eventType'] == 'Test') or ( 'EventType' in data and data['EventType'] == 'Test'): logger.info("Client %r made a test request, event: '%s'", request.remote_addr, 'Test') elif 'eventType' in data and data['eventType'] == 'Manual': logger.info("Client %r made a manual scan request for: '%s'", request.remote_addr, data['filepath']) final_path = utils.map_pushed_path(conf.configs, data['filepath']) # ignore this request? ignore, ignore_match = utils.should_ignore(final_path, conf.configs) if ignore: logger.info( "Ignored scan request for '%s' because '%s' was matched from SERVER_IGNORE_LIST", final_path, ignore_match) return "Ignoring scan request because %s was matched from your SERVER_IGNORE_LIST" % ignore_match if start_scan(final_path, 'Manual', 'Manual'): return """<!DOCTYPE html> <html lang="en"> <head> <title>Plex Autoscan</title> <meta charset="utf-8"> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row justify-content-md-center"> <div class="col-md-auto text-center" style="padding-top: 10px;"> <h1 style="margin: 10px; margin-bottom: 150px;">Plex Autoscan</h1> <h3 class="text-left" style="margin: 10px;">Success</h3> <div class="alert alert-info" role="alert"> <code style="color: #000;">'{0}'</code> was added to scan queue. </div> </div> </div> </div> </body> </html>""".format(final_path) else: return """<!DOCTYPE html> <html lang="en"> <head> <title>Plex Autoscan</title> <meta charset="utf-8"> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row justify-content-md-center"> <div class="col-md-auto text-center" style="padding-top: 10px;"> <h1 style="margin: 10px; margin-bottom: 150px;">Plex Autoscan</h1> <h3 class="text-left" style="margin: 10px;">Error</h3> <div class="alert alert-danger" role="alert"> <code style="color: #000;">'{0}'</code> has already been added to the scan queue. </div> </div> </div> </div> </body> </html>""".format(data['filepath']) elif 'series' in data and 'eventType' in data and data[ 'eventType'] == 'Rename' and 'path' in data['series']: # sonarr Rename webhook logger.info( "Client %r scan request for series: '%s', event: '%s'", request.remote_addr, data['series']['path'], "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) final_path = utils.map_pushed_path(conf.configs, data['series']['path']) start_scan( final_path, 'Sonarr', "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) elif 'movie' in data and 'eventType' in data and data[ 'eventType'] == 'Rename' and 'folderPath' in data['movie']: # radarr Rename webhook logger.info( "Client %r scan request for movie: '%s', event: '%s'", request.remote_addr, data['movie']['folderPath'], "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) final_path = utils.map_pushed_path(conf.configs, data['movie']['folderPath']) start_scan( final_path, 'Radarr', "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) elif 'movie' in data and 'movieFile' in data and 'folderPath' in data['movie'] and \ 'relativePath' in data['movieFile'] and 'eventType' in data: # radarr download/upgrade webhook path = os.path.join(data['movie']['folderPath'], data['movieFile']['relativePath']) logger.info( "Client %r scan request for movie: '%s', event: '%s'", request.remote_addr, path, "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) final_path = utils.map_pushed_path(conf.configs, path) start_scan( final_path, 'Radarr', "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) elif 'series' in data and 'episodeFile' in data and 'eventType' in data: # sonarr download/upgrade webhook path = os.path.join(data['series']['path'], data['episodeFile']['relativePath']) logger.info( "Client %r scan request for series: '%s', event: '%s'", request.remote_addr, path, "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) final_path = utils.map_pushed_path(conf.configs, path) start_scan( final_path, 'Sonarr', "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) elif 'artist' in data and 'trackFile' in data and 'eventType' in data: # lidarr download/upgrade webhook path = os.path.join(data['artist']['path'], data['trackFile']['relativePath']) logger.info( "Client %r scan request for album track: '%s', event: '%s'", request.remote_addr, path, "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) final_path = utils.map_pushed_path(conf.configs, path) start_scan( final_path, 'Lidarr', "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) else: logger.error("Unknown scan request from: %r", request.remote_addr) abort(400) return "OK"
def process_google_changes(changes): global google file_paths = [] # convert changes to file paths list for change in changes: logger.debug("Processing Google change: %s", change) if 'file' in change and 'fileId' in change: # dont consider trashed/removed events for processing if ('trashed' in change['file'] and change['file']['trashed']) or ( 'removed' in change and change['removed']): # remove item from cache if google.remove_item_from_cache(change['fileId']): logger.info("Removed '%s' from cache: %s", change['fileId'], change['file']['name']) continue # we always want to add changes to the cache so renames etc can be reflected inside the cache google.add_item_to_cache(change['fileId'], change['file']['name'], [] if 'parents' not in change['file'] else change['file']['parents']) # dont process folder events if 'mimeType' in change['file'] and 'vnd.google-apps.folder' in change['file']['mimeType']: # ignore this change as we dont want to scan folders continue # get this files paths success, item_paths = google.get_id_file_paths(change['fileId'], change['file']['teamDriveId'] if 'teamDriveId' in change[ 'file'] else None) if success and len(item_paths): file_paths.extend(item_paths) elif 'teamDrive' in change and 'teamDriveId' in change: # this is a teamdrive change # dont consider trashed/removed events for processing if 'removed' in change and change['removed']: # remove item from cache if google.remove_item_from_cache(change['teamDriveId']): logger.info("Removed teamDrive '%s' from cache: %s", change['teamDriveId'], change['teamDrive']['name'] if 'name' in change['teamDrive'] else 'Unknown teamDrive') continue if 'id' in change['teamDrive'] and 'name' in change['teamDrive']: # we always want to add changes to the cache so renames etc can be reflected inside the cache google.add_item_to_cache(change['teamDrive']['id'], change['teamDrive']['name'], []) continue # always dump the cache after running changes google.dump_cache() # remove files that are not of an allowed extension type removed_rejected_extensions = 0 for file_path in copy(file_paths): if not utils.allowed_scan_extension(file_path, conf.configs['GDRIVE']['SCAN_EXTENSIONS']): # this file did not have an allowed extension, remove it file_paths.remove(file_path) removed_rejected_extensions += 1 if removed_rejected_extensions: logger.info("Ignored %d file(s) from Google Drive changes for disallowed file extensions", removed_rejected_extensions) # remove files that are in the ignore paths list removed_rejected_paths = 0 for file_path in copy(file_paths): for ignore_path in conf.configs['GDRIVE']['IGNORE_PATHS']: if file_path.lower().startswith(ignore_path.lower()): # this file was from an ignored path, remove it file_paths.remove(file_path) removed_rejected_paths += 1 if removed_rejected_paths: logger.info("Ignored %d file(s) from Google Drive changes for disallowed file paths", removed_rejected_paths) # remove files that already exist in the plex database removed_rejected_exists = utils.remove_files_exist_in_plex_database(file_paths, conf.configs['PLEX_DATABASE_PATH']) if removed_rejected_exists: logger.info("Ignored %d file(s) from Google Drive changes for already being in Plex!", removed_rejected_exists) # process the file_paths list if len(file_paths): logger.info("Proceeding with scan of %d file(s) from Google Drive changes: %s", len(file_paths), file_paths) # loop each file, remapping and starting a scan thread for file_path in file_paths: final_path = utils.map_pushed_path(conf.configs, file_path) start_scan(final_path, 'Google Drive', 'Download') return True
def client_pushed(): if request.content_type == 'application/json': data = request.get_json(silent=True) else: data = request.form.to_dict() if not data: logger.error("Invalid scan request from: %r", request.remote_addr) abort(400) logger.debug("Client %r request dump:\n%s", request.remote_addr, json.dumps(data, indent=4, sort_keys=True)) if ('eventType' in data and data['eventType'] == 'Test') or ( 'EventType' in data and data['EventType'] == 'Test'): logger.info("Client %r made a test request, event: '%s'", request.remote_addr, 'Test') elif 'eventType' in data and data['eventType'] == 'Manual': logger.info("Client %r made a manual scan request for: '%s'", request.remote_addr, data['filepath']) final_path = utils.map_pushed_path(conf.configs, data['filepath']) # ignore this request? ignore, ignore_match = utils.should_ignore(final_path, conf.configs) if ignore: logger.info( "Ignored scan request for '%s' because '%s' was matched from SERVER_IGNORE_LIST", final_path, ignore_match) return "Ignoring scan request because %s was matched from your SERVER_IGNORE_LIST" % ignore_match if start_scan(final_path, 'Manual', 'Manual'): return "'%s' was added to scan backlog." % final_path else: return "Error adding '%s' to scan backlog." % data['filepath'] elif 'series' in data and 'eventType' in data and data[ 'eventType'] == 'Rename' and 'path' in data['series']: # sonarr Rename webhook logger.info( "Client %r scan request for series: '%s', event: '%s'", request.remote_addr, data['series']['path'], "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) final_path = utils.map_pushed_path(conf.configs, data['series']['path']) start_scan( final_path, 'Sonarr', "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) elif 'movie' in data and 'eventType' in data and data[ 'eventType'] == 'Rename' and 'folderPath' in data['movie']: # radarr Rename webhook logger.info( "Client %r scan request for movie: '%s', event: '%s'", request.remote_addr, data['movie']['folderPath'], "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) final_path = utils.map_pushed_path(conf.configs, data['movie']['folderPath']) start_scan( final_path, 'Radarr', "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) elif 'movie' in data and 'movieFile' in data and 'folderPath' in data['movie'] and \ 'relativePath' in data['movieFile'] and 'eventType' in data: # radarr download/upgrade webhook path = os.path.join(data['movie']['folderPath'], data['movieFile']['relativePath']) logger.info( "Client %r scan request for movie: '%s', event: '%s'", request.remote_addr, path, "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) final_path = utils.map_pushed_path(conf.configs, path) start_scan( final_path, 'Radarr', "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) elif 'series' in data and 'episodeFile' in data and 'eventType' in data: # sonarr download/upgrade webhook path = os.path.join(data['series']['path'], data['episodeFile']['relativePath']) logger.info( "Client %r scan request for series: '%s', event: '%s'", request.remote_addr, path, "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) final_path = utils.map_pushed_path(conf.configs, path) start_scan( final_path, 'Sonarr', "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) elif 'artist' in data and 'trackFile' in data and 'eventType' in data: # lidarr download/upgrade webhook path = os.path.join(data['artist']['path'], data['trackFile']['relativePath']) logger.info( "Client %r scan request for album track: '%s', event: '%s'", request.remote_addr, path, "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) final_path = utils.map_pushed_path(conf.configs, path) start_scan( final_path, 'Lidarr', "Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType']) else: logger.error("Unknown scan request from: %r", request.remote_addr) abort(400) return "OK"