package net.pterodactylus.rhynodge.triggers;
-import static com.google.common.base.Preconditions.checkState;
-
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashSet;
+import java.util.Map;
-import net.pterodactylus.rhynodge.Reaction;
import net.pterodactylus.rhynodge.State;
import net.pterodactylus.rhynodge.Trigger;
-import net.pterodactylus.rhynodge.output.DefaultOutput;
-import net.pterodactylus.rhynodge.output.Output;
import net.pterodactylus.rhynodge.states.EpisodeState;
import net.pterodactylus.rhynodge.states.EpisodeState.Episode;
import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
-import org.apache.commons.lang3.StringEscapeUtils;
-
-import com.google.common.base.Predicate;
-import com.google.common.collect.Collections2;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.function.Function.identity;
+import static java.util.stream.Collectors.toMap;
/**
* {@link Trigger} implementation that compares two {@link EpisodeState}s for
*/
public class NewEpisodeTrigger implements Trigger {
- /** All new episodes. */
- private Collection<Episode> newEpisodes;
-
- /** All changed episodes. */
- private Collection<Episode> changedEpisodes;
-
- //
- // TRIGGER METHODS
- //
+ private boolean triggered = false;
/**
* {@inheritDoc}
*/
@Override
- public boolean triggers(State currentState, State previousState) {
+ public State mergeStates(State previousState, State currentState) {
checkState(currentState instanceof EpisodeState, "currentState is not a EpisodeState but a %s", currentState.getClass().getName());
checkState(previousState instanceof EpisodeState, "previousState is not a EpisodeState but a %s", currentState.getClass().getName());
- final EpisodeState currentEpisodeState = (EpisodeState) currentState;
- final EpisodeState previousEpisodeState = (EpisodeState) previousState;
- newEpisodes = Collections2.filter(currentEpisodeState.episodes(), new Predicate<Episode>() {
-
- @Override
- public boolean apply(Episode episode) {
- return !previousEpisodeState.episodes().contains(episode);
+ Collection<Episode> newEpisodes = new HashSet<>();
+ Collection<Episode> changedEpisodes = new HashSet<>();
+ Collection<TorrentFile> newTorrentFiles = new HashSet<>();
+ Map<Episode, Episode> allEpisodes = ((EpisodeState) previousState).episodes().stream().collect(toMap(identity(), identity()));
+ for (Episode episode : ((EpisodeState) currentState).episodes()) {
+ if (!allEpisodes.containsKey(episode)) {
+ allEpisodes.put(episode, episode);
+ newEpisodes.add(episode);
+ triggered = true;
}
- });
-
- changedEpisodes = Collections2.filter(currentEpisodeState.episodes(), new Predicate<Episode>() {
-
- @Override
- public boolean apply(Episode episode) {
- if (!previousEpisodeState.episodes().contains(episode)) {
- return false;
+ Episode existingEpisode = allEpisodes.get(episode);
+ for (TorrentFile torrentFile : new ArrayList<>(episode.torrentFiles())) {
+ int oldSize = existingEpisode.torrentFiles().size();
+ existingEpisode.addTorrentFile(torrentFile);
+ int newSize = existingEpisode.torrentFiles().size();
+ if (oldSize != newSize) {
+ newTorrentFiles.add(torrentFile);
}
-
- /* find previous episode. */
- final Episode previousEpisode = findPreviousEpisode(episode);
-
- /* compare the list of torrent files. */
- Collection<TorrentFile> newTorrentFiles = Collections2.filter(episode.torrentFiles(), new Predicate<TorrentFile>() {
-
- @Override
- public boolean apply(TorrentFile torrentFile) {
- return !previousEpisode.torrentFiles().contains(torrentFile);
- }
- });
-
- return !newTorrentFiles.isEmpty();
- }
-
- private Episode findPreviousEpisode(Episode episode) {
- for (Episode previousStateEpisode : previousEpisodeState) {
- if (previousStateEpisode.equals(episode)) {
- return previousStateEpisode;
- }
+ if (!newEpisodes.contains(existingEpisode) && (oldSize != newSize)) {
+ changedEpisodes.add(existingEpisode);
}
- return null;
}
-
- });
-
- return !newEpisodes.isEmpty() || !changedEpisodes.isEmpty();
+ }
+ return new EpisodeState(allEpisodes.values(), newEpisodes, changedEpisodes, newTorrentFiles);
}
/**
* {@inheritDoc}
*/
@Override
- public Output output(Reaction reaction) {
- String summary;
- if (!newEpisodes.isEmpty()) {
- if (!changedEpisodes.isEmpty()) {
- summary = String.format("%d new and %d changed Torrent(s) for “%s!”", newEpisodes.size(), changedEpisodes.size(), reaction.name());
- } else {
- summary = String.format("%d new Torrent(s) for “%s!”", newEpisodes.size(), reaction.name());
- }
- } else {
- 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));
- output.addText("text/html", generateHtmlText(reaction, newEpisodes, changedEpisodes));
- return output;
- }
-
- //
- // STATIC METHODS
- //
-
- /**
- * Generates the plain text trigger output.
- *
- * @param reaction
- * The reaction that was triggered
- * @param newEpisodes
- * The new episodes
- * @param changedEpisodes
- * The changed episodes
- * @return The plain text output
- */
- private static String generatePlainText(Reaction reaction, Collection<Episode> newEpisodes, Collection<Episode> changedEpisodes) {
- StringBuilder stringBuilder = new StringBuilder();
- if (!newEpisodes.isEmpty()) {
- stringBuilder.append(reaction.name()).append(" - New Episodes\n\n");
- for (Episode episode : newEpisodes) {
- stringBuilder.append("- ").append(episode.identifier()).append("\n");
- for (TorrentFile torrentFile : episode) {
- stringBuilder.append(" - ").append(torrentFile.name()).append(", ").append(torrentFile.size()).append("\n");
- if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) {
- stringBuilder.append(" Magnet: ").append(torrentFile.magnetUri()).append("\n");
- }
- if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) {
- stringBuilder.append(" Download: ").append(torrentFile.downloadUri()).append("\n");
- }
- }
- }
- }
- if (!changedEpisodes.isEmpty()) {
- stringBuilder.append(reaction.name()).append(" - Changed Episodes\n\n");
- for (Episode episode : changedEpisodes) {
- stringBuilder.append("- ").append(episode.identifier()).append("\n");
- for (TorrentFile torrentFile : episode) {
- stringBuilder.append(" - ").append(torrentFile.name()).append(", ").append(torrentFile.size()).append("\n");
- if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) {
- stringBuilder.append(" Magnet: ").append(torrentFile.magnetUri()).append("\n");
- }
- if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) {
- stringBuilder.append(" Download: ").append(torrentFile.downloadUri()).append("\n");
- }
- }
- }
- }
- return stringBuilder.toString();
- }
-
- /**
- * Generates the HTML trigger output.
- *
- * @param reaction
- * The reaction that was triggered
- * @param newEpisodes
- * The new episodes
- * @param changedEpisodes
- * The changed episodes
- * @return The HTML output
- */
- private static String generateHtmlText(Reaction reaction, Collection<Episode> newEpisodes, Collection<Episode> changedEpisodes) {
- StringBuilder htmlBuilder = new StringBuilder();
- htmlBuilder.append("<html><body>\n");
- htmlBuilder.append("<h1>").append(StringEscapeUtils.escapeHtml4(reaction.name())).append("</h1>\n");
- if (!newEpisodes.isEmpty()) {
- htmlBuilder.append("<h2>New Episodes</h2>\n");
- htmlBuilder.append("<ul>\n");
- for (Episode episode : newEpisodes) {
- htmlBuilder.append("<li>Season ").append(episode.season()).append(", Episode ").append(episode.episode()).append("</li>\n");
- htmlBuilder.append("<ul>\n");
- for (TorrentFile torrentFile : episode) {
- htmlBuilder.append("<li>").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("</li>\n");
- htmlBuilder.append("<div>");
- htmlBuilder.append("<strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("</strong>, ");
- htmlBuilder.append("<strong>").append(torrentFile.fileCount()).append("</strong> file(s), ");
- htmlBuilder.append("<strong>").append(torrentFile.seedCount()).append("</strong> seed(s), ");
- htmlBuilder.append("<strong>").append(torrentFile.leechCount()).append("</strong> leecher(s)</div>\n");
- htmlBuilder.append("<div>");
- if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) {
- htmlBuilder.append("<a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.magnetUri())).append("\">Magnet</a> ");
- }
- if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) {
- htmlBuilder.append("<a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.downloadUri())).append("\">Download</a>");
- }
- htmlBuilder.append("</div>\n");
- }
- htmlBuilder.append("</ul>\n");
- }
- htmlBuilder.append("</ul>\n");
- }
- if (!changedEpisodes.isEmpty()) {
- htmlBuilder.append("<h2>Changed Episodes</h2>\n");
- htmlBuilder.append("<ul>\n");
- for (Episode episode : changedEpisodes) {
- htmlBuilder.append("<li>Season ").append(episode.season()).append(", Episode ").append(episode.episode()).append("</li>\n");
- htmlBuilder.append("<ul>\n");
- for (TorrentFile torrentFile : episode) {
- htmlBuilder.append("<li>").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("</li>\n");
- htmlBuilder.append("<div>");
- htmlBuilder.append("<strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("</strong>, ");
- htmlBuilder.append("<strong>").append(torrentFile.fileCount()).append("</strong> file(s), ");
- htmlBuilder.append("<strong>").append(torrentFile.seedCount()).append("</strong> seed(s), ");
- htmlBuilder.append("<strong>").append(torrentFile.leechCount()).append("</strong> leecher(s)</div>\n");
- htmlBuilder.append("<div>");
- if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) {
- htmlBuilder.append("<a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.magnetUri())).append("\">Magnet</a> ");
- }
- if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) {
- htmlBuilder.append("<a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.downloadUri())).append("\">Download</a>");
- }
- htmlBuilder.append("</div>\n");
- }
- htmlBuilder.append("</ul>\n");
- }
- htmlBuilder.append("</ul>\n");
- }
- htmlBuilder.append("</body></html>\n");
- return htmlBuilder.toString();
+ public boolean triggers() {
+ return triggered;
}
}