X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Ftriggers%2FNewEpisodeTrigger.java;h=075c6f6a998c71ea06070bbc7720de572096f390;hb=5253e286451ddd50cc5f94654f0e8d04322846cf;hp=55266af3b3ab07396afcdc7f251d261f7906d9d4;hpb=50d770036e53d5ba2795b219ec3f5dc6358406b8;p=rhynodge.git diff --git a/src/main/java/net/pterodactylus/rhynodge/triggers/NewEpisodeTrigger.java b/src/main/java/net/pterodactylus/rhynodge/triggers/NewEpisodeTrigger.java index 55266af..075c6f6 100644 --- a/src/main/java/net/pterodactylus/rhynodge/triggers/NewEpisodeTrigger.java +++ b/src/main/java/net/pterodactylus/rhynodge/triggers/NewEpisodeTrigger.java @@ -37,6 +37,8 @@ import org.apache.commons.lang3.StringEscapeUtils; import com.google.common.base.Function; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import com.google.common.collect.Ordering; import com.google.common.collect.Sets; @@ -57,6 +59,8 @@ public class NewEpisodeTrigger implements Trigger { /** All changed episodes. */ private final Collection changedEpisodes = Sets.newHashSet(); + /** All new torrent files. */ + private final Collection newTorrentFiles = Sets.newHashSet(); // // TRIGGER METHODS @@ -72,24 +76,29 @@ public class NewEpisodeTrigger implements Trigger { newEpisodes.clear(); changedEpisodes.clear(); this.allEpisodes.clear(); - Map allEpisodes = FluentIterable.from(((EpisodeState) previousState).episodes()).toMap(new Function() { + newTorrentFiles.clear(); + Map allEpisodes = Maps.newHashMap(FluentIterable.from(((EpisodeState) previousState).episodes()).toMap(new Function() { @Override public Episode apply(Episode episode) { return episode; } - }); + })); for (Episode episode : ((EpisodeState) currentState).episodes()) { if (!allEpisodes.containsKey(episode)) { allEpisodes.put(episode, episode); newEpisodes.add(episode); } - for (TorrentFile torrentFile : episode.torrentFiles()) { - int oldSize = allEpisodes.get(episode).torrentFiles().size(); - allEpisodes.get(episode).addTorrentFile(torrentFile); - int newSize = allEpisodes.get(episode).torrentFiles().size(); - if (!newEpisodes.contains(episode) && (oldSize != newSize)) { - changedEpisodes.add(episode); + Episode existingEpisode = allEpisodes.get(episode); + for (TorrentFile torrentFile : Lists.newArrayList(episode.torrentFiles())) { + int oldSize = existingEpisode.torrentFiles().size(); + existingEpisode.addTorrentFile(torrentFile); + int newSize = existingEpisode.torrentFiles().size(); + if (oldSize != newSize) { + newTorrentFiles.add(torrentFile); + } + if (!newEpisodes.contains(existingEpisode) && (oldSize != newSize)) { + changedEpisodes.add(existingEpisode); } } } @@ -121,8 +130,8 @@ public class NewEpisodeTrigger implements Trigger { summary = String.format("%d changed Torrent(s) for “%s!”", changedEpisodes.size(), reaction.name()); } DefaultOutput output = new DefaultOutput(summary); - output.addText("text/plain", generatePlainText(reaction, newEpisodes, changedEpisodes, allEpisodes)); - output.addText("text/html", generateHtmlText(reaction, newEpisodes, changedEpisodes, allEpisodes)); + output.addText("text/plain", generatePlainText(reaction)); + output.addText("text/html", generateHtmlText(reaction)); return output; } @@ -135,15 +144,9 @@ public class NewEpisodeTrigger implements Trigger { * * @param reaction * The reaction that was triggered - * @param newEpisodes - * The new episodes - * @param changedEpisodes - * The changed episodes - * @param allEpisodes - * All episodes * @return The plain text output */ - private static String generatePlainText(Reaction reaction, Collection newEpisodes, Collection changedEpisodes, Collection allEpisodes) { + private String generatePlainText(Reaction reaction) { StringBuilder stringBuilder = new StringBuilder(); if (!newEpisodes.isEmpty()) { stringBuilder.append(reaction.name()).append(" - New Episodes\n\n"); @@ -204,70 +207,57 @@ public class NewEpisodeTrigger implements Trigger { * * @param reaction * The reaction that was triggered - * @param newEpisodes - * The new episodes - * @param changedEpisodes - * The changed episodes - * @param allEpisodes - * All episodes * @return The HTML output */ - private static String generateHtmlText(Reaction reaction, Collection newEpisodes, Collection changedEpisodes, Collection allEpisodes) { + private String generateHtmlText(Reaction reaction) { StringBuilder htmlBuilder = new StringBuilder(); htmlBuilder.append("\n"); - htmlBuilder.append("

").append(StringEscapeUtils.escapeHtml4(reaction.name())).append("

\n"); - if (!newEpisodes.isEmpty()) { - htmlBuilder.append("

New Episodes

\n"); - htmlBuilder.append("
    \n"); - for (Episode episode : newEpisodes) { - htmlBuilder.append("
  • Season ").append(episode.season()).append(", Episode ").append(episode.episode()).append("
  • \n"); - htmlBuilder.append("
      \n"); + /* show all known episodes. */ + htmlBuilder.append("\n\n"); + htmlBuilder.append("\n"); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append("\n"); + htmlBuilder.append("\n"); + htmlBuilder.append("\n"); + Episode lastEpisode = null; + for (Entry> seasonEntry : FluentIterable.from(Ordering.natural().reverse().sortedCopy(allEpisodes)).index(Episode.BY_SEASON).asMap().entrySet()) { + for (Episode episode : seasonEntry.getValue()) { for (TorrentFile torrentFile : episode) { - htmlBuilder.append("
    • ").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("
    • \n"); - htmlBuilder.append("
      "); - htmlBuilder.append("").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append(", "); - htmlBuilder.append("").append(torrentFile.fileCount()).append(" file(s), "); - htmlBuilder.append("").append(torrentFile.seedCount()).append(" seed(s), "); - htmlBuilder.append("").append(torrentFile.leechCount()).append(" leecher(s)
      \n"); - htmlBuilder.append("
      "); - if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) { - htmlBuilder.append("Magnet "); + if (newEpisodes.contains(episode)) { + htmlBuilder.append("
      "); + } else if (newTorrentFiles.contains(torrentFile)) { + htmlBuilder.append(""); + } else { + htmlBuilder.append(""); } - if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) { - htmlBuilder.append("Download"); - } - htmlBuilder.append("\n"); - } - htmlBuilder.append("\n"); - } - htmlBuilder.append("\n"); - } - if (!changedEpisodes.isEmpty()) { - htmlBuilder.append("

      Changed Episodes

      \n"); - htmlBuilder.append("
        \n"); - for (Episode episode : changedEpisodes) { - htmlBuilder.append("
      • Season ").append(episode.season()).append(", Episode ").append(episode.episode()).append("
      • \n"); - htmlBuilder.append("
          \n"); - for (TorrentFile torrentFile : episode) { - htmlBuilder.append("
        • ").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("
        • \n"); - htmlBuilder.append("
          "); - htmlBuilder.append("").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append(", "); - htmlBuilder.append("").append(torrentFile.fileCount()).append(" file(s), "); - htmlBuilder.append("").append(torrentFile.seedCount()).append(" seed(s), "); - htmlBuilder.append("").append(torrentFile.leechCount()).append(" leecher(s)
          \n"); - htmlBuilder.append("
          "); - if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) { - htmlBuilder.append("Magnet "); - } - if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) { - htmlBuilder.append("Download"); + if ((lastEpisode == null) || !lastEpisode.equals(episode)) { + htmlBuilder.append("
      "); + } else { + htmlBuilder.append(""); } - htmlBuilder.append("\n"); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append("\n"); + lastEpisode = episode; } - htmlBuilder.append("\n"); } - htmlBuilder.append("\n"); } + htmlBuilder.append("\n"); + htmlBuilder.append("
      All Known Episodes
      SeasonEpisodeFilenameSizeFile(s)SeedsLeechersMagnetDownload
      ").append(episode.season()).append("").append(episode.episode()).append("").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("").append(torrentFile.fileCount()).append("").append(torrentFile.seedCount()).append("").append(torrentFile.leechCount()).append("LinkLink
      \n"); htmlBuilder.append("\n"); return htmlBuilder.toString(); }