package net.sourceforge.filebot.media; import static java.util.ResourceBundle.*; import static java.util.concurrent.TimeUnit.*; import static java.util.regex.Pattern.*; import static net.sourceforge.tuned.StringUtilities.*; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import net.sourceforge.filebot.web.CachedResource; public class ReleaseInfo { public String getVideoSource(File file) { // check parent and itself for group names return matchLast(getVideoSourcePattern(), file.getParent(), file.getName()); } public String getReleaseGroup(File file) throws IOException { // check parent and itself for group names return matchLast(getReleaseGroupPattern(), file.getParent(), file.getName()); } protected String matchLast(Pattern pattern, CharSequence... sequence) { String lastMatch = null; for (CharSequence name : sequence) { if (name == null) continue; Matcher matcher = pattern.matcher(name); while (matcher.find()) { lastMatch = matcher.group(); } } return lastMatch; } public List clean(Iterable items) throws IOException { return clean(items, getVideoSourcePattern(), getCodecPattern()); } public String clean(String item) throws IOException { return clean(item, getVideoSourcePattern(), getCodecPattern()); } public List cleanRG(Iterable items) throws IOException { return clean(items, getReleaseGroupPattern(), getVideoSourcePattern(), getCodecPattern()); } public String cleanRG(String item) throws IOException { return clean(item, getReleaseGroupPattern(), getVideoSourcePattern(), getCodecPattern()); } public List clean(Iterable items, Pattern... blacklisted) { List cleanedItems = new ArrayList(); for (String it : items) { cleanedItems.add(clean(it, blacklisted)); } return cleanedItems; } public String clean(String item, Pattern... blacklisted) { for (Pattern it : blacklisted) { item = it.matcher(item).replaceAll(""); } return item.replaceAll("[\\p{Punct}\\p{Space}]+", " ").trim(); } public Pattern getCodecPattern() { // pattern matching any video source name String pattern = getBundle(getClass().getName()).getString("pattern.codec"); return compile("(? releaseGroupResource = new CachedResource(getBundle(getClass().getName()).getString("url.release-groups"), String[].class, DAYS.toMillis(1)) { @Override public String[] process(ByteBuffer data) { return compile("\\s+").split(Charset.forName("UTF-8").decode(data)); } }; }