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
|
|
|
|
|
|
|
|
2012-02-15 01:12:09 -05:00
|
|
|
import static java.util.Arrays.*;
|
2012-06-22 03:47:26 -04:00
|
|
|
import static java.util.Collections.*;
|
2011-11-14 06:43:22 -05:00
|
|
|
import static java.util.ResourceBundle.*;
|
|
|
|
import static java.util.regex.Pattern.*;
|
2012-01-01 22:48:24 -05:00
|
|
|
import static net.sourceforge.filebot.similarity.Normalization.*;
|
2011-11-14 06:43:22 -05:00
|
|
|
import static net.sourceforge.tuned.StringUtilities.*;
|
|
|
|
|
|
|
|
import java.io.File;
|
2012-02-10 11:43:09 -05:00
|
|
|
import java.io.FileFilter;
|
2011-11-14 06:43:22 -05:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.charset.Charset;
|
2012-02-15 01:12:09 -05:00
|
|
|
import java.text.Collator;
|
|
|
|
import java.text.Normalizer;
|
|
|
|
import java.text.Normalizer.Form;
|
2011-11-14 06:43:22 -05:00
|
|
|
import java.util.ArrayList;
|
2012-02-15 01:12:09 -05:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Comparator;
|
2012-07-26 04:45:15 -04:00
|
|
|
import java.util.HashMap;
|
2012-02-15 01:12:09 -05:00
|
|
|
import java.util.HashSet;
|
2012-10-09 11:04:14 -04:00
|
|
|
import java.util.LinkedHashMap;
|
2011-11-14 06:43:22 -05:00
|
|
|
import java.util.List;
|
2011-12-30 10:34:02 -05:00
|
|
|
import java.util.Locale;
|
2012-01-02 11:59:37 -05:00
|
|
|
import java.util.Map;
|
2012-01-01 22:48:24 -05:00
|
|
|
import java.util.Scanner;
|
2012-02-15 01:12:09 -05:00
|
|
|
import java.util.Set;
|
2012-01-02 11:59:37 -05:00
|
|
|
import java.util.TreeMap;
|
2011-11-14 06:43:22 -05:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2012-01-01 22:48:24 -05:00
|
|
|
import java.util.zip.GZIPInputStream;
|
2011-11-14 06:43:22 -05:00
|
|
|
|
2013-03-17 10:19:11 -04:00
|
|
|
import net.sourceforge.filebot.web.AnidbClient.AnidbSearchResult;
|
2011-11-14 06:43:22 -05:00
|
|
|
import net.sourceforge.filebot.web.CachedResource;
|
2012-01-01 22:48:24 -05:00
|
|
|
import net.sourceforge.filebot.web.Movie;
|
2012-10-14 07:57:25 -04:00
|
|
|
import net.sourceforge.filebot.web.TheTVDBClient.TheTVDBSearchResult;
|
2012-01-01 22:48:24 -05:00
|
|
|
import net.sourceforge.tuned.ByteBufferInputStream;
|
2011-11-14 06:43:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
public class ReleaseInfo {
|
|
|
|
|
2012-10-24 07:57:36 -04:00
|
|
|
public String getVideoSource(String... strings) {
|
2011-11-14 06:43:22 -05:00
|
|
|
// check parent and itself for group names
|
2012-10-24 07:57:36 -04:00
|
|
|
return matchLast(getVideoSourcePattern(), getBundle(getClass().getName()).getString("pattern.video.source").split("[|]"), strings);
|
2011-11-14 06:43:22 -05:00
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2012-10-24 07:57:36 -04:00
|
|
|
public String getReleaseGroup(String... strings) throws IOException {
|
2012-06-27 22:36:32 -04:00
|
|
|
// check file and folder for release group names
|
|
|
|
String[] groups = releaseGroupResource.get();
|
|
|
|
|
|
|
|
// try case-sensitive match
|
2012-10-24 07:57:36 -04:00
|
|
|
String match = matchLast(getReleaseGroupPattern(true), groups, strings);
|
2012-06-27 22:36:32 -04:00
|
|
|
|
|
|
|
// try case-insensitive match as fallback
|
|
|
|
if (match == null) {
|
2012-10-24 07:57:36 -04:00
|
|
|
match = matchLast(getReleaseGroupPattern(false), groups, strings);
|
2012-06-27 22:36:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return match;
|
2011-11-14 06:43:22 -05:00
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2012-01-02 11:59:37 -05:00
|
|
|
public Locale getLanguageSuffix(String name) {
|
|
|
|
// match locale identifier and lookup Locale object
|
2012-02-15 01:12:09 -05:00
|
|
|
Map<String, Locale> languages = getLanguageMap(Locale.ENGLISH, Locale.getDefault());
|
2012-02-15 01:16:32 -05:00
|
|
|
|
2012-11-22 11:45:40 -05:00
|
|
|
String lang = matchLast(getLanguageSuffixPattern(languages.keySet(), false), null, name);
|
2012-01-02 11:59:37 -05:00
|
|
|
if (lang == null)
|
|
|
|
return null;
|
|
|
|
|
2012-02-15 01:12:09 -05:00
|
|
|
return languages.get(lang);
|
2012-01-02 11:59:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-01 22:48:24 -05:00
|
|
|
protected String matchLast(Pattern pattern, String[] standardValues, CharSequence... sequence) {
|
2011-11-14 06:43:22 -05:00
|
|
|
String lastMatch = null;
|
|
|
|
|
2012-01-01 22:48:24 -05:00
|
|
|
// match last occurrence
|
2011-11-14 06:43:22 -05:00
|
|
|
for (CharSequence name : sequence) {
|
|
|
|
if (name == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Matcher matcher = pattern.matcher(name);
|
|
|
|
while (matcher.find()) {
|
|
|
|
lastMatch = matcher.group();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-01 22:48:24 -05:00
|
|
|
// prefer standard value over matched value
|
2012-01-02 11:59:37 -05:00
|
|
|
if (lastMatch != null && standardValues != null) {
|
2012-01-01 22:48:24 -05:00
|
|
|
for (String standard : standardValues) {
|
|
|
|
if (standard.equalsIgnoreCase(lastMatch)) {
|
|
|
|
return standard;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-14 06:43:22 -05:00
|
|
|
return lastMatch;
|
|
|
|
}
|
|
|
|
|
2012-07-24 16:01:48 -04:00
|
|
|
// cached patterns
|
2012-07-26 04:45:15 -04:00
|
|
|
private final Map<Boolean, Pattern[]> stopwords = new HashMap<Boolean, Pattern[]>(2);
|
|
|
|
private final Map<Boolean, Pattern[]> blacklist = new HashMap<Boolean, Pattern[]>(2);
|
2012-07-24 16:01:48 -04:00
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2012-02-23 13:48:35 -05:00
|
|
|
public List<String> cleanRelease(Collection<String> items, boolean strict) throws IOException {
|
2012-07-24 16:01:48 -04:00
|
|
|
Pattern[] stopwords;
|
|
|
|
Pattern[] blacklist;
|
2012-02-23 13:48:35 -05:00
|
|
|
|
2012-07-24 16:01:48 -04:00
|
|
|
// initialize cached patterns
|
2012-07-26 04:45:15 -04:00
|
|
|
synchronized (this.stopwords) {
|
|
|
|
stopwords = this.stopwords.get(strict);
|
|
|
|
blacklist = this.blacklist.get(strict);
|
2012-07-24 16:01:48 -04:00
|
|
|
|
|
|
|
if (stopwords == null || blacklist == null) {
|
|
|
|
Set<String> languages = getLanguageMap(Locale.ENGLISH, Locale.getDefault()).keySet();
|
|
|
|
Pattern clutterBracket = getClutterBracketPattern(strict);
|
|
|
|
Pattern releaseGroup = getReleaseGroupPattern(strict);
|
2012-11-22 11:45:40 -05:00
|
|
|
Pattern languageSuffix = getLanguageSuffixPattern(languages, strict);
|
2012-07-24 16:01:48 -04:00
|
|
|
Pattern languageTag = getLanguageTagPattern(languages);
|
|
|
|
Pattern videoSource = getVideoSourcePattern();
|
|
|
|
Pattern videoFormat = getVideoFormatPattern();
|
|
|
|
Pattern resolution = getResolutionPattern();
|
|
|
|
Pattern queryBlacklist = getBlacklistPattern();
|
|
|
|
|
|
|
|
stopwords = new Pattern[] { languageTag, videoSource, videoFormat, resolution, languageSuffix };
|
2012-11-15 08:48:28 -05:00
|
|
|
blacklist = new Pattern[] { queryBlacklist, languageTag, clutterBracket, releaseGroup, videoSource, videoFormat, resolution, languageSuffix };
|
2012-07-24 16:01:48 -04:00
|
|
|
|
|
|
|
// cache compiled patterns for common usage
|
2012-07-26 04:45:15 -04:00
|
|
|
this.stopwords.put(strict, stopwords);
|
|
|
|
this.blacklist.put(strict, blacklist);
|
2012-07-24 16:01:48 -04:00
|
|
|
}
|
|
|
|
}
|
2012-02-23 13:48:35 -05:00
|
|
|
|
|
|
|
List<String> output = new ArrayList<String>(items.size());
|
2011-11-26 04:50:31 -05:00
|
|
|
for (String it : items) {
|
2012-07-13 07:15:14 -04:00
|
|
|
it = strict ? clean(it, stopwords) : substringBefore(it, stopwords);
|
2013-04-01 06:17:20 -04:00
|
|
|
it = normalizePunctuation(clean(it, blacklist));
|
2012-02-23 13:48:35 -05:00
|
|
|
|
|
|
|
// ignore empty values
|
|
|
|
if (it.length() > 0) {
|
|
|
|
output.add(it);
|
2012-01-01 22:48:24 -05:00
|
|
|
}
|
2011-11-26 04:50:31 -05:00
|
|
|
}
|
2011-11-14 06:43:22 -05:00
|
|
|
|
2012-02-23 13:48:35 -05:00
|
|
|
return output;
|
2011-11-26 04:50:31 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2013-04-01 06:17:20 -04:00
|
|
|
return item;
|
2011-11-14 06:43:22 -05:00
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2012-02-23 13:48:35 -05:00
|
|
|
public String substringBefore(String item, Pattern... stopwords) {
|
|
|
|
for (Pattern it : stopwords) {
|
|
|
|
Matcher matcher = it.matcher(item);
|
|
|
|
if (matcher.find()) {
|
2012-07-04 06:20:52 -04:00
|
|
|
String substring = item.substring(0, matcher.start()); // use substring before the matched stopword
|
|
|
|
if (normalizePunctuation(substring).length() >= 3) {
|
|
|
|
item = substring; // make sure that the substring has enough data
|
|
|
|
}
|
2012-02-23 13:48:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Pattern getLanguageTagPattern(Collection<String> languages) {
|
2012-02-15 01:12:09 -05:00
|
|
|
// [en]
|
2012-07-26 04:45:15 -04:00
|
|
|
return compile("(?<=[-\\[{(])(" + join(quoteAll(languages), "|") + ")(?=\\p{Punct})", CASE_INSENSITIVE | UNICODE_CASE);
|
2012-01-02 11:59:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-22 11:45:40 -05:00
|
|
|
public Pattern getLanguageSuffixPattern(Collection<String> languages, boolean strict) {
|
2012-02-15 01:12:09 -05:00
|
|
|
// .en.srt
|
2012-11-22 11:45:40 -05:00
|
|
|
return compile("(?<=" + (strict ? "[.]" : "[\\p{Punct}\\p{Space}]") + ")(" + join(quoteAll(languages), "|") + ")(?=[._ ]*$)", (strict ? 0 : CASE_INSENSITIVE) | UNICODE_CASE);
|
2011-12-30 10:34:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Pattern getResolutionPattern() {
|
|
|
|
// match screen resolutions 640x480, 1280x720, etc
|
|
|
|
return compile("(?<!\\p{Alnum})(\\d{4}|[6-9]\\d{2})x(\\d{4}|[4-9]\\d{2})(?!\\p{Alnum})");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-30 16:42:25 -05:00
|
|
|
public Pattern getVideoFormatPattern() {
|
2011-11-14 06:43:22 -05:00
|
|
|
// pattern matching any video source name
|
2011-12-30 16:42:25 -05:00
|
|
|
String pattern = getBundle(getClass().getName()).getString("pattern.video.format");
|
2011-11-14 06:43:22 -05:00
|
|
|
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
|
|
|
|
2012-06-22 03:47:26 -04:00
|
|
|
public Pattern getClutterBracketPattern(boolean strict) {
|
|
|
|
// match patterns like [Action, Drama] or {ENG-XViD-MP3-DVDRiP} etc
|
|
|
|
String contentFilter = strict ? "[\\p{Space}\\p{Punct}&&[^\\[\\]]]" : "\\p{Alpha}";
|
|
|
|
return compile("(?:\\[([^\\[\\]]+?" + contentFilter + "[^\\[\\]]+?)\\])|(?:\\{([^\\{\\}]+?" + contentFilter + "[^\\{\\}]+?)\\})|(?:\\(([^\\(\\)]+?" + contentFilter + "[^\\(\\)]+?)\\))");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-05 23:10:26 -04:00
|
|
|
public Pattern getReleaseGroupPattern(boolean strict) throws IOException {
|
2011-11-14 06:43:22 -05:00
|
|
|
// pattern matching any release group name enclosed in separators
|
2012-07-26 04:45:15 -04:00
|
|
|
return compile("(?<!\\p{Alnum})(" + join(releaseGroupResource.get(), "|") + ")(?!\\p{Alnum})", strict ? 0 : CASE_INSENSITIVE | UNICODE_CASE);
|
2011-11-14 06:43:22 -05:00
|
|
|
}
|
|
|
|
|
2011-12-03 05:50:45 -05:00
|
|
|
|
2012-07-05 23:10:26 -04:00
|
|
|
public Pattern getBlacklistPattern() throws IOException {
|
2011-12-30 16:42:25 -05:00
|
|
|
// pattern matching any release group name enclosed in separators
|
2012-07-26 04:45:15 -04:00
|
|
|
return compile("(?<!\\p{Alnum})(" + join(queryBlacklistResource.get(), "|") + ")(?!\\p{Alnum})", CASE_INSENSITIVE | UNICODE_CASE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Pattern getExcludePattern() throws IOException {
|
|
|
|
// pattern matching any release group name enclosed in separators
|
|
|
|
return compile(join(excludeBlacklistResource.get(), "|"), CASE_INSENSITIVE | UNICODE_CASE);
|
2012-01-01 22:48:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-05 23:10:26 -04:00
|
|
|
public Movie[] getMovieList() throws IOException {
|
2012-01-01 22:48:24 -05:00
|
|
|
return movieListResource.get();
|
2011-12-30 16:42:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-14 07:57:25 -04:00
|
|
|
public TheTVDBSearchResult[] getTheTVDBIndex() throws IOException {
|
2013-03-17 10:19:11 -04:00
|
|
|
return tvdbIndexResource.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public AnidbSearchResult[] getAnidbIndex() throws IOException {
|
|
|
|
return anidbIndexResource.get();
|
2012-10-14 07:57:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-09 11:04:14 -04:00
|
|
|
public Map<Pattern, String> getSeriesDirectMappings() throws IOException {
|
|
|
|
Map<Pattern, String> mappings = new LinkedHashMap<Pattern, String>();
|
|
|
|
for (String line : seriesDirectMappingsResource.get()) {
|
|
|
|
String[] tsv = line.split("\t", 2);
|
|
|
|
if (tsv.length == 2) {
|
|
|
|
mappings.put(compile("(?<!\\p{Alnum})(" + tsv[0] + ")(?!\\p{Alnum})", CASE_INSENSITIVE | UNICODE_CASE), tsv[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mappings;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-10 11:43:09 -05:00
|
|
|
public FileFilter getDiskFolderFilter() {
|
|
|
|
return new FolderEntryFilter(compile(getBundle(getClass().getName()).getString("pattern.diskfolder.entry")));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-26 04:45:15 -04:00
|
|
|
public FileFilter getClutterFileFilter() throws IOException {
|
2013-03-28 05:04:35 -04:00
|
|
|
return new ClutterFileFilter(getExcludePattern(), 262144000); // only files smaller than 250 MB may be considered clutter
|
2012-06-15 06:45:35 -04:00
|
|
|
}
|
|
|
|
|
2011-11-26 10:41:58 -05:00
|
|
|
// fetch release group names online and try to update the data every other day
|
2012-01-01 22:48:24 -05:00
|
|
|
protected final CachedResource<String[]> releaseGroupResource = new PatternResource(getBundle(getClass().getName()).getString("url.release-groups"));
|
|
|
|
protected final CachedResource<String[]> queryBlacklistResource = new PatternResource(getBundle(getClass().getName()).getString("url.query-blacklist"));
|
2012-07-26 04:45:15 -04:00
|
|
|
protected final CachedResource<String[]> excludeBlacklistResource = new PatternResource(getBundle(getClass().getName()).getString("url.exclude-blacklist"));
|
2012-01-01 22:48:24 -05:00
|
|
|
protected final CachedResource<Movie[]> movieListResource = new MovieResource(getBundle(getClass().getName()).getString("url.movie-list"));
|
2012-10-09 11:04:14 -04:00
|
|
|
protected final CachedResource<String[]> seriesDirectMappingsResource = new PatternResource(getBundle(getClass().getName()).getString("url.series-mappings"));
|
2013-03-17 10:19:11 -04:00
|
|
|
protected final CachedResource<TheTVDBSearchResult[]> tvdbIndexResource = new TheTVDBIndexResource(getBundle(getClass().getName()).getString("url.thetvdb-index"));
|
|
|
|
protected final CachedResource<AnidbSearchResult[]> anidbIndexResource = new AnidbIndexResource(getBundle(getClass().getName()).getString("url.anidb-index"));
|
2011-12-30 16:42:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
protected static class PatternResource extends CachedResource<String[]> {
|
|
|
|
|
|
|
|
public PatternResource(String resource) {
|
|
|
|
super(resource, String[].class, 24 * 60 * 60 * 1000); // 24h update interval
|
|
|
|
}
|
|
|
|
|
2011-11-14 06:43:22 -05:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String[] process(ByteBuffer data) {
|
2011-12-30 16:42:25 -05:00
|
|
|
return compile("\\n").split(Charset.forName("UTF-8").decode(data));
|
2011-11-14 06:43:22 -05:00
|
|
|
}
|
2011-12-30 16:42:25 -05:00
|
|
|
}
|
2011-11-14 06:43:22 -05:00
|
|
|
|
2012-01-01 22:48:24 -05:00
|
|
|
|
|
|
|
protected static class MovieResource extends CachedResource<Movie[]> {
|
|
|
|
|
|
|
|
public MovieResource(String resource) {
|
2012-02-23 13:48:35 -05:00
|
|
|
super(resource, Movie[].class, 7 * 24 * 60 * 60 * 1000); // check for updates once a week
|
2012-01-01 22:48:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Movie[] process(ByteBuffer data) throws IOException {
|
|
|
|
Scanner scanner = new Scanner(new GZIPInputStream(new ByteBufferInputStream(data)), "UTF-8").useDelimiter("\t|\n");
|
|
|
|
|
|
|
|
List<Movie> movies = new ArrayList<Movie>();
|
|
|
|
while (scanner.hasNext()) {
|
|
|
|
int imdbid = scanner.nextInt();
|
2012-07-31 03:46:33 -04:00
|
|
|
String name = scanner.next().trim();
|
2012-01-01 22:48:24 -05:00
|
|
|
int year = scanner.nextInt();
|
2012-07-24 13:44:54 -04:00
|
|
|
movies.add(new Movie(name, year, imdbid, -1));
|
2012-01-01 22:48:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return movies.toArray(new Movie[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-10 11:43:09 -05:00
|
|
|
|
2012-10-14 07:57:25 -04:00
|
|
|
protected static class TheTVDBIndexResource extends CachedResource<TheTVDBSearchResult[]> {
|
|
|
|
|
|
|
|
public TheTVDBIndexResource(String resource) {
|
|
|
|
super(resource, TheTVDBSearchResult[].class, 7 * 24 * 60 * 60 * 1000); // check for updates once a week
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TheTVDBSearchResult[] process(ByteBuffer data) throws IOException {
|
|
|
|
Scanner scanner = new Scanner(new GZIPInputStream(new ByteBufferInputStream(data)), "UTF-8").useDelimiter("\t|\n");
|
|
|
|
|
|
|
|
List<TheTVDBSearchResult> tvshows = new ArrayList<TheTVDBSearchResult>();
|
2012-12-03 13:08:02 -05:00
|
|
|
while (scanner.hasNext() && scanner.hasNextInt()) {
|
2012-10-14 07:57:25 -04:00
|
|
|
int id = scanner.nextInt();
|
|
|
|
String name = scanner.next().trim();
|
|
|
|
tvshows.add(new TheTVDBSearchResult(name, id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return tvshows.toArray(new TheTVDBSearchResult[0]);
|
2012-02-11 09:03:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-17 10:19:11 -04:00
|
|
|
protected static class AnidbIndexResource extends CachedResource<AnidbSearchResult[]> {
|
|
|
|
|
|
|
|
public AnidbIndexResource(String resource) {
|
|
|
|
super(resource, AnidbSearchResult[].class, 7 * 24 * 60 * 60 * 1000); // check for updates once a week
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public AnidbSearchResult[] process(ByteBuffer data) throws IOException {
|
|
|
|
Scanner scanner = new Scanner(new GZIPInputStream(new ByteBufferInputStream(data)), "UTF-8").useDelimiter("\t|\n");
|
|
|
|
|
|
|
|
List<AnidbSearchResult> anime = new ArrayList<AnidbSearchResult>();
|
|
|
|
while (scanner.hasNext() && scanner.hasNextInt()) {
|
|
|
|
int aid = scanner.nextInt();
|
|
|
|
String primaryTitle = scanner.next().trim();
|
|
|
|
String englishTitle = scanner.next().trim();
|
|
|
|
|
|
|
|
if (englishTitle.isEmpty() || englishTitle.equals(primaryTitle)) {
|
|
|
|
anime.add(new AnidbSearchResult(aid, primaryTitle, null));
|
|
|
|
} else {
|
|
|
|
anime.add(new AnidbSearchResult(aid, primaryTitle, singletonMap("en", englishTitle)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return anime.toArray(new AnidbSearchResult[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-10 11:43:09 -05:00
|
|
|
protected static class FolderEntryFilter implements FileFilter {
|
|
|
|
|
|
|
|
private final Pattern entryPattern;
|
|
|
|
|
|
|
|
|
|
|
|
public FolderEntryFilter(Pattern entryPattern) {
|
|
|
|
this.entryPattern = entryPattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean accept(File dir) {
|
|
|
|
if (dir.isDirectory()) {
|
|
|
|
for (String entry : dir.list()) {
|
|
|
|
if (entryPattern.matcher(entry).matches()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-15 01:12:09 -05:00
|
|
|
|
2012-06-15 06:45:35 -04:00
|
|
|
public static class FileFolderNameFilter implements FileFilter {
|
|
|
|
|
|
|
|
private final Pattern namePattern;
|
|
|
|
|
|
|
|
|
|
|
|
public FileFolderNameFilter(Pattern namePattern) {
|
|
|
|
this.namePattern = namePattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file) {
|
|
|
|
return (namePattern.matcher(file.getName()).find() || (file.isFile() && namePattern.matcher(file.getParentFile().getName()).find()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-28 05:04:35 -04:00
|
|
|
public static class ClutterFileFilter extends FileFolderNameFilter {
|
|
|
|
|
|
|
|
private long maxFileSize;
|
|
|
|
|
|
|
|
|
|
|
|
public ClutterFileFilter(Pattern namePattern, long maxFileSize) {
|
|
|
|
super(namePattern);
|
|
|
|
this.maxFileSize = maxFileSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file) {
|
|
|
|
return super.accept(file) && file.isFile() && file.length() < maxFileSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-15 01:12:09 -05:00
|
|
|
private Collection<String> quoteAll(Collection<String> strings) {
|
|
|
|
List<String> patterns = new ArrayList<String>(strings.size());
|
|
|
|
for (String it : strings) {
|
|
|
|
patterns.add(Pattern.quote(it));
|
|
|
|
}
|
|
|
|
return patterns;
|
|
|
|
}
|
|
|
|
|
2012-06-22 03:47:26 -04:00
|
|
|
|
2012-02-15 01:12:09 -05:00
|
|
|
private Map<String, Locale> getLanguageMap(Locale... supportedDisplayLocale) {
|
|
|
|
// use maximum strength collator by default
|
|
|
|
Collator collator = Collator.getInstance(Locale.ROOT);
|
|
|
|
collator.setDecomposition(Collator.FULL_DECOMPOSITION);
|
|
|
|
collator.setStrength(Collator.PRIMARY);
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
Comparator<String> order = (Comparator) collator;
|
2012-07-24 16:01:48 -04:00
|
|
|
Map<String, Locale> languageMap = languageMap = new TreeMap<String, Locale>(order);
|
2012-02-15 01:12:09 -05:00
|
|
|
|
|
|
|
for (String code : Locale.getISOLanguages()) {
|
|
|
|
Locale locale = new Locale(code);
|
|
|
|
languageMap.put(locale.getLanguage(), locale);
|
|
|
|
languageMap.put(locale.getISO3Language(), locale);
|
|
|
|
|
|
|
|
// map display language names for given locales
|
2012-07-24 16:01:48 -04:00
|
|
|
for (Locale language : new HashSet<Locale>(asList(supportedDisplayLocale))) {
|
2012-02-15 01:12:09 -05:00
|
|
|
// make sure language name is properly normalized so accents and whatever don't break the regex pattern syntax
|
|
|
|
String languageName = Normalizer.normalize(locale.getDisplayLanguage(language), Form.NFKD);
|
2012-11-22 11:45:40 -05:00
|
|
|
languageMap.put(languageName.toLowerCase(), locale);
|
2012-02-15 01:12:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove illegal tokens
|
|
|
|
languageMap.remove("");
|
2012-06-22 03:47:26 -04:00
|
|
|
languageMap.remove("II");
|
|
|
|
languageMap.remove("III");
|
2013-01-27 01:04:32 -05:00
|
|
|
languageMap.remove("hi"); // hi => hearing-impaired subtitles, NOT hindi language
|
2012-06-22 03:47:26 -04:00
|
|
|
|
|
|
|
Map<String, Locale> result = unmodifiableMap(languageMap);
|
|
|
|
return result;
|
2012-02-15 01:12:09 -05:00
|
|
|
}
|
2011-11-14 06:43:22 -05:00
|
|
|
}
|