Пример #1
0
  private File getDestinationDirectory() {
    String show = ShowStore.getShow(showName).getName();
    String destPath = userPrefs.getDestinationDirectory().getAbsolutePath() + File.separatorChar;
    destPath = destPath + StringUtils.sanitiseTitle(show) + File.separatorChar;

    // Defect #50: Only add the 'season #' folder if set, otherwise put files in showname root
    if (StringUtils.isNotBlank(userPrefs.getSeasonPrefix())) {
      destPath =
          destPath
              + userPrefs.getSeasonPrefix()
              + (userPrefs.isSeasonPrefixLeadingZero() && this.seasonNumber < 9 ? "0" : "")
              + this.seasonNumber
              + File.separatorChar;
    }
    return new File(destPath);
  }
  /**
   * Download the URL and return as a String. Gzip handling from http://goo.gl/J88WG
   *
   * @param url the URL to download
   * @return String of the URL contents
   * @throws IOException when there is an error connecting or reading the URL
   */
  public String downloadUrl(URL url) throws TVRenamerIOException {
    InputStream inputStream = null;
    StringBuilder contents = new StringBuilder();

    try {
      if (url != null) {
        logger.log(Level.INFO, "Downloading URL {0}", url.toString());

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        HttpURLConnection.setFollowRedirects(true);
        // allow both GZip and Deflate (ZLib) encodings
        conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
        conn.setConnectTimeout(CONNECT_TIMEOUT_MS);
        conn.setReadTimeout(READ_TIMEOUT_MS);

        // create the appropriate stream wrapper based on the encoding type
        String encoding = conn.getContentEncoding();
        if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
          inputStream = new GZIPInputStream(conn.getInputStream());
        } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
          inputStream = new InflaterInputStream(conn.getInputStream(), new Inflater(true));
        } else {
          inputStream = conn.getInputStream();
        }

        // always specify encoding while reading streams
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

        logger.finer("Before reading url stream");

        String s;
        while ((s = reader.readLine()) != null) {
          contents.append(s);
        }

        if (logger.isLoggable(Level.FINEST)) {
          // no need to encode for logger output
          logger.log(Level.FINEST, "Url stream:\n{0}", contents.toString());
        }
      }
    } catch (Exception e) {
      String message = "Exception when attempting to download and parse URL " + url;
      logger.log(Level.SEVERE, message, e);
      throw new TVRenamerIOException(message, e);
    } finally {
      try {
        if (inputStream != null) {
          inputStream.close();
        }
      } catch (IOException e) {
        logger.log(Level.SEVERE, "Exception when attempting to close input stream", e);
      }
    }

    return StringUtils.encodeSpecialCharacters(contents.toString());
  }
Пример #3
0
  public String getNewFilename() {
    switch (this.status) {
      case ADDED:
        {
          return ADDED_PLACEHOLDER_FILENAME;
        }
      case DOWNLOADED:
      case RENAMED:
        {
          String showName = "";
          String seasonNum = "";
          String titleString = "";
          Calendar airDate = Calendar.getInstance();
          ;

          try {
            Show show = ShowStore.getShow(this.showName);
            showName = show.getName();

            Season season = show.getSeason(this.seasonNumber);
            if (season == null) {
              seasonNum = String.valueOf(this.seasonNumber);
              logger.log(
                  Level.SEVERE,
                  "Season #" + this.seasonNumber + " not found for show '" + this.showName + "'");
            } else {
              seasonNum = String.valueOf(season.getNumber());

              try {
                titleString = season.getTitle(this.episodeNumber);
                airDate.setTime(season.getAirDate(this.episodeNumber));
              } catch (EpisodeNotFoundException e) {
                logger.log(Level.SEVERE, "Episode not found for '" + this.toString() + "'", e);
              }
            }

          } catch (ShowNotFoundException e) {
            showName = this.showName;
            logger.log(Level.SEVERE, "Show not found for '" + this.toString() + "'", e);
          }

          String newFilename = userPrefs.getRenameReplacementString();

          // Ensure that all special characters in the replacement are quoted
          showName = Matcher.quoteReplacement(showName);
          showName = GlobalOverrides.getInstance().getShowName(showName);
          titleString = Matcher.quoteReplacement(titleString);

          // Make whatever modifications are required
          String episodeNumberString = new DecimalFormat("##0").format(this.episodeNumber);
          String episodeNumberWithLeadingZeros =
              new DecimalFormat("#00").format(this.episodeNumber);
          String episodeTitleNoSpaces = titleString.replaceAll(" ", ".");
          String seasonNumberWithLeadingZero = new DecimalFormat("00").format(this.seasonNumber);

          newFilename = newFilename.replaceAll(ReplacementToken.SHOW_NAME.getToken(), showName);
          newFilename = newFilename.replaceAll(ReplacementToken.SEASON_NUM.getToken(), seasonNum);
          newFilename =
              newFilename.replaceAll(
                  ReplacementToken.SEASON_NUM_LEADING_ZERO.getToken(), seasonNumberWithLeadingZero);
          newFilename =
              newFilename.replaceAll(ReplacementToken.EPISODE_NUM.getToken(), episodeNumberString);
          newFilename =
              newFilename.replaceAll(
                  ReplacementToken.EPISODE_NUM_LEADING_ZERO.getToken(),
                  episodeNumberWithLeadingZeros);
          newFilename =
              newFilename.replaceAll(ReplacementToken.EPISODE_TITLE.getToken(), titleString);
          newFilename =
              newFilename.replaceAll(
                  ReplacementToken.EPISODE_TITLE_NO_SPACES.getToken(), episodeTitleNoSpaces);

          // Date and times
          newFilename =
              newFilename.replaceAll(
                  ReplacementToken.DATE_DAY_NUM.getToken(), formatDate(airDate, "d"));
          newFilename =
              newFilename.replaceAll(
                  ReplacementToken.DATE_DAY_NUMLZ.getToken(), formatDate(airDate, "dd"));
          newFilename =
              newFilename.replaceAll(
                  ReplacementToken.DATE_MONTH_NUM.getToken(), formatDate(airDate, "M"));
          newFilename =
              newFilename.replaceAll(
                  ReplacementToken.DATE_MONTH_NUMLZ.getToken(), formatDate(airDate, "MM"));
          newFilename =
              newFilename.replaceAll(
                  ReplacementToken.DATE_YEAR_FULL.getToken(), formatDate(airDate, "yyyy"));
          newFilename =
              newFilename.replaceAll(
                  ReplacementToken.DATE_YEAR_MIN.getToken(), formatDate(airDate, "yy"));

          String resultingFilename =
              newFilename.concat(".").concat(StringUtils.getExtension(file.getName()));
          return StringUtils.sanitiseTitle(resultingFilename);
        }
      case BROKEN:
      default:
        return BROKEN_PLACEHOLDER_FILENAME;
    }
  }