2 * Rhynodge - EpisodeFilter.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.rhynodge.filters;
20 import static com.google.common.base.Preconditions.checkState;
22 import java.util.HashMap;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
27 import net.pterodactylus.rhynodge.Filter;
28 import net.pterodactylus.rhynodge.State;
29 import net.pterodactylus.rhynodge.states.EpisodeState;
30 import net.pterodactylus.rhynodge.states.EpisodeState.Episode;
31 import net.pterodactylus.rhynodge.states.FailedState;
32 import net.pterodactylus.rhynodge.states.TorrentState;
33 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
36 * {@link Filter} implementation that extracts {@link Episode} information from
37 * the {@link TorrentFile}s contained in a {@link TorrentState}.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class EpisodeFilter implements Filter {
43 /** The pattern to parse episode information from the filename. */
44 private static Pattern episodePattern = Pattern.compile("S(\\d{2})E(\\d{2})|[^\\d](\\d{1,2})x(\\d{2})[^\\d]");
54 public State filter(State state) {
55 if (!state.success()) {
56 return FailedState.from(state);
58 checkState(state instanceof TorrentState, "state is not a TorrentState but a %s!", state.getClass());
60 TorrentState torrentState = (TorrentState) state;
61 Map<Episode, Episode> episodes = new HashMap<Episode, Episode>();
62 for (TorrentFile torrentFile : torrentState) {
63 Episode episode = extractEpisode(torrentFile);
64 if (episode == null) {
67 if (!episodes.containsKey(episode)) {
68 episodes.put(episode, episode);
70 episode = episodes.get(episode);
71 episode.addTorrentFile(torrentFile);
74 return new EpisodeState(episodes.values());
82 * Extracts episode information from the given torrent file.
85 * The torrent file to extract the episode information from
86 * @return The extracted episode information, or {@code null} if no episode
87 * information could be found
89 private static Episode extractEpisode(TorrentFile torrentFile) {
90 Matcher matcher = episodePattern.matcher(torrentFile.name());
91 if (!matcher.find()) {
94 String seasonString = matcher.group(1);
95 String episodeString = matcher.group(2);
96 if ((seasonString == null) && (episodeString == null)) {
97 seasonString = matcher.group(3);
98 episodeString = matcher.group(4);
100 int season = Integer.valueOf(seasonString);
101 int episode = Integer.valueOf(episodeString);
102 return new Episode(season, episode);