Exemplo n.º 1
0
 @Override
 public void execute() throws Exception {
   Path destinationFile = task.getDestinationFile().toAbsolutePath();
   if (!Files.isReadable(destinationFile) || Files.size(destinationFile) == 0) {
     String contentType =
         HTTPClient.getInstance()
             .downloadToFile(
                 new WebResource(task.getUrl(), task.getReferer()), destinationFile, 0);
     if (task.isImage() && !StringUtils.startsWith(contentType, "image/")) {
       ErrorManager.getInstance().reportError(String.format("%s is not an image", task.getUrl()));
       Files.delete(destinationFile);
     }
   }
 }
Exemplo n.º 2
0
public abstract class SubtitlesFinder implements Reconfigurable, Enableable {

  private Semaphore semaphore = new Semaphore(1);

  @Configurable(defaultValue = "true")
  private boolean enabled;

  private boolean ready;

  @Override
  public boolean isEnabled() {
    return enabled;
  }

  public void setEnabled(boolean enabled) {
    this.enabled = enabled;
  }

  protected HTTPClient client = HTTPClient.getInstance();

  @Override
  public void reconfigure() {
    this.ready = false;
    try {
      try {
        customInit();
      } catch (Exception e) {
        ErrorManager.getInstance().reportThrowable(e);
        this.enabled = false;
      }
    } finally {
      this.ready = true;
    }
  }

  public void customInit() throws Exception {}

  public void warn(String message) {
    ErrorManager.getInstance()
        .reportWarning(String.format("%s : %s", getClass().getSimpleName(), message));
  }

  public abstract FinderQuality getQuality();

  public boolean isReady() {
    return ready;
  }

  protected abstract RemoteSubTitles downloadSubtitle(VideoDetails details, Language language)
      throws Exception;

  public RemoteSubTitles findSubtitles(VideoDetails details, Language language) throws Exception {
    while (!isReady()) {
      // currently being reconfigured
      Thread.sleep(500);
    }
    semaphore.acquire();
    try {
      return downloadSubtitle(details, language);
    } finally {
      semaphore.release();
    }
  }
}