X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Fstates%2FTorrentState.java;h=f2e9ae299098d48a5e5cafb96d6daf3f2c33bdc1;hb=5d962b76adef88663cfa4acc093836c71fe9dd82;hp=3a7bf54f7b4da7d97f141ee5c671b83796db01e6;hpb=6d8a3475d8e56cea63d53d456baef434318223a7;p=rhynodge.git diff --git a/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java b/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java index 3a7bf54..f2e9ae2 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java @@ -20,18 +20,31 @@ package net.pterodactylus.rhynodge.states; import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Optional; +import java.util.Set; +import javax.annotation.Nonnull; + +import net.pterodactylus.rhynodge.Reaction; import net.pterodactylus.rhynodge.State; +import net.pterodactylus.rhynodge.output.DefaultOutput; +import net.pterodactylus.rhynodge.output.Output; import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.collect.Lists; +import com.google.common.collect.Ordering; +import org.apache.commons.lang3.StringEscapeUtils; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.google.common.collect.Lists; +import static com.google.common.collect.Ordering.from; +import static java.lang.String.format; /** * {@link State} that contains information about an arbitrary number of torrent @@ -45,11 +58,13 @@ public class TorrentState extends AbstractState implements Iterable @JsonProperty private List files = Lists.newArrayList(); + private final Set newTorrentFiles = new HashSet<>(); + /** * Creates a new torrent state without torrent files. */ public TorrentState() { - this(Collections. emptySet()); + this(Collections.emptySet()); } /** @@ -62,6 +77,11 @@ public class TorrentState extends AbstractState implements Iterable files.addAll(torrentFiles); } + public TorrentState(Collection torrentFiles, Collection newTorrentFiles) { + files.addAll(torrentFiles); + this.newTorrentFiles.addAll(newTorrentFiles); + } + // // ACCESSORS // @@ -92,6 +112,90 @@ public class TorrentState extends AbstractState implements Iterable return this; } + @Nonnull + @Override + protected String summary(Reaction reaction) { + return format("Found %d new Torrent(s) for “%s!”", newTorrentFiles.size(), reaction.name()); + } + + @Nonnull + @Override + protected String plainText() { + StringBuilder plainText = new StringBuilder(); + plainText.append("New Torrents:\n\n"); + for (TorrentFile torrentFile : newTorrentFiles) { + plainText.append(torrentFile.name()).append('\n'); + plainText.append('\t').append(torrentFile.size()).append(" in ").append(torrentFile.fileCount()).append(" file(s)\n"); + plainText.append('\t').append(torrentFile.seedCount()).append(" seed(s), ").append(torrentFile.leechCount()).append(" leecher(s)\n"); + if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) { + plainText.append('\t').append(torrentFile.magnetUri()).append('\n'); + } + if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) { + plainText.append('\t').append(torrentFile.downloadUri()).append('\n'); + } + plainText.append('\n'); + } + return plainText.toString(); + } + + @Nullable + @Override + protected String htmlText() { + StringBuilder htmlBuilder = new StringBuilder(); + htmlBuilder.append("\n"); + 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("\n"); + htmlBuilder.append("\n"); + htmlBuilder.append("\n"); + for (TorrentFile torrentFile : sortNewFirst().sortedCopy(files)) { + if (newTorrentFiles.contains(torrentFile)) { + htmlBuilder.append(""); + } else { + htmlBuilder.append(""); + } + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append(""); + htmlBuilder.append("\n"); + } + htmlBuilder.append("\n"); + htmlBuilder.append("
All Known Torrents
FilenameSizeFile(s)SeedsLeechersMagnetDownload
").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(); + } + + /** + * Returns an ordering that sorts torrent files by whether they are new + * (according to {@link #files}) or not. New files will be sorted + * first. + * + * @return An ordering for “new files first” + */ + private Ordering sortNewFirst() { + return from((TorrentFile leftTorrentFile, TorrentFile rightTorrentFile) -> { + if (newTorrentFiles.contains(leftTorrentFile) && !newTorrentFiles.contains(rightTorrentFile)) { + return -1; + } + if (!newTorrentFiles.contains(leftTorrentFile) && newTorrentFiles.contains(rightTorrentFile)) { + return 1; + } + return 0; + }); + } + // // ITERABLE METHODS // @@ -113,7 +217,7 @@ public class TorrentState extends AbstractState implements Iterable */ @Override public String toString() { - return String.format("%s[files=%s]", getClass().getSimpleName(), files); + return format("%s[files=%s]", getClass().getSimpleName(), files); } /** @@ -332,7 +436,7 @@ public class TorrentState extends AbstractState implements Iterable */ @Override public String toString() { - return String.format("%s(%s,%s,%s)", name(), size(), magnetUri(), downloadUri()); + return format("%s(%s,%s,%s)", name(), size(), magnetUri(), downloadUri()); } }