Ignore chains and states default directories.
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / filters / EpisodeFilter.java
1 /*
2  * Reactor - EpisodeFilter.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.reactor.filters;
19
20 import static com.google.common.base.Preconditions.checkState;
21
22 import java.util.LinkedHashMap;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 import net.pterodactylus.reactor.Filter;
27 import net.pterodactylus.reactor.State;
28 import net.pterodactylus.reactor.states.EpisodeState;
29 import net.pterodactylus.reactor.states.EpisodeState.Episode;
30 import net.pterodactylus.reactor.states.FailedState;
31 import net.pterodactylus.reactor.states.TorrentState;
32 import net.pterodactylus.reactor.states.TorrentState.TorrentFile;
33
34 /**
35  * {@link Filter} implementation that extracts {@link Episode} information from
36  * the {@link TorrentFile}s contained in a {@link TorrentState}.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class EpisodeFilter implements Filter {
41
42         /** The pattern to parse episode information from the filename. */
43         private static Pattern episodePattern = Pattern.compile("S(\\d{2})E(\\d{2})|[^\\d](\\d{1,2})x(\\d{2})[^\\d]");
44
45         //
46         // FILTER METHODS
47         //
48
49         /**
50          * {@inheritDoc}
51          */
52         @Override
53         public State filter(State state) {
54                 if (!state.success()) {
55                         return FailedState.from(state);
56                 }
57                 checkState(state instanceof TorrentState, "state is not a TorrentState but a %s!", state.getClass());
58
59                 TorrentState torrentState = (TorrentState) state;
60                 LinkedHashMap<Episode, Episode> episodes = new LinkedHashMap<Episode, Episode>();
61                 for (TorrentFile torrentFile : torrentState) {
62                         Episode episode = extractEpisode(torrentFile);
63                         if (episode == null) {
64                                 continue;
65                         }
66                         episodes.put(episode, episode);
67                         episode = episodes.get(episode);
68                         episode.addTorrentFile(torrentFile);
69                 }
70
71                 return new EpisodeState(episodes.values());
72         }
73
74         //
75         // STATIC METHODS
76         //
77
78         /**
79          * Extracts episode information from the given torrent file.
80          *
81          * @param torrentFile
82          *            The torrent file to extract the episode information from
83          * @return The extracted episode information, or {@code null} if no episode
84          *         information could be found
85          */
86         private static Episode extractEpisode(TorrentFile torrentFile) {
87                 Matcher matcher = episodePattern.matcher(torrentFile.name());
88                 if (!matcher.find()) {
89                         return null;
90                 }
91                 String seasonString = matcher.group(1);
92                 String episodeString = matcher.group(2);
93                 if ((seasonString == null) && (episodeString == null)) {
94                         seasonString = matcher.group(3);
95                         episodeString = matcher.group(4);
96                 }
97                 int season = Integer.valueOf(seasonString);
98                 int episode = Integer.valueOf(episodeString);
99                 return new Episode(season, episode);
100         }
101
102 }