<commit_before>package org.lodder.subtools.multisubdownloader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.lodder.subtools.multisubdownloader.framework.Container;
import org.lodder.subtools.multisubdownloader.gui.dialog.progress.search.CLISearchProgress;
import org.lodder.subtools.multisubdownloader.lib.Actions;
import org.lodder.subtools.multisubdownloader.lib.Info;
import org.lodder.subtools.multisubdownloader.lib.ReleaseFactory;
import org.lodder.subtools.multisubdownloader.lib.control.subtitles.Filtering;
import org.lodder.subtools.multisubdownloader.settings.SettingsControl;
import org.lodder.subtools.multisubdownloader.subtitleproviders.SubtitleProvider;
import org.lodder.subtools.multisubdownloader.subtitleproviders.SubtitleProviderStore;
import org.lodder.subtools.multisubdownloader.workers.SearchHandler;
import org.lodder.subtools.multisubdownloader.workers.SearchManager;
import org.lodder.subtools.sublibrary.logging.Level;
import org.lodder.subtools.sublibrary.logging.Listener;
import org.lodder.subtools.sublibrary.logging.Logger;
import org.lodder.subtools.sublibrary.model.Release;
import org.lodder.subtools.sublibrary.model.Subtitle;

public class CommandLine implements Listener, SearchHandler {

  private final SettingsControl prefctrl;
  private final Container app;
  private boolean recursive = false;
  private String languagecode = "";
  private boolean force = false;
  private File folder = null;
  private boolean downloadall = false;
  private final Actions actions;
  private boolean subtitleSelectionDialog = false;
  private List<Release> releases;
  private SearchManager searchManager;
  private ReleaseFactory releaseFactory;
  private Filtering filtering;
  private CLISearchProgress searchProgress;

  public CommandLine(final SettingsControl prefctrl, Container app) {
    Logger.instance.addListener(this);
    this.app = app;
    this.prefctrl = prefctrl;
    try {
      if (this.prefctrl.getSettings().isAutoUpdateMapping()) {
        this.prefctrl.updateMappingFromOnline();
      }
    } catch (Throwable e) {
      Logger.instance.error(Logger.stack2String(e));
    }
    actions = new Actions(prefctrl.getSettings(), true);
  }

  public void setReleaseFactory(ReleaseFactory releaseFactory) {
    this.releaseFactory = releaseFactory;
  }

  public void setFiltering(Filtering filtering){
    this.filtering = filtering;
  }

  private void search() throws Exception {
    List<File> folders = new ArrayList<File>();
    if (folder == null) {
      folders.addAll(prefctrl.getSettings().getDefaultIncomingFolders());
    } else {
      folders.add(folder);
    }

    List<File> files = new ArrayList<>();
    for (File folder : folders) {
      files.addAll(actions.getFileListing(folder, recursive, languagecode, force));
    }
    Logger.instance.debug("# Files found to process: " + files.size());

    releases = new ArrayList<>();
    for (File file : files) {
      Release release = releaseFactory.createRelease(file);
      if (release == null) continue;

      releases.add(release);
    }

    SubtitleProviderStore subtitleProviderStore =
        (SubtitleProviderStore) this.app.make("SubtitleProviderStore");

    searchManager = new SearchManager(this.prefctrl.getSettings());
    searchManager.setLanguage(this.getLanguagecode());

    for (SubtitleProvider subtitleProvider : subtitleProviderStore.getAllProviders()) {
      if (!this.prefctrl.getSettings().isSerieSource(subtitleProvider.getName())) continue;

      searchManager.addProvider(subtitleProvider);
    }

    for (Release release : releases) {
      searchManager.addRelease(release);
    }

    searchManager.onFound(this);

    searchProgress = new CLISearchProgress();
    /* If debug enabled, show more info */
    if (Logger.instance.getLogLevel().intValue() <= Level.DEBUG.intValue()) {
      searchProgress.setVerbose(true);
    }

    searchManager.setProgressListener(searchProgress);

    searchManager.start();
  }

  @Override
  public void onFound(Release release, List<Subtitle> subtitles) {
    if (filtering != null) subtitles = filtering.getFiltered(subtitles, release);

    release.getMatchingSubs().addAll(subtitles);
    if (searchManager.getProgress() < 100) return;

    /* stop printing progress */
    searchProgress.disable();<commit_msg>Disable verbose progress<commit_after>package org.lodder.subtools.multisubdownloader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.lodder.subtools.multisubdownloader.framework.Container;
import org.lodder.subtools.multisubdownloader.gui.dialog.progress.search.CLISearchProgress;
import org.lodder.subtools.multisubdownloader.lib.Actions;
import org.lodder.subtools.multisubdownloader.lib.Info;
import org.lodder.subtools.multisubdownloader.lib.ReleaseFactory;
import org.lodder.subtools.multisubdownloader.lib.control.subtitles.Filtering;
import org.lodder.subtools.multisubdownloader.settings.SettingsControl;
import org.lodder.subtools.multisubdownloader.subtitleproviders.SubtitleProvider;
import org.lodder.subtools.multisubdownloader.subtitleproviders.SubtitleProviderStore;
import org.lodder.subtools.multisubdownloader.workers.SearchHandler;
import org.lodder.subtools.multisubdownloader.workers.SearchManager;
import org.lodder.subtools.sublibrary.logging.Listener;
import org.lodder.subtools.sublibrary.logging.Logger;
import org.lodder.subtools.sublibrary.model.Release;
import org.lodder.subtools.sublibrary.model.Subtitle;

public class CommandLine implements Listener, SearchHandler {

  private final SettingsControl prefctrl;
  private final Container app;
  private boolean recursive = false;
  private String languagecode = "";
  private boolean force = false;
  private File folder = null;
  private boolean downloadall = false;
  private final Actions actions;
  private boolean subtitleSelectionDialog = false;
  private List<Release> releases;
  private SearchManager searchManager;
  private ReleaseFactory releaseFactory;
  private Filtering filtering;
  private CLISearchProgress searchProgress;

  public CommandLine(final SettingsControl prefctrl, Container app) {
    Logger.instance.addListener(this);
    this.app = app;
    this.prefctrl = prefctrl;
    try {
      if (this.prefctrl.getSettings().isAutoUpdateMapping()) {
        this.prefctrl.updateMappingFromOnline();
      }
    } catch (Throwable e) {
      Logger.instance.error(Logger.stack2String(e));
    }
    actions = new Actions(prefctrl.getSettings(), true);
  }

  public void setReleaseFactory(ReleaseFactory releaseFactory) {
    this.releaseFactory = releaseFactory;
  }

  public void setFiltering(Filtering filtering){
    this.filtering = filtering;
  }

  private void search() throws Exception {
    List<File> folders = new ArrayList<File>();
    if (folder == null) {
      folders.addAll(prefctrl.getSettings().getDefaultIncomingFolders());
    } else {
      folders.add(folder);
    }

    List<File> files = new ArrayList<>();
    for (File folder : folders) {
      files.addAll(actions.getFileListing(folder, recursive, languagecode, force));
    }
    Logger.instance.debug("# Files found to process: " + files.size());

    releases = new ArrayList<>();
    for (File file : files) {
      Release release = releaseFactory.createRelease(file);
      if (release == null) continue;

      releases.add(release);
    }

    SubtitleProviderStore subtitleProviderStore =
        (SubtitleProviderStore) this.app.make("SubtitleProviderStore");

    searchManager = new SearchManager(this.prefctrl.getSettings());
    searchManager.setLanguage(this.getLanguagecode());

    for (SubtitleProvider subtitleProvider : subtitleProviderStore.getAllProviders()) {
      if (!this.prefctrl.getSettings().isSerieSource(subtitleProvider.getName())) continue;

      searchManager.addProvider(subtitleProvider);
    }

    for (Release release : releases) {
      searchManager.addRelease(release);
    }

    searchManager.onFound(this);

    searchProgress = new CLISearchProgress();
    //TODO: check if verbose is wanted
    searchProgress.setVerbose(false);

    searchManager.setProgressListener(searchProgress);

    searchManager.start();
  }

  @Override
  public void onFound(Release release, List<Subtitle> subtitles) {
    if (filtering != null) subtitles = filtering.getFiltered(subtitles, release);

    release.getMatchingSubs().addAll(subtitles);
    if (searchManager.getProgress() < 100) return;

    /* stop printing progress */
    searchProgress.disable();
{'fraction_non_alphanumeric': 0.10148312507298844, 'fraction_numerical': 0.0009342520144809063, 'mean_word_length': 4.637919684002633, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '538234', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2407, 'n_words': 472}