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