2011-11-14 06:43:22 -05:00
|
|
|
|
2011-12-26 13:10:53 -05:00
|
|
|
package net.sourceforge.filebot.media;
|
2011-11-14 06:43:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
import static java.util.ResourceBundle.*;
|
2011-11-26 10:41:58 -05:00
|
|
|
import static java.util.concurrent.TimeUnit.*;
|
2011-11-14 06:43:22 -05:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2011-11-14 06:43:22 -05:00
|
|
|
public String getReleaseGroup(File file) throws IOException {
|
|
|
|
// check parent and itself for group names
|
|
|
|
return matchLast(getReleaseGroupPattern(), file.getParent(), file.getName());
|
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2011-11-14 06:43:22 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2011-11-26 04:50:31 -05:00
|
|
|
public List<String> clean(Iterable<String> items) throws IOException {
|
2011-11-14 06:43:22 -05:00
|
|
|
return clean(items, getVideoSourcePattern(), getCodecPattern());
|
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2011-11-26 04:50:31 -05:00
|
|
|
public String clean(String item) throws IOException {
|
|
|
|
return clean(item, getVideoSourcePattern(), getCodecPattern());
|
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2011-11-14 06:43:22 -05:00
|
|
|
public List<String> cleanRG(Iterable<String> items) throws IOException {
|
|
|
|
return clean(items, getReleaseGroupPattern(), getVideoSourcePattern(), getCodecPattern());
|
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2011-11-26 04:50:31 -05:00
|
|
|
public String cleanRG(String item) throws IOException {
|
|
|
|
return clean(item, getReleaseGroupPattern(), getVideoSourcePattern(), getCodecPattern());
|
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2011-11-14 06:43:22 -05:00
|
|
|
public List<String> clean(Iterable<String> items, Pattern... blacklisted) {
|
2011-11-26 04:50:31 -05:00
|
|
|
List<String> cleanedItems = new ArrayList<String>();
|
|
|
|
for (String it : items) {
|
|
|
|
cleanedItems.add(clean(it, blacklisted));
|
|
|
|
}
|
2011-11-14 06:43:22 -05:00
|
|
|
|
2011-11-26 04:50:31 -05:00
|
|
|
return cleanedItems;
|
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2011-11-26 04:50:31 -05:00
|
|
|
public String clean(String item, Pattern... blacklisted) {
|
|
|
|
for (Pattern it : blacklisted) {
|
|
|
|
item = it.matcher(item).replaceAll("");
|
2011-11-14 06:43:22 -05:00
|
|
|
}
|
|
|
|
|
2011-11-26 04:50:31 -05:00
|
|
|
return item.replaceAll("[\\p{Punct}\\p{Space}]+", " ").trim();
|
2011-11-14 06:43:22 -05:00
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2011-11-14 06:43:22 -05:00
|
|
|
public Pattern getCodecPattern() {
|
|
|
|
// pattern matching any video source name
|
|
|
|
String pattern = getBundle(getClass().getName()).getString("pattern.codec");
|
|
|
|
return compile("(?<!\\p{Alnum})(" + pattern + ")(?!\\p{Alnum})", CASE_INSENSITIVE);
|
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2011-11-14 06:43:22 -05:00
|
|
|
public Pattern getVideoSourcePattern() {
|
|
|
|
// pattern matching any video source name
|
|
|
|
String pattern = getBundle(getClass().getName()).getString("pattern.video.source");
|
|
|
|
return compile("(?<!\\p{Alnum})(" + pattern + ")(?!\\p{Alnum})", CASE_INSENSITIVE);
|
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2011-11-14 06:43:22 -05:00
|
|
|
public Pattern getReleaseGroupPattern() throws IOException {
|
|
|
|
// pattern matching any release group name enclosed in separators
|
|
|
|
return compile("(?<!\\p{Alnum})(" + join(releaseGroupResource.get(), "|") + ")(?!\\p{Alnum})", CASE_INSENSITIVE);
|
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2011-11-26 10:41:58 -05:00
|
|
|
// fetch release group names online and try to update the data every other day
|
2011-12-24 02:30:54 -05:00
|
|
|
protected final CachedResource<String[]> releaseGroupResource = new CachedResource<String[]>(getBundle(getClass().getName()).getString("url.release-groups"), String[].class, DAYS.toMillis(1)) {
|
2011-11-14 06:43:22 -05:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String[] process(ByteBuffer data) {
|
2011-11-26 10:41:58 -05:00
|
|
|
return compile("\\s+").split(Charset.forName("UTF-8").decode(data));
|
2011-11-14 06:43:22 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|